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" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 9 | |
Brian Salomon | 028a9a5 | 2017-05-11 11:39:08 -0400 | [diff] [blame] | 10 | #include <cmath> |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 11 | #include "../private/GrGLSL.h" |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 12 | #include "GrBackendSemaphore.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 13 | #include "GrBackendSurface.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 14 | #include "GrFixedClip.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 15 | #include "GrGLBuffer.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 16 | #include "GrGLGpuCommandBuffer.h" |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 17 | #include "GrGLSemaphore.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 18 | #include "GrGLStencilAttachment.h" |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 19 | #include "GrGLTextureRenderTarget.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 20 | #include "GrGpuResourcePriv.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 21 | #include "GrMesh.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 22 | #include "GrPipeline.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 23 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 24 | #include "GrShaderCaps.h" |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 25 | #include "GrSurfaceProxyPriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 26 | #include "GrTexturePriv.h" |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 27 | #include "GrTypes.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 28 | #include "SkAutoMalloc.h" |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 29 | #include "SkHalf.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 30 | #include "SkJSONWriter.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 31 | #include "SkMakeUnique.h" |
| 32 | #include "SkMipMap.h" |
| 33 | #include "SkPixmap.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 34 | #include "SkSLCompiler.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 35 | #include "SkStrokeRec.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 36 | #include "SkTemplates.h" |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 37 | #include "SkTraceEvent.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 38 | #include "SkTypes.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 39 | #include "builders/GrGLShaderStringBuilder.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 41 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 42 | #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 43 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | #define SKIP_CACHE_CHECK true |
| 45 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 46 | #if GR_GL_CHECK_ALLOC_WITH_GET_ERROR |
| 47 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface) |
| 48 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call) |
| 49 | #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 50 | #else |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 51 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) |
| 52 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call) |
| 53 | #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR |
| 54 | #endif |
| 55 | |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 56 | //#define USE_NSIGHT |
| 57 | |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 60 | static const GrGLenum gXfermodeEquation2Blend[] = { |
| 61 | // Basic OpenGL blend equations. |
| 62 | GR_GL_FUNC_ADD, |
| 63 | GR_GL_FUNC_SUBTRACT, |
| 64 | GR_GL_FUNC_REVERSE_SUBTRACT, |
| 65 | |
| 66 | // GL_KHR_blend_equation_advanced. |
| 67 | GR_GL_SCREEN, |
| 68 | GR_GL_OVERLAY, |
| 69 | GR_GL_DARKEN, |
| 70 | GR_GL_LIGHTEN, |
| 71 | GR_GL_COLORDODGE, |
| 72 | GR_GL_COLORBURN, |
| 73 | GR_GL_HARDLIGHT, |
| 74 | GR_GL_SOFTLIGHT, |
| 75 | GR_GL_DIFFERENCE, |
| 76 | GR_GL_EXCLUSION, |
| 77 | GR_GL_MULTIPLY, |
| 78 | GR_GL_HSL_HUE, |
| 79 | GR_GL_HSL_SATURATION, |
| 80 | GR_GL_HSL_COLOR, |
| 81 | GR_GL_HSL_LUMINOSITY |
| 82 | }; |
| 83 | GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation); |
| 84 | GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation); |
| 85 | GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation); |
| 86 | GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation); |
| 87 | GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation); |
| 88 | GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation); |
| 89 | GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation); |
| 90 | GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation); |
| 91 | GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation); |
| 92 | GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation); |
| 93 | GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation); |
| 94 | GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation); |
| 95 | GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation); |
| 96 | GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation); |
| 97 | GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation); |
| 98 | GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation); |
| 99 | GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation); |
| 100 | GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation); |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 101 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 102 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 103 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 104 | GR_GL_ZERO, |
| 105 | GR_GL_ONE, |
| 106 | GR_GL_SRC_COLOR, |
| 107 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 108 | GR_GL_DST_COLOR, |
| 109 | GR_GL_ONE_MINUS_DST_COLOR, |
| 110 | GR_GL_SRC_ALPHA, |
| 111 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 112 | GR_GL_DST_ALPHA, |
| 113 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 114 | GR_GL_CONSTANT_COLOR, |
| 115 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 116 | GR_GL_CONSTANT_ALPHA, |
| 117 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 118 | |
| 119 | // extended blend coeffs |
| 120 | GR_GL_SRC1_COLOR, |
| 121 | GR_GL_ONE_MINUS_SRC1_COLOR, |
| 122 | GR_GL_SRC1_ALPHA, |
| 123 | GR_GL_ONE_MINUS_SRC1_ALPHA, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 126 | bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 127 | static const bool gCoeffReferencesBlendConst[] = { |
| 128 | false, |
| 129 | false, |
| 130 | false, |
| 131 | false, |
| 132 | false, |
| 133 | false, |
| 134 | false, |
| 135 | false, |
| 136 | false, |
| 137 | false, |
| 138 | true, |
| 139 | true, |
| 140 | true, |
| 141 | true, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 142 | |
| 143 | // extended blend coeffs |
| 144 | false, |
| 145 | false, |
| 146 | false, |
| 147 | false, |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 148 | }; |
| 149 | return gCoeffReferencesBlendConst[coeff]; |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 150 | GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 152 | GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff); |
| 153 | GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff); |
| 154 | GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff); |
| 155 | GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff); |
| 156 | GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff); |
| 157 | GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff); |
| 158 | GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff); |
| 159 | GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff); |
| 160 | GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff); |
| 161 | GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff); |
| 162 | GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff); |
| 163 | GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff); |
| 164 | GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff); |
| 165 | GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 166 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 167 | GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff); |
| 168 | GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff); |
| 169 | GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff); |
| 170 | GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 171 | |
| 172 | // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 173 | GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 178 | sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options, |
| 179 | GrContext* context) { |
| 180 | if (!interface) { |
Brian Salomon | 3d6801e | 2017-12-11 10:06:31 -0500 | [diff] [blame] | 181 | interface = GrGLMakeNativeInterface(); |
| 182 | // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated |
| 183 | // to GrGLMakeNativeInterface. |
| 184 | if (!interface) { |
| 185 | interface = sk_ref_sp(GrGLCreateNativeInterface()); |
| 186 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 187 | if (!interface) { |
| 188 | return nullptr; |
| 189 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 190 | } |
Jim Van Verth | 7633477 | 2017-05-05 16:46:05 -0400 | [diff] [blame] | 191 | #ifdef USE_NSIGHT |
| 192 | const_cast<GrContextOptions&>(options).fSuppressPathRendering = true; |
| 193 | #endif |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 194 | auto glContext = GrGLContext::Make(std::move(interface), options); |
| 195 | if (!glContext) { |
| 196 | return nullptr; |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 197 | } |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 198 | return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context)); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 201 | GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context) |
| 202 | : GrGpu(context) |
| 203 | , fGLContext(std::move(ctx)) |
| 204 | , fProgramCache(new ProgramCache(this)) |
| 205 | , fHWProgramID(0) |
| 206 | , fTempSrcFBOID(0) |
| 207 | , fTempDstFBOID(0) |
| 208 | , fStencilClearFBOID(0) |
| 209 | , fHWMaxUsedBufferTextureUnit(-1) |
| 210 | , fHWMinSampleShading(0.0) { |
| 211 | SkASSERT(fGLContext); |
| 212 | fCaps = sk_ref_sp(fGLContext->caps()); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 213 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 214 | fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 215 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 216 | fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER; |
| 217 | fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER; |
| 218 | fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER; |
| 219 | fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER; |
| 220 | if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) { |
| 221 | fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = |
| 222 | GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM; |
| 223 | fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = |
| 224 | GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM; |
| 225 | } else { |
| 226 | fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER; |
| 227 | fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER; |
| 228 | } |
Rob Phillips | bc534f6 | 2017-09-05 19:56:19 -0400 | [diff] [blame] | 229 | for (int i = 0; i < kGrBufferTypeCount; ++i) { |
| 230 | fHWBufferState[i].invalidate(); |
| 231 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 232 | GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState)); |
| 233 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 234 | if (this->caps()->shaderCaps()->texelBufferSupport()) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 235 | fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 238 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
| 239 | fPathRendering.reset(new GrGLPathRendering(this)); |
| 240 | } |
| 241 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 242 | GrGLClearErr(this->glInterface()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 245 | GrGLGpu::~GrGLGpu() { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 246 | // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu |
| 247 | // to release the resources held by the objects themselves. |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 248 | fPathRendering.reset(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 249 | fCopyProgramArrayBuffer.reset(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 250 | fMipmapProgramArrayBuffer.reset(); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 251 | fStencilClipClearArrayBuffer.reset(); |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 252 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 253 | if (fHWProgramID) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 254 | // detach the current program so there is no confusion on OpenGL's part |
| 255 | // that we want it to be deleted |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 256 | GL_CALL(UseProgram(0)); |
| 257 | } |
| 258 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 259 | if (fTempSrcFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 260 | this->deleteFramebuffer(fTempSrcFBOID); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 261 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 262 | if (fTempDstFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 263 | this->deleteFramebuffer(fTempDstFBOID); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 264 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 265 | if (fStencilClearFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 266 | this->deleteFramebuffer(fStencilClearFBOID); |
bsalomon | dd3143b | 2015-02-23 09:27:45 -0800 | [diff] [blame] | 267 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 268 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 269 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 270 | if (0 != fCopyPrograms[i].fProgram) { |
| 271 | GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram)); |
| 272 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 273 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 274 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 275 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 276 | if (0 != fMipmapPrograms[i].fProgram) { |
| 277 | GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram)); |
| 278 | } |
| 279 | } |
| 280 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 281 | if (fStencilClipClearProgram) { |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 282 | GL_CALL(DeleteProgram(fStencilClipClearProgram)); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 285 | if (fClearColorProgram.fProgram) { |
| 286 | GL_CALL(DeleteProgram(fClearColorProgram.fProgram)); |
| 287 | } |
| 288 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 289 | delete fProgramCache; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 290 | } |
| 291 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 292 | void GrGLGpu::disconnect(DisconnectType type) { |
| 293 | INHERITED::disconnect(type); |
| 294 | if (DisconnectType::kCleanup == type) { |
| 295 | if (fHWProgramID) { |
| 296 | GL_CALL(UseProgram(0)); |
| 297 | } |
| 298 | if (fTempSrcFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 299 | this->deleteFramebuffer(fTempSrcFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 300 | } |
| 301 | if (fTempDstFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 302 | this->deleteFramebuffer(fTempDstFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 303 | } |
| 304 | if (fStencilClearFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 305 | this->deleteFramebuffer(fStencilClearFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 306 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 307 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 308 | if (fCopyPrograms[i].fProgram) { |
| 309 | GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram)); |
| 310 | } |
| 311 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 312 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 313 | if (fMipmapPrograms[i].fProgram) { |
| 314 | GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram)); |
| 315 | } |
| 316 | } |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 317 | if (fStencilClipClearProgram) { |
| 318 | GL_CALL(DeleteProgram(fStencilClipClearProgram)); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 319 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 320 | |
| 321 | if (fClearColorProgram.fProgram) { |
| 322 | GL_CALL(DeleteProgram(fClearColorProgram.fProgram)); |
| 323 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 324 | } else { |
| 325 | if (fProgramCache) { |
| 326 | fProgramCache->abandon(); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | delete fProgramCache; |
| 331 | fProgramCache = nullptr; |
| 332 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 333 | fHWProgramID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 334 | fTempSrcFBOID = 0; |
| 335 | fTempDstFBOID = 0; |
| 336 | fStencilClearFBOID = 0; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 337 | fCopyProgramArrayBuffer.reset(); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 338 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 339 | fCopyPrograms[i].fProgram = 0; |
| 340 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 341 | fMipmapProgramArrayBuffer.reset(); |
| 342 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 343 | fMipmapPrograms[i].fProgram = 0; |
| 344 | } |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 345 | fStencilClipClearProgram = 0; |
| 346 | fStencilClipClearArrayBuffer.reset(); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 347 | fClearColorProgram.fProgram = 0; |
| 348 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 349 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 350 | this->glPathRendering()->disconnect(type); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 351 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 352 | } |
| 353 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 354 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 355 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 356 | void GrGLGpu::onResetContext(uint32_t resetBits) { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 357 | if (resetBits & kMisc_GrGLBackendState) { |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 358 | // we don't use the zb at all |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 359 | GL_CALL(Disable(GR_GL_DEPTH_TEST)); |
| 360 | GL_CALL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 361 | |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 362 | // We don't use face culling. |
| 363 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
| 364 | // We do use separate stencil. Our algorithms don't care which face is front vs. back so |
| 365 | // just set this to the default for self-consistency. |
| 366 | GL_CALL(FrontFace(GR_GL_CCW)); |
| 367 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 368 | fHWBufferState[kTexel_GrBufferType].invalidate(); |
| 369 | fHWBufferState[kDrawIndirect_GrBufferType].invalidate(); |
| 370 | fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate(); |
| 371 | fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate(); |
cdalton | c161310 | 2016-03-16 07:48:20 -0700 | [diff] [blame] | 372 | |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 373 | if (kGL_GrGLStandard == this->glStandard()) { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 374 | #ifndef USE_NSIGHT |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 375 | // Desktop-only state that we never change |
| 376 | if (!this->glCaps().isCoreProfile()) { |
| 377 | GL_CALL(Disable(GR_GL_POINT_SMOOTH)); |
| 378 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 379 | GL_CALL(Disable(GR_GL_POLYGON_SMOOTH)); |
| 380 | GL_CALL(Disable(GR_GL_POLYGON_STIPPLE)); |
| 381 | GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP)); |
| 382 | GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP)); |
| 383 | } |
| 384 | // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a |
| 385 | // core profile. This seems like a bug since the core spec removes any mention of |
| 386 | // GL_ARB_imaging. |
| 387 | if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) { |
| 388 | GL_CALL(Disable(GR_GL_COLOR_TABLE)); |
| 389 | } |
| 390 | GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL)); |
Jim Van Verth | 609e7cc | 2017-03-30 14:28:08 -0400 | [diff] [blame] | 391 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 392 | if (this->caps()->wireframeMode()) { |
| 393 | GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE)); |
| 394 | } else { |
| 395 | GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL)); |
| 396 | } |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 397 | #endif |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 398 | // Since ES doesn't support glPointSize at all we always use the VS to |
| 399 | // set the point size |
| 400 | GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE)); |
| 401 | |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 402 | } |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 403 | |
| 404 | if (kGLES_GrGLStandard == this->glStandard() && |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 405 | this->hasExtension("GL_ARM_shader_framebuffer_fetch")) { |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 406 | // The arm extension requires specifically enabling MSAA fetching per sample. |
| 407 | // On some devices this may have a perf hit. Also multiple render targets are disabled |
| 408 | GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM)); |
| 409 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 410 | fHWWriteToColor = kUnknown_TriState; |
| 411 | // we only ever use lines in hairline mode |
| 412 | GL_CALL(LineWidth(1)); |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 413 | GL_CALL(Disable(GR_GL_DITHER)); |
bsalomon@google.com | cad107b | 2013-06-28 14:32:08 +0000 | [diff] [blame] | 414 | } |
edisonn@google.com | ba66999 | 2013-06-28 16:03:21 +0000 | [diff] [blame] | 415 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 416 | if (resetBits & kMSAAEnable_GrGLBackendState) { |
| 417 | fMSAAEnabled = kUnknown_TriState; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 418 | |
egdaniel | eed519e | 2016-01-15 11:36:18 -0800 | [diff] [blame] | 419 | if (this->caps()->usesMixedSamples()) { |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 420 | if (0 != this->caps()->maxRasterSamples()) { |
| 421 | fHWRasterMultisampleEnabled = kUnknown_TriState; |
| 422 | fHWNumRasterSamples = 0; |
| 423 | } |
| 424 | |
| 425 | // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage |
| 426 | // 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] | 427 | GL_CALL(CoverageModulation(GR_GL_RGBA)); |
| 428 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 429 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 430 | |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 431 | fHWActiveTextureUnitIdx = -1; // invalid |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 432 | fLastPrimitiveType = static_cast<GrPrimitiveType>(-1); |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 433 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 434 | if (resetBits & kTextureBinding_GrGLBackendState) { |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 435 | for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 436 | fHWBoundTextureUniqueIDs[s].makeInvalid(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 437 | } |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 438 | for (int b = 0; b < fHWBufferTextures.count(); ++b) { |
| 439 | SkASSERT(this->caps()->shaderCaps()->texelBufferSupport()); |
| 440 | fHWBufferTextures[b].fKnownBound = false; |
| 441 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 442 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 443 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 444 | if (resetBits & kBlend_GrGLBackendState) { |
| 445 | fHWBlendState.invalidate(); |
| 446 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 447 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 448 | if (resetBits & kView_GrGLBackendState) { |
| 449 | fHWScissorSettings.invalidate(); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 450 | fHWWindowRectsState.invalidate(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 451 | fHWViewport.invalidate(); |
| 452 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 454 | if (resetBits & kStencil_GrGLBackendState) { |
| 455 | fHWStencilSettings.invalidate(); |
| 456 | fHWStencilTestEnabled = kUnknown_TriState; |
| 457 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 458 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 459 | // Vertex |
| 460 | if (resetBits & kVertex_GrGLBackendState) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 461 | fHWVertexArrayState.invalidate(); |
| 462 | fHWBufferState[kVertex_GrBufferType].invalidate(); |
| 463 | fHWBufferState[kIndex_GrBufferType].invalidate(); |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 464 | if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) { |
| 465 | fRequiresFlushBeforeNextInstancedDraw = true; |
| 466 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 467 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 469 | if (resetBits & kRenderTarget_GrGLBackendState) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 470 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 471 | fHWSRGBFramebuffer = kUnknown_TriState; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 472 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 473 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 474 | if (resetBits & kPathRendering_GrGLBackendState) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 475 | if (this->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 476 | this->glPathRendering()->resetContext(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 477 | } |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 478 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 479 | |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 480 | // we assume these values |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 481 | if (resetBits & kPixelStore_GrGLBackendState) { |
| 482 | if (this->glCaps().unpackRowLengthSupport()) { |
| 483 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 484 | } |
| 485 | if (this->glCaps().packRowLengthSupport()) { |
| 486 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 487 | } |
| 488 | if (this->glCaps().unpackFlipYSupport()) { |
| 489 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 490 | } |
| 491 | if (this->glCaps().packFlipYSupport()) { |
| 492 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 493 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 494 | } |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 495 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 496 | if (resetBits & kProgram_GrGLBackendState) { |
| 497 | fHWProgramID = 0; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 498 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 501 | static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps, |
| 502 | GrGLTexture::IDDesc* idDesc) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 503 | GrGLTextureInfo info; |
| 504 | if (!backendTex.getGLTextureInfo(&info) || !info.fID) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 505 | return false; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 506 | } |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 507 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 508 | idDesc->fInfo = info; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 509 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 510 | if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) { |
| 511 | if (!caps.shaderCaps()->externalTextureSupport()) { |
| 512 | return false; |
| 513 | } |
| 514 | } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) { |
| 515 | if (!caps.rectangleTextureSupport()) { |
| 516 | return false; |
| 517 | } |
| 518 | } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) { |
| 519 | return false; |
| 520 | } |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 525 | GrWrapOwnership ownership) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 526 | GrGLTexture::IDDesc idDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 527 | if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) { |
| 528 | return nullptr; |
| 529 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 530 | if (!idDesc.fInfo.fFormat) { |
| 531 | idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config()); |
| 532 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 533 | if (kBorrow_GrWrapOwnership == ownership) { |
| 534 | idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
| 535 | } else { |
| 536 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
| 537 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 538 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 539 | GrSurfaceDesc surfDesc; |
| 540 | surfDesc.fFlags = kNone_GrSurfaceFlags; |
| 541 | surfDesc.fWidth = backendTex.width(); |
| 542 | surfDesc.fHeight = backendTex.height(); |
| 543 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 544 | surfDesc.fSampleCnt = 1; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 545 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 546 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid |
| 547 | : GrMipMapsStatus::kNotAllocated; |
| 548 | |
| 549 | return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 553 | int sampleCnt, |
| 554 | GrWrapOwnership ownership) { |
| 555 | GrGLTexture::IDDesc idDesc; |
| 556 | if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 557 | return nullptr; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 558 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 559 | if (!idDesc.fInfo.fFormat) { |
| 560 | idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config()); |
| 561 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 562 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 563 | // We don't support rendering to a EXTERNAL texture. |
| 564 | if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) { |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 565 | return nullptr; |
| 566 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 567 | |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 568 | if (kBorrow_GrWrapOwnership == ownership) { |
Brian Osman | a6953f2 | 2017-03-10 20:14:05 +0000 | [diff] [blame] | 569 | idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 570 | } else { |
| 571 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 572 | } |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 573 | |
Ben Wagner | 18b61f9 | 2016-10-25 10:44:02 -0400 | [diff] [blame] | 574 | GrSurfaceDesc surfDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 575 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 576 | surfDesc.fWidth = backendTex.width(); |
| 577 | surfDesc.fHeight = backendTex.height(); |
| 578 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 579 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config()); |
| 580 | if (surfDesc.fSampleCnt < 1) { |
| 581 | return nullptr; |
| 582 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 583 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 584 | GrGLRenderTarget::IDDesc rtIDDesc; |
| 585 | if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) { |
| 586 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 587 | } |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 588 | |
| 589 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty |
| 590 | : GrMipMapsStatus::kNotAllocated; |
| 591 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 592 | sk_sp<GrGLTextureRenderTarget> texRT( |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 593 | GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 594 | texRT->baseLevelWasBoundToFBO(); |
| 595 | return std::move(texRT); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 598 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 599 | GrGLFramebufferInfo info; |
| 600 | if (!backendRT.getGLFramebufferInfo(&info)) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 601 | return nullptr; |
| 602 | } |
| 603 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 604 | GrGLRenderTarget::IDDesc idDesc; |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 605 | idDesc.fRTFBOID = info.fFBOID; |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 606 | idDesc.fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 607 | idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
Brian Osman | 0b791f5 | 2017-03-10 08:30:22 -0500 | [diff] [blame] | 608 | idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed; |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 609 | idDesc.fIsMixedSampled = false; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 610 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 611 | GrSurfaceDesc desc; |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 612 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 613 | desc.fWidth = backendRT.width(); |
| 614 | desc.fHeight = backendRT.height(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 615 | desc.fConfig = backendRT.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 616 | desc.fSampleCnt = |
| 617 | this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config()); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 618 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 619 | return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits()); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 622 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 623 | int sampleCnt) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 624 | GrGLTextureInfo info; |
| 625 | if (!tex.getGLTextureInfo(&info) || !info.fID) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 626 | return nullptr; |
| 627 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 628 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 629 | if (GR_GL_TEXTURE_RECTANGLE != info.fTarget && |
| 630 | GR_GL_TEXTURE_2D != info.fTarget) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 631 | // Only texture rectangle and texture 2d are supported. We do not check whether texture |
| 632 | // rectangle is supported by Skia - if the caller provided us with a texture rectangle, |
| 633 | // we assume the necessary support exists. |
| 634 | return nullptr; |
| 635 | } |
| 636 | |
Ben Wagner | 18b61f9 | 2016-10-25 10:44:02 -0400 | [diff] [blame] | 637 | GrSurfaceDesc surfDesc; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 638 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 639 | surfDesc.fWidth = tex.width(); |
| 640 | surfDesc.fHeight = tex.height(); |
| 641 | surfDesc.fConfig = tex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 642 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config()); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 643 | |
| 644 | GrGLRenderTarget::IDDesc rtIDDesc; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 645 | if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 646 | return nullptr; |
| 647 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 648 | return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 649 | } |
| 650 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 651 | //////////////////////////////////////////////////////////////////////////////// |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 652 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 653 | bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width, |
| 654 | int height, GrColorType srcColorType, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 655 | DrawPreference* drawPreference, |
| 656 | WritePixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 657 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 658 | auto srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(dstSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 659 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 660 | // We assume the base class has only inserted a draw for sRGB reasons. So the temp surface |
| 661 | // has the config of the original src data. There is no swizzling nor src config spoofing. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 662 | SkASSERT(tempDrawInfo->fWriteColorType == srcColorType); |
| 663 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == srcColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 664 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 665 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 666 | srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
| 667 | } |
| 668 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
| 669 | srcConfigSRGBEncoded = GrSRGBEncoded::kNo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 672 | if (SkToBool(dstSurface->asRenderTarget())) { |
| 673 | if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) { |
| 674 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture()); |
| 679 | |
| 680 | if (texture) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 681 | if (GR_GL_TEXTURE_EXTERNAL == texture->target()) { |
| 682 | // We don't currently support writing pixels to EXTERNAL textures. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 683 | return false; |
| 684 | } |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 685 | if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() && |
| 686 | this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() && |
| 687 | (width < dstSurface->width() || height < dstSurface->height())) { |
| 688 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 689 | } |
| 690 | } else { |
| 691 | // This subclass only allows writes to textures. If the dst is not a texture we have to draw |
| 692 | // into it. We could use glDrawPixels on GLs that have it, but we don't today. |
| 693 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 696 | // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 697 | if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 1) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 698 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 699 | } |
| 700 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 701 | auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srcConfigSRGBEncoded); |
| 702 | SkASSERT(srcAsConfig != kUnknown_GrPixelConfig); |
| 703 | auto dstColorType = GrPixelConfigToColorType(dstSurface->config()); |
| 704 | bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcAsConfig) == dstSurface->config(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 705 | |
| 706 | if (configsAreRBSwaps) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 707 | if (!this->caps()->isConfigTexturable(srcAsConfig)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 708 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 709 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 710 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 711 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | 88c7b98 | 2015-07-31 11:20:16 -0700 | [diff] [blame] | 712 | } else if (this->glCaps().rgba8888PixelsOpsAreSlow() && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 713 | kRGBA_8888_GrPixelConfig == srcAsConfig) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 714 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 715 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 716 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 717 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 718 | } else if (kGLES_GrGLStandard == this->glStandard() && |
| 719 | this->glCaps().bgraIsInternalFormat()) { |
| 720 | // The internal format and external formats must match texture uploads so we can't |
| 721 | // swizzle while uploading when BGRA is a distinct internal format. |
| 722 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 723 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 724 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 725 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 729 | if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 730 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 731 | } |
| 732 | |
| 733 | return true; |
| 734 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 735 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 736 | static bool check_write_and_transfer_input(GrGLTexture* glTex) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 737 | if (!glTex) { |
| 738 | return false; |
| 739 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 740 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 741 | // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures |
| 742 | if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 743 | return false; |
| 744 | } |
| 745 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 746 | return true; |
| 747 | } |
| 748 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 749 | bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 750 | int width, int height, GrColorType srcColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 751 | const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 752 | auto glTex = static_cast<GrGLTexture*>(surface->asTexture()); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 753 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 754 | if (!check_write_and_transfer_input(glTex)) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 755 | return false; |
| 756 | } |
| 757 | |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 758 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 759 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 760 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 761 | // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the |
| 762 | // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match. |
| 763 | // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know |
| 764 | // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our |
| 765 | // caps knows to make the external format be GL_RGBA. |
| 766 | auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config()); |
| 767 | auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 768 | return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), origin, |
| 769 | glTex->target(), kWrite_UploadType, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 770 | srcAsConfig, texels, mipLevelCount); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 773 | // For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType. |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 774 | static inline GrGLint config_alignment(GrPixelConfig config) { |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 775 | switch (config) { |
| 776 | case kAlpha_8_GrPixelConfig: |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 777 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 778 | case kAlpha_8_as_Red_GrPixelConfig: |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 779 | case kGray_8_GrPixelConfig: |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 780 | case kGray_8_as_Lum_GrPixelConfig: |
| 781 | case kGray_8_as_Red_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 782 | return 1; |
| 783 | case kRGB_565_GrPixelConfig: |
| 784 | case kRGBA_4444_GrPixelConfig: |
| 785 | case kAlpha_half_GrPixelConfig: |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 786 | case kAlpha_half_as_Red_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 787 | case kRGBA_half_GrPixelConfig: |
| 788 | return 2; |
| 789 | case kRGBA_8888_GrPixelConfig: |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 790 | case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here. |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 791 | case kBGRA_8888_GrPixelConfig: |
| 792 | case kSRGBA_8888_GrPixelConfig: |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 793 | case kSBGRA_8888_GrPixelConfig: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 794 | case kRGBA_1010102_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 795 | case kRGBA_float_GrPixelConfig: |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 796 | case kRG_float_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 797 | return 4; |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 798 | case kUnknown_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 799 | return 0; |
| 800 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 801 | SK_ABORT("Invalid pixel config"); |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 802 | return 0; |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 805 | bool GrGLGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 806 | GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 807 | size_t rowBytes) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 808 | GrGLTexture* glTex = static_cast<GrGLTexture*>(texture); |
| 809 | GrPixelConfig texConfig = glTex->config(); |
| 810 | SkASSERT(this->caps()->isConfigTexturable(texConfig)); |
| 811 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 812 | if (!check_write_and_transfer_input(glTex)) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 813 | return false; |
| 814 | } |
| 815 | |
| 816 | if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) { |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | this->setScratchTextureUnit(); |
| 821 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
| 822 | |
| 823 | SkASSERT(!transferBuffer->isMapped()); |
| 824 | SkASSERT(!transferBuffer->isCPUBacked()); |
| 825 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer); |
| 826 | this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer); |
| 827 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 828 | SkDEBUGCODE( |
| 829 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 830 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 831 | SkASSERT(bounds.contains(subRect)); |
| 832 | ) |
| 833 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 834 | int bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 835 | const size_t trimRowBytes = width * bpp; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 836 | if (!rowBytes) { |
| 837 | rowBytes = trimRowBytes; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 838 | } |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 839 | const void* pixels = (void*)offset; |
Bruce Dawson | 71479b7 | 2017-07-05 14:30:20 -0700 | [diff] [blame] | 840 | if (width < 0 || height < 0) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 841 | return false; |
| 842 | } |
| 843 | |
| 844 | bool restoreGLRowLength = false; |
| 845 | if (trimRowBytes != rowBytes) { |
| 846 | // we should have checked for this support already |
| 847 | SkASSERT(this->glCaps().unpackRowLengthSupport()); |
| 848 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp)); |
| 849 | restoreGLRowLength = true; |
| 850 | } |
| 851 | |
| 852 | // Internal format comes from the texture desc. |
| 853 | GrGLenum internalFormat; |
| 854 | // External format and type come from the upload data. |
| 855 | GrGLenum externalFormat; |
| 856 | GrGLenum externalType; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 857 | auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo); |
| 858 | if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 859 | &externalFormat, &externalType)) { |
| 860 | return false; |
| 861 | } |
| 862 | |
| 863 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig))); |
| 864 | GL_CALL(TexSubImage2D(glTex->target(), |
| 865 | 0, |
| 866 | left, top, |
| 867 | width, |
| 868 | height, |
| 869 | externalFormat, externalType, |
| 870 | pixels)); |
| 871 | |
| 872 | if (restoreGLRowLength) { |
| 873 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 874 | } |
| 875 | |
| 876 | return true; |
| 877 | } |
| 878 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 879 | /** |
| 880 | * Creates storage space for the texture and fills it with texels. |
| 881 | * |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 882 | * @param config Pixel config of the texture. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 883 | * @param interface The GL interface in use. |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 884 | * @param caps The capabilities of the GL device. |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 885 | * @param internalFormat The data format used for the internal storage of the texture. May be sized. |
| 886 | * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 887 | * @param externalFormat The data format used for the external storage of the texture. |
| 888 | * @param externalType The type of the data used for the external storage of the texture. |
| 889 | * @param texels The texel data of the texture being created. |
| 890 | * @param baseWidth The width of the texture's base mipmap level |
| 891 | * @param baseHeight The height of the texture's base mipmap level |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 892 | */ |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 893 | static bool allocate_and_populate_texture(GrPixelConfig config, |
| 894 | const GrGLInterface& interface, |
| 895 | const GrGLCaps& caps, |
| 896 | GrGLenum target, |
| 897 | GrGLenum internalFormat, |
| 898 | GrGLenum internalFormatForTexStorage, |
| 899 | GrGLenum externalFormat, |
| 900 | GrGLenum externalType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 901 | const GrMipLevel texels[], int mipLevelCount, |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 902 | int baseWidth, int baseHeight) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 903 | CLEAR_ERROR_BEFORE_ALLOC(&interface); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 904 | |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 905 | bool useTexStorage = caps.isConfigTexSupportEnabled(config); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 906 | // We can only use TexStorage if we know we will not later change the storage requirements. |
| 907 | // This means if we may later want to add mipmaps, we cannot use TexStorage. |
| 908 | // Right now, we cannot know if we will later add mipmaps or not. |
| 909 | // The only time we can use TexStorage is when we already have the |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 910 | // mipmaps. |
| 911 | useTexStorage &= mipLevelCount > 1; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 912 | |
| 913 | if (useTexStorage) { |
| 914 | // We never resize or change formats of textures. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 915 | GL_ALLOC_CALL(&interface, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 916 | TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage, |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 917 | baseWidth, baseHeight)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 918 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 919 | if (error != GR_GL_NO_ERROR) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 920 | return false; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 921 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 922 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 923 | const void* currentMipData = texels[currentMipLevel].fPixels; |
| 924 | if (currentMipData == nullptr) { |
| 925 | continue; |
| 926 | } |
| 927 | int twoToTheMipLevel = 1 << currentMipLevel; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 928 | int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel); |
| 929 | int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 930 | |
| 931 | GR_GL_CALL(&interface, |
| 932 | TexSubImage2D(target, |
| 933 | currentMipLevel, |
| 934 | 0, // left |
| 935 | 0, // top |
| 936 | currentWidth, |
| 937 | currentHeight, |
| 938 | externalFormat, externalType, |
| 939 | currentMipData)); |
| 940 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 941 | return true; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 942 | } |
| 943 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 944 | if (!mipLevelCount) { |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 945 | GL_ALLOC_CALL(&interface, |
| 946 | TexImage2D(target, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 947 | 0, |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 948 | internalFormat, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 949 | baseWidth, |
| 950 | baseHeight, |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 951 | 0, // border |
| 952 | externalFormat, externalType, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 953 | nullptr)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 954 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 955 | if (error != GR_GL_NO_ERROR) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 959 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 960 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 961 | int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel); |
| 962 | int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel); |
Greg Daniel | 8b059bd | 2017-09-28 20:46:45 +0000 | [diff] [blame] | 963 | const void* currentMipData = texels[currentMipLevel].fPixels; |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 964 | // Even if curremtMipData is nullptr, continue to call TexImage2D. |
| 965 | // This will allocate texture memory which we can later populate. |
| 966 | GL_ALLOC_CALL(&interface, |
| 967 | TexImage2D(target, |
| 968 | currentMipLevel, |
| 969 | internalFormat, |
| 970 | currentWidth, |
| 971 | currentHeight, |
| 972 | 0, // border |
| 973 | externalFormat, externalType, |
| 974 | currentMipData)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 975 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 976 | if (error != GR_GL_NO_ERROR) { |
| 977 | return false; |
| 978 | } |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 979 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 982 | return true; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 986 | * After a texture is created, any state which was altered during its creation |
| 987 | * needs to be restored. |
| 988 | * |
| 989 | * @param interface The GL interface to use. |
| 990 | * @param caps The capabilities of the GL device. |
| 991 | * @param restoreGLRowLength Should the row length unpacking be restored? |
| 992 | * @param glFlipY Did GL flip the texture vertically? |
| 993 | */ |
| 994 | static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps, |
| 995 | bool restoreGLRowLength, bool glFlipY) { |
| 996 | if (restoreGLRowLength) { |
| 997 | SkASSERT(caps.unpackRowLengthSupport()); |
| 998 | GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 999 | } |
| 1000 | if (glFlipY) { |
| 1001 | GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1005 | void GrGLGpu::unbindCpuToGpuXferBuffer() { |
| 1006 | auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType]; |
| 1007 | if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) { |
| 1008 | GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0)); |
| 1009 | xferBufferState.invalidate(); |
| 1010 | } |
| 1011 | |
| 1012 | } |
| 1013 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1014 | // TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to |
| 1015 | // convert from GrColorType to externalFormat/externalType GLenum values. |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1016 | bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, |
| 1017 | GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType, |
| 1018 | int left, int top, int width, int height, GrPixelConfig dataConfig, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1019 | const GrMipLevel texels[], int mipLevelCount, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1020 | GrMipMapsStatus* mipMapsStatus) { |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1021 | SkASSERT(this->caps()->isConfigTexturable(texConfig)); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1022 | SkDEBUGCODE( |
| 1023 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 1024 | SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight); |
| 1025 | SkASSERT(bounds.contains(subRect)); |
| 1026 | ) |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1027 | SkASSERT(1 == mipLevelCount || |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1028 | (0 == left && 0 == top && width == texWidth && height == texHeight)); |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1029 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1030 | this->unbindCpuToGpuXferBuffer(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1031 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1032 | // texels is const. |
| 1033 | // But we may need to flip the texture vertically to prepare it. |
| 1034 | // Rather than flip in place and alter the incoming data, |
| 1035 | // we allocate a new buffer to flip into. |
| 1036 | // 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] | 1037 | SkAutoTMalloc<GrMipLevel> texelsShallowCopy; |
| 1038 | |
| 1039 | if (mipLevelCount) { |
| 1040 | texelsShallowCopy.reset(mipLevelCount); |
| 1041 | memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel)); |
| 1042 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1043 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1044 | const GrGLInterface* interface = this->glInterface(); |
| 1045 | const GrGLCaps& caps = this->glCaps(); |
| 1046 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1047 | size_t bpp = GrBytesPerPixel(dataConfig); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1048 | |
| 1049 | if (width == 0 || height == 0) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 1050 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1051 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1052 | |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1053 | // Internal format comes from the texture desc. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1054 | GrGLenum internalFormat; |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1055 | // External format and type come from the upload data. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1056 | GrGLenum externalFormat; |
| 1057 | GrGLenum externalType; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1058 | if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat, |
| 1059 | &externalType)) { |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1060 | return false; |
| 1061 | } |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 1062 | // TexStorage requires a sized format, and internalFormat may or may not be |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1063 | GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig); |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 1064 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1065 | /* |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1066 | * Check whether to allocate a temporary buffer for flipping y or |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1067 | * because our srcData has extra bytes past each row. If so, we need |
| 1068 | * to trim those off here, since GL ES may not let us specify |
| 1069 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1070 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1071 | bool restoreGLRowLength = false; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 1072 | bool swFlipY = false; |
| 1073 | bool glFlipY = false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1074 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1075 | if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1076 | if (caps.unpackFlipYSupport()) { |
| 1077 | glFlipY = true; |
| 1078 | } else { |
| 1079 | swFlipY = true; |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1080 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | // in case we need a temporary, trimmed copy of the src pixels |
| 1084 | SkAutoSMalloc<128 * 128> tempStorage; |
| 1085 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1086 | if (mipMapsStatus) { |
| 1087 | *mipMapsStatus = GrMipMapsStatus::kValid; |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1088 | } |
| 1089 | |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1090 | const bool usesMips = mipLevelCount > 1; |
| 1091 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1092 | // find the combined size of all the mip levels and the relative offset of |
| 1093 | // each into the collective buffer |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1094 | bool willNeedData = false; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1095 | size_t combinedBufferSize = 0; |
| 1096 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1097 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1098 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 1099 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1100 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1101 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1102 | const size_t trimRowBytes = currentWidth * bpp; |
| 1103 | const size_t trimmedSize = trimRowBytes * currentHeight; |
| 1104 | |
| 1105 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 1106 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 1107 | : trimRowBytes; |
| 1108 | |
| 1109 | |
| 1110 | if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes) || |
| 1111 | swFlipY) { |
| 1112 | willNeedData = true; |
| 1113 | } |
| 1114 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1115 | individualMipOffsets.push_back(combinedBufferSize); |
| 1116 | combinedBufferSize += trimmedSize; |
| 1117 | } else { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1118 | if (mipMapsStatus) { |
| 1119 | *mipMapsStatus = GrMipMapsStatus::kDirty; |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1120 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1121 | individualMipOffsets.push_back(0); |
| 1122 | } |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1123 | } |
| 1124 | if (mipMapsStatus && mipLevelCount <= 1) { |
| 1125 | *mipMapsStatus = GrMipMapsStatus::kNotAllocated; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1126 | } |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1127 | char* buffer = nullptr; |
| 1128 | if (willNeedData) { |
| 1129 | buffer = (char*)tempStorage.reset(combinedBufferSize); |
| 1130 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1131 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1132 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1133 | if (!texelsShallowCopy[currentMipLevel].fPixels) { |
| 1134 | continue; |
| 1135 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1136 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1137 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1138 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
| 1139 | const size_t trimRowBytes = currentWidth * bpp; |
| 1140 | |
| 1141 | /* |
| 1142 | * check whether to allocate a temporary buffer for flipping y or |
| 1143 | * because our srcData has extra bytes past each row. If so, we need |
| 1144 | * to trim those off here, since GL ES may not let us specify |
| 1145 | * GL_UNPACK_ROW_LENGTH. |
| 1146 | */ |
| 1147 | restoreGLRowLength = false; |
| 1148 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1149 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 1150 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 1151 | : trimRowBytes; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1152 | |
ericrk | 154349b | 2016-05-10 14:36:53 -0700 | [diff] [blame] | 1153 | // TODO: This optimization should be enabled with or without mips. |
| 1154 | // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per |
| 1155 | // mip level, before calling glTexImage2D. |
ericrk | 154349b | 2016-05-10 14:36:53 -0700 | [diff] [blame] | 1156 | if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1157 | // can't use this for flipping, only non-neg values allowed. :( |
| 1158 | if (rowBytes != trimRowBytes) { |
| 1159 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1160 | GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1161 | restoreGLRowLength = true; |
| 1162 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1163 | } else if (trimRowBytes != rowBytes || swFlipY) { |
| 1164 | // copy data into our new storage, skipping the trailing bytes |
| 1165 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
| 1166 | if (swFlipY && currentHeight >= 1) { |
| 1167 | src += (currentHeight - 1) * rowBytes; |
Jim Van Verth | f7037d0 | 2017-06-21 10:52:43 -0400 | [diff] [blame] | 1168 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1169 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1170 | for (int y = 0; y < currentHeight; y++) { |
| 1171 | memcpy(dst, src, trimRowBytes); |
| 1172 | if (swFlipY) { |
| 1173 | src -= rowBytes; |
| 1174 | } else { |
| 1175 | src += rowBytes; |
| 1176 | } |
| 1177 | dst += trimRowBytes; |
| 1178 | } |
| 1179 | // now point data to our copied version |
| 1180 | texelsShallowCopy[currentMipLevel].fPixels = buffer + |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1181 | individualMipOffsets[currentMipLevel]; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1182 | texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1183 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1186 | if (mipLevelCount) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1187 | if (glFlipY) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1188 | GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1189 | } |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1190 | 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] | 1191 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1192 | |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1193 | bool succeeded = true; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1194 | if (kNewTexture_UploadType == uploadType) { |
| 1195 | if (0 == left && 0 == top && texWidth == width && texHeight == height) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 1196 | succeeded = allocate_and_populate_texture( |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1197 | texConfig, *interface, caps, target, internalFormat, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1198 | internalFormatForTexStorage, externalFormat, externalType, |
| 1199 | texelsShallowCopy, mipLevelCount, width, height); |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1200 | } else { |
| 1201 | succeeded = false; |
| 1202 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1203 | } else { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 1204 | if (swFlipY || glFlipY) { |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1205 | top = texHeight - (top + height); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1206 | } |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1207 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1208 | if (!texelsShallowCopy[currentMipLevel].fPixels) { |
| 1209 | continue; |
| 1210 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1211 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1212 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1213 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1214 | |
| 1215 | GL_CALL(TexSubImage2D(target, |
| 1216 | currentMipLevel, |
| 1217 | left, top, |
| 1218 | currentWidth, |
| 1219 | currentHeight, |
| 1220 | externalFormat, externalType, |
| 1221 | texelsShallowCopy[currentMipLevel].fPixels)); |
| 1222 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1225 | restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY); |
| 1226 | |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1227 | return succeeded; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1230 | static bool renderbuffer_storage_msaa(const GrGLContext& ctx, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1231 | int sampleCount, |
| 1232 | GrGLenum format, |
| 1233 | int width, int height) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 1234 | CLEAR_ERROR_BEFORE_ALLOC(ctx.interface()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1235 | SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1236 | switch (ctx.caps()->msFBOType()) { |
Brian Salomon | 00731b4 | 2016-10-14 11:30:51 -0400 | [diff] [blame] | 1237 | case GrGLCaps::kStandard_MSFBOType: |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1238 | case GrGLCaps::kMixedSamples_MSFBOType: |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1239 | GL_ALLOC_CALL(ctx.interface(), |
| 1240 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 1241 | sampleCount, |
| 1242 | format, |
| 1243 | width, height)); |
| 1244 | break; |
| 1245 | case GrGLCaps::kES_Apple_MSFBOType: |
| 1246 | GL_ALLOC_CALL(ctx.interface(), |
| 1247 | RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER, |
| 1248 | sampleCount, |
| 1249 | format, |
| 1250 | width, height)); |
| 1251 | break; |
| 1252 | case GrGLCaps::kES_EXT_MsToTexture_MSFBOType: |
| 1253 | case GrGLCaps::kES_IMG_MsToTexture_MSFBOType: |
| 1254 | GL_ALLOC_CALL(ctx.interface(), |
| 1255 | RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER, |
| 1256 | sampleCount, |
| 1257 | format, |
| 1258 | width, height)); |
| 1259 | break; |
| 1260 | case GrGLCaps::kNone_MSFBOType: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 1261 | 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] | 1262 | break; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1263 | } |
Brian Salomon | 9251d4e | 2015-03-17 16:03:19 -0400 | [diff] [blame] | 1264 | return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface())); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1267 | bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1268 | const GrGLTextureInfo& texInfo, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1269 | GrGLRenderTarget::IDDesc* idDesc) { |
| 1270 | idDesc->fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1271 | idDesc->fRTFBOID = 0; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1272 | idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1273 | idDesc->fTexFBOID = 0; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1274 | SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) == |
| 1275 | this->caps()->usesMixedSamples()); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1276 | idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1277 | |
| 1278 | GrGLenum status; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1279 | |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1280 | GrGLenum colorRenderbufferFormat = 0; // suppress warning |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 1281 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1282 | if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 1283 | goto FAILED; |
| 1284 | } |
| 1285 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1286 | GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID)); |
| 1287 | if (!idDesc->fTexFBOID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1288 | goto FAILED; |
| 1289 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1290 | |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1291 | // If we are using multisampling we will create two FBOS. We render to one and then resolve to |
| 1292 | // the texture bound to the other. The exception is the IMG multisample extension. With this |
| 1293 | // extension the texture is multisampled when rendered to and then auto-resolves it when it is |
| 1294 | // rendered from. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1295 | if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1296 | GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID)); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1297 | GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1298 | if (!idDesc->fRTFBOID || |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1299 | !idDesc->fMSColorRenderbufferID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1300 | goto FAILED; |
| 1301 | } |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1302 | if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) { |
| 1303 | return false; |
| 1304 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1305 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1306 | idDesc->fRTFBOID = idDesc->fTexFBOID; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1309 | // below here we may bind the FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1310 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1311 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1312 | SkASSERT(desc.fSampleCnt > 1); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1313 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID)); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1314 | if (!renderbuffer_storage_msaa(*fGLContext, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1315 | desc.fSampleCnt, |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1316 | colorRenderbufferFormat, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1317 | desc.fWidth, desc.fHeight)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1318 | goto FAILED; |
| 1319 | } |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1320 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1321 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1322 | GR_GL_COLOR_ATTACHMENT0, |
| 1323 | GR_GL_RENDERBUFFER, |
| 1324 | idDesc->fMSColorRenderbufferID)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1325 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1326 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1327 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1328 | goto FAILED; |
| 1329 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1330 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1333 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1334 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1335 | if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1336 | GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1337 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1338 | texInfo.fTarget, |
| 1339 | texInfo.fID, 0, desc.fSampleCnt)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1340 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1341 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1342 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1343 | texInfo.fTarget, |
| 1344 | texInfo.fID, 0)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1345 | } |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1346 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1347 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1348 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1349 | goto FAILED; |
| 1350 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1351 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | return true; |
| 1355 | |
| 1356 | FAILED: |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1357 | if (idDesc->fMSColorRenderbufferID) { |
| 1358 | GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1359 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1360 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1361 | this->deleteFramebuffer(idDesc->fRTFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1362 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1363 | if (idDesc->fTexFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1364 | this->deleteFramebuffer(idDesc->fTexFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1365 | } |
| 1366 | return false; |
| 1367 | } |
| 1368 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1369 | // good to set a break-point here to know when createTexture fails |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1370 | static sk_sp<GrTexture> return_null_texture() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1371 | // SkDEBUGFAIL("null texture"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1372 | return nullptr; |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 1375 | #if 0 && defined(SK_DEBUG) |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1376 | static size_t as_size_t(int x) { |
| 1377 | return x; |
| 1378 | } |
| 1379 | #endif |
| 1380 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1381 | static void set_initial_texture_params(const GrGLInterface* interface, |
| 1382 | const GrGLTextureInfo& info, |
| 1383 | GrGLTexture::TexParams* initialTexParams) { |
| 1384 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1385 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1386 | // texture that is not mipmap complete (considering the filter in use). |
| 1387 | // we only set a subset here so invalidate first |
| 1388 | initialTexParams->invalidate(); |
| 1389 | initialTexParams->fMinFilter = GR_GL_NEAREST; |
| 1390 | initialTexParams->fMagFilter = GR_GL_NEAREST; |
| 1391 | initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1392 | initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE; |
| 1393 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1394 | GR_GL_TEXTURE_MAG_FILTER, |
| 1395 | initialTexParams->fMagFilter)); |
| 1396 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1397 | GR_GL_TEXTURE_MIN_FILTER, |
| 1398 | initialTexParams->fMinFilter)); |
| 1399 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1400 | GR_GL_TEXTURE_WRAP_S, |
| 1401 | initialTexParams->fWrapS)); |
| 1402 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1403 | GR_GL_TEXTURE_WRAP_T, |
| 1404 | initialTexParams->fWrapT)); |
| 1405 | } |
| 1406 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1407 | sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc, |
| 1408 | SkBudgeted budgeted, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1409 | const GrMipLevel texels[], |
| 1410 | int mipLevelCount) { |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1411 | // We fail if the MSAA was requested and is not available. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1412 | if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1413 | //SkDebugf("MSAA RT requested but not supported on this platform."); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1414 | return return_null_texture(); |
| 1415 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1416 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1417 | bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1418 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1419 | GrMipLevel zeroLevel; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1420 | std::unique_ptr<uint8_t[]> zeros; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1421 | if (performClear && !this->glCaps().clearTextureSupport() && |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 1422 | !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1423 | size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth; |
| 1424 | size_t size = rowSize * desc.fHeight; |
| 1425 | zeros.reset(new uint8_t[size]); |
| 1426 | memset(zeros.get(), 0, size); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1427 | zeroLevel.fPixels = zeros.get(); |
| 1428 | zeroLevel.fRowBytes = 0; |
| 1429 | texels = &zeroLevel; |
| 1430 | mipLevelCount = 1; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1431 | performClear = false; |
| 1432 | } |
| 1433 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 1434 | bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1435 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1436 | GrGLTexture::IDDesc idDesc; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1437 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1438 | GrMipMapsStatus mipMapsStatus; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1439 | GrGLTexture::TexParams initialTexParams; |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1440 | if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels, |
| 1441 | mipLevelCount, &mipMapsStatus)) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1442 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1443 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1444 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1445 | sk_sp<GrGLTexture> tex; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 1446 | if (isRenderTarget) { |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1447 | // 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] | 1448 | GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1449 | GrGLRenderTarget::IDDesc rtIDDesc; |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1450 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1451 | if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1452 | GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1453 | return return_null_texture(); |
| 1454 | } |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1455 | tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1456 | mipMapsStatus); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 1457 | tex->baseLevelWasBoundToFBO(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1458 | } else { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1459 | tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1460 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1461 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1462 | #ifdef TRACE_TEXTURE_CREATION |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1463 | SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n", |
brianosman | c4459f6 | 2016-07-19 08:02:20 -0700 | [diff] [blame] | 1464 | idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1465 | #endif |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1466 | if (tex && performClear) { |
| 1467 | if (this->glCaps().clearTextureSupport()) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1468 | static constexpr uint32_t kZero = 0; |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 1469 | 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] | 1470 | } else { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1471 | GrGLIRect viewport; |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1472 | this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport, |
| 1473 | kDst_TempFBOTarget); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1474 | this->disableScissor(); |
| 1475 | this->disableWindowRectangles(); |
| 1476 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 1477 | fHWWriteToColor = kYes_TriState; |
| 1478 | GL_CALL(ClearColor(0, 0, 0, 0)); |
| 1479 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1480 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get()); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1481 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
| 1482 | } |
| 1483 | } |
Kevin Lubick | f7621cb | 2018-04-16 15:51:44 -0400 | [diff] [blame] | 1484 | return std::move(tex); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | namespace { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1488 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1489 | const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1490 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1491 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1492 | GrGLStencilAttachment::Format* format) { |
sugoi@google.com | 6ba0b0f | 2013-05-03 13:52:32 +0000 | [diff] [blame] | 1493 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1494 | // we shouldn't ever know one size and not the other |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1495 | SkASSERT((kUnknownBitCount == format->fStencilBits) == |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1496 | (kUnknownBitCount == format->fTotalBits)); |
| 1497 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1498 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1499 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1500 | (GrGLint*)&format->fStencilBits); |
| 1501 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1502 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1503 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1504 | (GrGLint*)&format->fTotalBits); |
| 1505 | format->fTotalBits += format->fStencilBits; |
| 1506 | } else { |
| 1507 | format->fTotalBits = format->fStencilBits; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1513 | int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) { |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1514 | static const int kSize = 16; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1515 | SkASSERT(this->caps()->isConfigRenderable(config)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1516 | if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) { |
| 1517 | // Default to unsupported, set this if we find a stencil format that works. |
| 1518 | int firstWorkingStencilFormatIndex = -1; |
Brian Osman | 91f9a2c | 2017-09-05 15:02:46 -0400 | [diff] [blame] | 1519 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1520 | // Create color texture |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1521 | GrGLuint colorID = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1522 | GL_CALL(GenTextures(1, &colorID)); |
| 1523 | this->setScratchTextureUnit(); |
| 1524 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID)); |
| 1525 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1526 | GR_GL_TEXTURE_MAG_FILTER, |
| 1527 | GR_GL_NEAREST)); |
| 1528 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1529 | GR_GL_TEXTURE_MIN_FILTER, |
| 1530 | GR_GL_NEAREST)); |
| 1531 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1532 | GR_GL_TEXTURE_WRAP_S, |
| 1533 | GR_GL_CLAMP_TO_EDGE)); |
| 1534 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1535 | GR_GL_TEXTURE_WRAP_T, |
| 1536 | GR_GL_CLAMP_TO_EDGE)); |
| 1537 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1538 | GrGLenum internalFormat; |
| 1539 | GrGLenum externalFormat; |
| 1540 | GrGLenum externalType; |
| 1541 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 1542 | &externalType)) { |
| 1543 | return false; |
| 1544 | } |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1545 | this->unbindCpuToGpuXferBuffer(); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1546 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1547 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, |
bsalomon | 926cb02 | 2015-12-17 18:15:11 -0800 | [diff] [blame] | 1548 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1549 | internalFormat, |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1550 | kSize, |
| 1551 | kSize, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1552 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1553 | externalFormat, |
| 1554 | externalType, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1555 | nullptr)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1556 | if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1557 | GL_CALL(DeleteTextures(1, &colorID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1558 | return -1; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | // unbind the texture from the texture unit before binding it to the frame buffer |
| 1562 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 1563 | |
| 1564 | // Create Framebuffer |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1565 | GrGLuint fb = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1566 | GL_CALL(GenFramebuffers(1, &fb)); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1567 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1568 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1569 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 1570 | GR_GL_COLOR_ATTACHMENT0, |
| 1571 | GR_GL_TEXTURE_2D, |
| 1572 | colorID, |
| 1573 | 0)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1574 | GrGLuint sbRBID = 0; |
| 1575 | GL_CALL(GenRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1576 | |
| 1577 | // look over formats till I find a compatible one |
| 1578 | int stencilFmtCnt = this->glCaps().stencilFormats().count(); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1579 | if (sbRBID) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1580 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1581 | for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) { |
| 1582 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i]; |
| 1583 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1584 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1585 | sFmt.fInternalFormat, |
| 1586 | kSize, kSize)); |
| 1587 | if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1588 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1589 | GR_GL_STENCIL_ATTACHMENT, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1590 | GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1591 | if (sFmt.fPacked) { |
| 1592 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1593 | GR_GL_DEPTH_ATTACHMENT, |
| 1594 | GR_GL_RENDERBUFFER, sbRBID)); |
| 1595 | } else { |
| 1596 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1597 | GR_GL_DEPTH_ATTACHMENT, |
| 1598 | GR_GL_RENDERBUFFER, 0)); |
| 1599 | } |
| 1600 | GrGLenum status; |
| 1601 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1602 | if (status == GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1603 | firstWorkingStencilFormatIndex = i; |
| 1604 | break; |
| 1605 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1606 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1607 | GR_GL_STENCIL_ATTACHMENT, |
| 1608 | GR_GL_RENDERBUFFER, 0)); |
| 1609 | if (sFmt.fPacked) { |
| 1610 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1611 | GR_GL_DEPTH_ATTACHMENT, |
| 1612 | GR_GL_RENDERBUFFER, 0)); |
| 1613 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1614 | } |
| 1615 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1616 | GL_CALL(DeleteRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1617 | } |
| 1618 | GL_CALL(DeleteTextures(1, &colorID)); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1619 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0); |
| 1620 | this->deleteFramebuffer(fb); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1621 | fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1622 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1623 | return this->glCaps().getStencilFormatIndexForConfig(config); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1626 | bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1627 | GrGLTexture::TexParams* initialTexParams, const GrMipLevel texels[], |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1628 | int mipLevelCount, GrMipMapsStatus* mipMapsStatus) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1629 | info->fID = 0; |
| 1630 | info->fTarget = GR_GL_TEXTURE_2D; |
| 1631 | GL_CALL(GenTextures(1, &(info->fID))); |
| 1632 | |
| 1633 | if (!info->fID) { |
| 1634 | return false; |
| 1635 | } |
| 1636 | |
| 1637 | this->setScratchTextureUnit(); |
| 1638 | GL_CALL(BindTexture(info->fTarget, info->fID)); |
| 1639 | |
| 1640 | if (renderTarget && this->glCaps().textureUsageSupport()) { |
| 1641 | // provides a hint about how this texture will be used |
| 1642 | GL_CALL(TexParameteri(info->fTarget, |
| 1643 | GR_GL_TEXTURE_USAGE, |
| 1644 | GR_GL_FRAMEBUFFER_ATTACHMENT)); |
| 1645 | } |
| 1646 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1647 | if (info) { |
| 1648 | set_initial_texture_params(this->glInterface(), *info, initialTexParams); |
| 1649 | } |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1650 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1651 | if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, kTopLeft_GrSurfaceOrigin, |
| 1652 | info->fTarget, kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, |
| 1653 | desc.fConfig, texels, mipLevelCount, mipMapsStatus)) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1654 | GL_CALL(DeleteTextures(1, &(info->fID))); |
| 1655 | return false; |
| 1656 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 1657 | info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig); |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1658 | return true; |
| 1659 | } |
| 1660 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1661 | GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1662 | int width, int height) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1663 | SkASSERT(width >= rt->width()); |
| 1664 | SkASSERT(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1665 | |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1666 | int samples = rt->numStencilSamples(); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1667 | GrGLStencilAttachment::IDDesc sbDesc; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1668 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1669 | int sIdx = this->getCompatibleStencilIndex(rt->config()); |
bsalomon | 62a627b | 2015-12-17 09:50:47 -0800 | [diff] [blame] | 1670 | if (sIdx < 0) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1671 | return nullptr; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1672 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1673 | |
| 1674 | if (!sbDesc.fRenderbufferID) { |
| 1675 | GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID)); |
| 1676 | } |
| 1677 | if (!sbDesc.fRenderbufferID) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1678 | return nullptr; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1679 | } |
| 1680 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID)); |
| 1681 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx]; |
| 1682 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1683 | // we do this "if" so that we don't call the multisample |
| 1684 | // version on a GL that doesn't have an MSAA extension. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1685 | if (samples > 1) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1686 | SkAssertResult(renderbuffer_storage_msaa(*fGLContext, |
| 1687 | samples, |
| 1688 | sFmt.fInternalFormat, |
| 1689 | width, height)); |
| 1690 | } else { |
| 1691 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1692 | sFmt.fInternalFormat, |
| 1693 | width, height)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1694 | SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1695 | } |
| 1696 | fStats.incStencilAttachmentCreates(); |
| 1697 | // After sized formats we attempt an unsized format and take |
| 1698 | // whatever sizes GL gives us. In that case we query for the size. |
| 1699 | GrGLStencilAttachment::Format format = sFmt; |
| 1700 | get_stencil_rb_sizes(this->glInterface(), &format); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1701 | GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this, |
| 1702 | sbDesc, |
| 1703 | width, |
| 1704 | height, |
| 1705 | samples, |
| 1706 | format); |
| 1707 | return stencil; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1710 | //////////////////////////////////////////////////////////////////////////////// |
| 1711 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1712 | // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer |
| 1713 | // objects are implemented as client-side-arrays on tile-deferred architectures. |
| 1714 | #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW |
| 1715 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1716 | GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 1717 | GrAccessPattern accessPattern, const void* data) { |
| 1718 | return GrGLBuffer::Create(this, size, intendedType, accessPattern, data); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1719 | } |
| 1720 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 1721 | void GrGLGpu::flushScissor(const GrScissorState& scissorState, |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1722 | const GrGLIRect& rtViewport, |
| 1723 | GrSurfaceOrigin rtOrigin) { |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 1724 | if (scissorState.enabled()) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1725 | GrGLIRect scissor; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1726 | scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1727 | // if the scissor fully contains the viewport then we fall through and |
| 1728 | // disable the scissor test. |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1729 | if (!scissor.contains(rtViewport)) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1730 | if (fHWScissorSettings.fRect != scissor) { |
| 1731 | scissor.pushToGLScissor(this->glInterface()); |
| 1732 | fHWScissorSettings.fRect = scissor; |
| 1733 | } |
| 1734 | if (kYes_TriState != fHWScissorSettings.fEnabled) { |
| 1735 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1736 | fHWScissorSettings.fEnabled = kYes_TriState; |
| 1737 | } |
| 1738 | return; |
| 1739 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1740 | } |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1741 | |
| 1742 | // See fall through note above |
| 1743 | this->disableScissor(); |
| 1744 | } |
| 1745 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1746 | void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1747 | const GrGLRenderTarget* rt, GrSurfaceOrigin origin) { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1748 | #ifndef USE_NSIGHT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1749 | typedef GrWindowRectsState::Mode Mode; |
| 1750 | SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen. |
| 1751 | SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1752 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1753 | if (!this->caps()->maxWindowRectangles() || |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1754 | fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1755 | return; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1758 | // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above |
| 1759 | // 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] | 1760 | int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows)); |
| 1761 | SkASSERT(windowState.numWindows() == numWindows); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1762 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1763 | GrGLIRect glwindows[GrWindowRectangles::kMaxWindows]; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1764 | const SkIRect* skwindows = windowState.windows().data(); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1765 | for (int i = 0; i < numWindows; ++i) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1766 | glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1769 | GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE; |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1770 | GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts())); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1771 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1772 | fHWWindowRectsState.set(origin, rt->getViewport(), windowState); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1773 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | void GrGLGpu::disableWindowRectangles() { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1777 | #ifndef USE_NSIGHT |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1778 | if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1779 | return; |
| 1780 | } |
| 1781 | GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1782 | fHWWindowRectsState.setDisabled(); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1783 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 1786 | void GrGLGpu::flushMinSampleShading(float minSampleShading) { |
| 1787 | if (fHWMinSampleShading != minSampleShading) { |
| 1788 | if (minSampleShading > 0.0) { |
| 1789 | GL_CALL(Enable(GR_GL_SAMPLE_SHADING)); |
| 1790 | GL_CALL(MinSampleShading(minSampleShading)); |
| 1791 | } |
| 1792 | else { |
| 1793 | GL_CALL(Disable(GR_GL_SAMPLE_SHADING)); |
| 1794 | } |
| 1795 | fHWMinSampleShading = minSampleShading; |
| 1796 | } |
| 1797 | } |
| 1798 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 1799 | bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc, |
| 1800 | bool willDrawPoints) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 1801 | sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 1802 | if (!program) { |
| 1803 | GrCapsDebugf(this->caps(), "Failed to create program!\n"); |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
| 1807 | program->generateMipmaps(primProc, pipeline); |
| 1808 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1809 | GrXferProcessor::BlendInfo blendInfo; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1810 | pipeline.getXferProcessor().getBlendInfo(&blendInfo); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1811 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1812 | this->flushColorWrite(blendInfo.fWriteColor); |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 1813 | this->flushMinSampleShading(primProc.getSampleShading()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1814 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 1815 | GrGLuint programID = program->programID(); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1816 | if (fHWProgramID != programID) { |
| 1817 | GL_CALL(UseProgram(programID)); |
| 1818 | fHWProgramID = programID; |
| 1819 | } |
| 1820 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1821 | if (blendInfo.fWriteColor) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1822 | // Swizzle the blend to match what the shader will output. |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1823 | const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle( |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 1824 | pipeline.proxy()->config()); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1825 | this->flushBlend(blendInfo, swizzle); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1826 | } |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1827 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1828 | program->setData(primProc, pipeline); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1829 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 1830 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 1831 | GrStencilSettings stencil; |
| 1832 | if (pipeline.isStencilEnabled()) { |
| 1833 | // TODO: attach stencil and create settings during render target flush. |
| 1834 | SkASSERT(glRT->renderTargetPriv().getStencilAttachment()); |
| 1835 | stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), |
| 1836 | glRT->renderTargetPriv().numStencilBits()); |
| 1837 | } |
| 1838 | this->flushStencil(stencil); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1839 | this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin()); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1840 | this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin()); |
csmartdalton | dabc91b | 2016-11-15 14:26:27 -0700 | [diff] [blame] | 1841 | this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1842 | |
| 1843 | // This must come after textures are flushed because a texture may need |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1844 | // to be msaa-resolved (which will modify bound FBO state). |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1845 | this->flushRenderTarget(glRT, pipeline.getDisableOutputConversionToSRGB()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1846 | |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1850 | void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc, |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1851 | const GrBuffer* indexBuffer, |
| 1852 | const GrBuffer* vertexBuffer, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1853 | int baseVertex, |
| 1854 | const GrBuffer* instanceBuffer, |
| 1855 | int baseInstance) { |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 1856 | using EnablePrimitiveRestart = GrGLAttribArrayState::EnablePrimitiveRestart; |
| 1857 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1858 | GrGLAttribArrayState* attribState; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1859 | if (indexBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1860 | SkASSERT(indexBuffer && !indexBuffer->isMapped()); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1861 | attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1862 | } else { |
| 1863 | attribState = fHWVertexArrayState.bindInternalVertexArray(this); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1864 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1865 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1866 | struct { |
| 1867 | const GrBuffer* fBuffer; |
| 1868 | int fStride; |
| 1869 | size_t fBufferOffset; |
| 1870 | } bindings[2]; |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 1871 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1872 | if (int vertexStride = primProc.getVertexStride()) { |
| 1873 | SkASSERT(vertexBuffer && !vertexBuffer->isMapped()); |
| 1874 | bindings[0].fBuffer = vertexBuffer; |
| 1875 | bindings[0].fStride = vertexStride; |
| 1876 | bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride; |
| 1877 | } |
| 1878 | if (int instanceStride = primProc.getInstanceStride()) { |
| 1879 | SkASSERT(instanceBuffer && !instanceBuffer->isMapped()); |
| 1880 | bindings[1].fBuffer = instanceBuffer; |
| 1881 | bindings[1].fStride = instanceStride; |
| 1882 | bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride; |
| 1883 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1884 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1885 | int numAttribs = primProc.numAttribs(); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 1886 | auto enableRestart = EnablePrimitiveRestart(primProc.willUsePrimitiveRestart() && indexBuffer); |
| 1887 | attribState->enableVertexArrays(this, numAttribs, enableRestart); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1888 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1889 | for (int i = 0; i < numAttribs; ++i) { |
| 1890 | using InputRate = GrPrimitiveProcessor::Attribute::InputRate; |
| 1891 | const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i); |
| 1892 | const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0; |
| 1893 | const auto& binding = bindings[divisor]; |
| 1894 | attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride, |
| 1895 | binding.fBufferOffset + attrib.fOffsetInRecord, divisor); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1899 | GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1900 | this->handleDirtyContext(); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1901 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1902 | // Index buffer state is tied to the vertex array. |
| 1903 | if (kIndex_GrBufferType == type) { |
| 1904 | this->bindVertexArray(0); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1905 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1906 | |
| 1907 | SkASSERT(type >= 0 && type <= kLast_GrBufferType); |
| 1908 | auto& bufferState = fHWBufferState[type]; |
| 1909 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 1910 | if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) { |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1911 | if (buffer->isCPUBacked()) { |
| 1912 | if (!bufferState.fBufferZeroKnownBound) { |
| 1913 | GL_CALL(BindBuffer(bufferState.fGLTarget, 0)); |
| 1914 | } |
| 1915 | } else { |
| 1916 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer); |
| 1917 | GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID())); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1918 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1919 | bufferState.fBufferZeroKnownBound = buffer->isCPUBacked(); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 1920 | bufferState.fBoundBufferUniqueID = buffer->uniqueID(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | return bufferState.fGLTarget; |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1926 | void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) { |
| 1927 | if (buffer->hasAttachedToTexture()) { |
| 1928 | // Detach this buffer from any textures to ensure the underlying memory is freed. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1929 | GrGpuResource::UniqueID uniqueID = buffer->uniqueID(); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1930 | for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) { |
| 1931 | auto& buffTex = fHWBufferTextures[i]; |
| 1932 | if (uniqueID != buffTex.fAttachedBufferUniqueID) { |
| 1933 | continue; |
| 1934 | } |
| 1935 | if (i == fHWMaxUsedBufferTextureUnit) { |
| 1936 | --fHWMaxUsedBufferTextureUnit; |
| 1937 | } |
| 1938 | |
| 1939 | this->setTextureUnit(i); |
| 1940 | if (!buffTex.fKnownBound) { |
| 1941 | SkASSERT(buffTex.fTextureID); |
| 1942 | GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID)); |
| 1943 | buffTex.fKnownBound = true; |
| 1944 | } |
| 1945 | GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER, |
| 1946 | this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0)); |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1951 | void GrGLGpu::disableScissor() { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1952 | if (kNo_TriState != fHWScissorSettings.fEnabled) { |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1953 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1954 | fHWScissorSettings.fEnabled = kNo_TriState; |
| 1955 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 1959 | void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, |
| 1960 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1961 | // parent class should never let us get here with no RT |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1962 | SkASSERT(target); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1963 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1964 | this->handleDirtyContext(); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1965 | |
| 1966 | GrGLfloat r, g, b, a; |
| 1967 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 1968 | a = GrColorUnpackA(color) * scale255; |
| 1969 | GrGLfloat scaleRGB = scale255; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1970 | r = GrColorUnpackR(color) * scaleRGB; |
| 1971 | g = GrColorUnpackG(color) * scaleRGB; |
| 1972 | b = GrColorUnpackB(color) * scaleRGB; |
| 1973 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1974 | if (this->glCaps().useDrawToClearColor()) { |
| 1975 | this->clearColorAsDraw(clip, r, g, b, a, target, origin); |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
| 1980 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1981 | if (clip.scissorEnabled()) { |
| 1982 | this->flushRenderTarget(glRT, origin, clip.scissorRect()); |
| 1983 | } else { |
| 1984 | this->flushRenderTarget(glRT); |
| 1985 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1986 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
| 1987 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
| 1988 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1989 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 1990 | fHWWriteToColor = kYes_TriState; |
Brian Salomon | 028a9a5 | 2017-05-11 11:39:08 -0400 | [diff] [blame] | 1991 | |
Eric Karl | aeaf22b | 2017-05-18 15:08:09 -0700 | [diff] [blame] | 1992 | if (this->glCaps().clearToBoundaryValuesIsBroken() && |
| 1993 | (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] | 1994 | static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f); |
| 1995 | static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f); |
Eric Karl | aeaf22b | 2017-05-18 15:08:09 -0700 | [diff] [blame] | 1996 | a = (1 == a) ? safeAlpha1 : safeAlpha0; |
Brian Salomon | 028a9a5 | 2017-05-11 11:39:08 -0400 | [diff] [blame] | 1997 | } |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1998 | GL_CALL(ClearColor(r, g, b, a)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1999 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2002 | void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) { |
| 2003 | if (!target) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 2006 | |
| 2007 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
| 2008 | // this should only be called internally when we know we have a |
| 2009 | // stencil buffer. |
| 2010 | SkASSERT(sb); |
| 2011 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 2012 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2013 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2014 | |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 2015 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2016 | this->disableWindowRectangles(); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 2017 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2018 | GL_CALL(StencilMask(0xffffffff)); |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2019 | GL_CALL(ClearStencil(clearValue)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2020 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 2021 | fHWStencilSettings.invalidate(); |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 2022 | if (!clearValue) { |
| 2023 | sb->cleared(); |
| 2024 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 2027 | void GrGLGpu::clearStencilClip(const GrFixedClip& clip, |
| 2028 | bool insideStencilMask, |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 2029 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2030 | SkASSERT(target); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2031 | this->handleDirtyContext(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 2032 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 2033 | if (this->glCaps().useDrawToClearStencilClip()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2034 | this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 2035 | return; |
| 2036 | } |
| 2037 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 2038 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 2039 | // this should only be called internally when we know we have a |
| 2040 | // stencil buffer. |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 2041 | SkASSERT(sb); |
| 2042 | GrGLint stencilBitCount = sb->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2043 | #if 0 |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2044 | SkASSERT(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2045 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2046 | #else |
| 2047 | // 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] | 2048 | // ANGLE a partial stencil mask will cause clears to be |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 2049 | // turned into draws. Our contract on GrOpList says that |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2050 | // changing the clip between stencil passes may or may not |
| 2051 | // 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] | 2052 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2053 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2054 | GrGLint value; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 2055 | if (insideStencilMask) { |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2056 | value = (1 << (stencilBitCount - 1)); |
| 2057 | } else { |
| 2058 | value = 0; |
| 2059 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 2060 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2061 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2062 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 2063 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2064 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2065 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 2066 | GL_CALL(StencilMask((uint32_t) clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2067 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2068 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 2069 | fHWStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2072 | static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps, |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2073 | int width, int height, GrPixelConfig config, |
| 2074 | size_t rowBytes) { |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2075 | // If the surface is already TopLeft, we don't need to flip. |
| 2076 | if (kTopLeft_GrSurfaceOrigin == origin) { |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2077 | return false; |
| 2078 | } |
| 2079 | |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 2080 | // If the read is really small or smaller than the min texture size, don't force a draw. |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 2081 | static const int kMinSize = 32; |
| 2082 | if (width < kMinSize || height < kMinSize) { |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 2083 | return false; |
| 2084 | } |
| 2085 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2086 | // if GL can do the flip then we'll never pay for it. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2087 | if (caps.packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | // If we have to do memcpy to handle non-trim rowBytes then we |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 2092 | // get the flip for free. Otherwise it costs. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2093 | // Note that we're assuming that 0 rowBytes has already been handled and that the width has been |
| 2094 | // clipped. |
| 2095 | return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes; |
| 2096 | } |
| 2097 | |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2098 | bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) { |
Brian Salomon | 625cd9e | 2016-12-15 09:35:19 -0500 | [diff] [blame] | 2099 | #ifdef SK_BUILD_FOR_MAC |
| 2100 | // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's |
| 2101 | // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer |
| 2102 | // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium |
| 2103 | // we always use a temporary surface to test for read pixels support. |
| 2104 | // https://www.crbug.com/662802 |
| 2105 | if (this->glContext().driver() == kChromium_GrGLDriver) { |
| 2106 | return this->readPixelsSupported(target->config(), readConfig); |
| 2107 | } |
| 2108 | #endif |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2109 | auto bindRenderTarget = [this, target]() -> bool { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2110 | this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target)); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2111 | return true; |
| 2112 | }; |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2113 | auto unbindRenderTarget = []{}; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2114 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 2115 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 2116 | }; |
| 2117 | GrPixelConfig rtConfig = target->config(); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2118 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget, |
| 2119 | unbindRenderTarget); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) { |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2123 | sk_sp<GrTexture> temp; |
| 2124 | auto bindRenderTarget = [this, rtConfig, &temp]() -> bool { |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 2125 | GrSurfaceDesc desc; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2126 | desc.fConfig = rtConfig; |
| 2127 | desc.fWidth = desc.fHeight = 16; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 2128 | if (this->glCaps().isConfigRenderable(rtConfig)) { |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2129 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 2130 | temp = this->createTexture(desc, SkBudgeted::kNo); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2131 | if (!temp) { |
| 2132 | return false; |
| 2133 | } |
| 2134 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2135 | this->flushRenderTargetNoColorWrites(glrt); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2136 | return true; |
| 2137 | } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 2138 | temp = this->createTexture(desc, SkBudgeted::kNo); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2139 | if (!temp) { |
| 2140 | return false; |
| 2141 | } |
| 2142 | GrGLIRect vp; |
| 2143 | this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2144 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2145 | return true; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2146 | } |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2147 | return false; |
| 2148 | }; |
| 2149 | auto unbindRenderTarget = [this, &temp]() { |
| 2150 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get()); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2151 | }; |
| 2152 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 2153 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 2154 | }; |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2155 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget, |
| 2156 | unbindRenderTarget); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) { |
| 2160 | if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) { |
| 2161 | return this->readPixelsSupported(rt, readConfig); |
| 2162 | } else { |
| 2163 | GrPixelConfig config = surfaceForConfig->config(); |
| 2164 | return this->readPixelsSupported(config, readConfig); |
| 2165 | } |
| 2166 | } |
| 2167 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2168 | bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2169 | int height, size_t rowBytes, GrColorType dstColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2170 | DrawPreference* drawPreference, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2171 | ReadPixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2172 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 2173 | auto dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(srcSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2174 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 2175 | // We assume the base class has only inserted a draw for sRGB reasons. So the |
| 2176 | // the temp surface has the config of the dst data. There is no swizzling, nor dst config |
| 2177 | // spoofing. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2178 | SkASSERT(tempDrawInfo->fReadColorType == dstColorType); |
| 2179 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == dstColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2180 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2181 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 2182 | dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
| 2183 | } |
| 2184 | if (GrColorTypeIsAlphaOnly(dstColorType)) { |
| 2185 | dstConfigSRGBEncoded = GrSRGBEncoded::kNo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2186 | } |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2187 | GrPixelConfig srcConfig = srcSurface->config(); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2188 | |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 2189 | tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact |
| 2190 | : SkBackingFit::kApprox; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2191 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2192 | // TODO: Update this logic to use color type. |
| 2193 | auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, dstConfigSRGBEncoded); |
| 2194 | |
| 2195 | if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == dstAsConfig && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2196 | this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2197 | tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2198 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2199 | tempDrawInfo->fReadColorType = GrColorType::kBGRA_8888; |
bsalomon | b411b3b | 2015-07-31 09:34:24 -0700 | [diff] [blame] | 2200 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
ericrk | b4ecabd | 2016-03-11 15:18:20 -0800 | [diff] [blame] | 2201 | } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2202 | GrBytesPerPixel(dstAsConfig) == 4 && |
| 2203 | GrPixelConfigSwapRAndB(dstAsConfig) == srcConfig && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2204 | this->readPixelsSupported(srcSurface, srcConfig)) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2205 | // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2206 | // Better to do a draw with a R/B swap and then read as the original config. |
| 2207 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2208 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2209 | tempDrawInfo->fReadColorType = GrPixelConfigToColorType(srcConfig); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2210 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2211 | } else if (!this->readPixelsSupported(srcSurface, dstAsConfig)) { |
| 2212 | if (kBGRA_8888_GrPixelConfig == dstAsConfig && |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2213 | this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2214 | this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2215 | // We're trying to read BGRA but it's not supported. If RGBA is renderable and |
| 2216 | // we can read it back, then do a swizzling draw to a RGBA and read it back (which |
| 2217 | // will effectively be BGRA). |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2218 | tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 2219 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2220 | tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2221 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2222 | } else if (kSBGRA_8888_GrPixelConfig == dstAsConfig && |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2223 | this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) && |
| 2224 | this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, |
| 2225 | kSRGBA_8888_GrPixelConfig)) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 2226 | // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and |
| 2227 | // we can read it back, then do a swizzling draw to a sRGBA and read it back (which |
| 2228 | // will effectively be sBGRA). |
| 2229 | tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig; |
| 2230 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2231 | tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 2232 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2233 | } else if (kAlpha_8_GrPixelConfig == dstAsConfig) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2234 | // onReadPixels implements a fallback for cases where we want to read kAlpha_8, |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2235 | // it's unsupported, but 32bit RGBA reads are supported. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2236 | if (!this->readPixelsSupported(srcSurface, kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2237 | // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888) |
| 2238 | // first and then onReadPixels will read that to a 32bit temporary buffer. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2239 | if (this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2240 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2241 | tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2242 | tempDrawInfo->fReadColorType = GrColorType::kAlpha_8; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2243 | } else { |
| 2244 | return false; |
| 2245 | } |
| 2246 | } else { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2247 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2248 | SkASSERT(tempDrawInfo->fReadColorType == GrColorType::kAlpha_8); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2249 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2250 | } else if (kRGBA_half_GrPixelConfig == dstAsConfig && |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2251 | this->readPixelsSupported(srcSurface, kRGBA_float_GrPixelConfig)) { |
| 2252 | // If reading in half float format is not supported, then read in float format. |
| 2253 | return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2254 | } else if (this->glCaps().canConfigBeFBOColorAttachment(dstAsConfig) && |
| 2255 | this->readPixelsSupported(dstAsConfig, dstAsConfig)) { |
bsalomon | e6d665e | 2016-03-10 07:22:25 -0800 | [diff] [blame] | 2256 | // Do a draw to convert from the src config to the read config. |
| 2257 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2258 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstAsConfig; |
| 2259 | tempDrawInfo->fReadColorType = dstColorType; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2260 | } else { |
| 2261 | return false; |
| 2262 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2265 | if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2266 | read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, dstAsConfig, |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2267 | rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2268 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2271 | return true; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2274 | bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width, |
| 2275 | int height, GrColorType dstColorType, void* buffer, size_t rowBytes) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2276 | SkASSERT(surface); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2277 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2278 | GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2279 | if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2280 | return false; |
| 2281 | } |
| 2282 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2283 | // TODO: Avoid this conversion by making GrGLCaps work with color types. |
| 2284 | auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo); |
| 2285 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2286 | // We have a special case fallback for reading eight bit alpha. We will read back all four 8 |
| 2287 | // bit channels as RGBA and then extract A. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2288 | if (!this->readPixelsSupported(surface, dstAsConfig)) { |
| 2289 | if (kAlpha_8_GrPixelConfig == dstAsConfig && |
| 2290 | this->readPixelsSupported(surface, kRGBA_8888_GrPixelConfig)) { |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 2291 | std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2292 | if (this->onReadPixels(surface, origin, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2293 | GrColorType::kRGBA_8888, temp.get(), width * 4)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2294 | uint8_t* dst = reinterpret_cast<uint8_t*>(buffer); |
| 2295 | for (int j = 0; j < height; ++j) { |
| 2296 | for (int i = 0; i < width; ++i) { |
| 2297 | dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24; |
| 2298 | } |
| 2299 | } |
| 2300 | return true; |
| 2301 | } |
| 2302 | } |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2303 | |
| 2304 | // If reading in half float format is not supported, then read in a temporary float buffer |
| 2305 | // and convert to half float. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2306 | if (kRGBA_half_GrPixelConfig == dstAsConfig && |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2307 | this->readPixelsSupported(surface, kRGBA_float_GrPixelConfig)) { |
| 2308 | std::unique_ptr<float[]> temp(new float[width * height * 4]); |
| 2309 | if (this->onReadPixels(surface, origin, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2310 | GrColorType::kRGBA_F32, temp.get(), width * sizeof(float) * 4)) { |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2311 | uint8_t* dst = reinterpret_cast<uint8_t*>(buffer); |
| 2312 | float* src = temp.get(); |
| 2313 | for (int j = 0; j < height; ++j) { |
| 2314 | SkHalf* dstRow = reinterpret_cast<SkHalf*>(dst); |
| 2315 | for (int i = 0; i < width; ++i) { |
| 2316 | for (int color = 0; color < 4; color++) { |
| 2317 | *dstRow++ = SkFloatToHalf(*src++); |
| 2318 | } |
| 2319 | } |
| 2320 | dst += rowBytes; |
| 2321 | } |
| 2322 | return true; |
| 2323 | } |
| 2324 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 2325 | return false; |
| 2326 | } |
| 2327 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2328 | GrGLenum externalFormat; |
| 2329 | GrGLenum externalType; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2330 | if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2331 | &externalType)) { |
| 2332 | return false; |
| 2333 | } |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2334 | bool flipY = kBottomLeft_GrSurfaceOrigin == origin; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2335 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2336 | GrGLIRect glvp; |
| 2337 | if (renderTarget) { |
| 2338 | // resolve the render target if necessary |
| 2339 | switch (renderTarget->getResolveType()) { |
| 2340 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 2341 | return false; |
| 2342 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2343 | this->flushRenderTargetNoColorWrites(renderTarget); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2344 | break; |
| 2345 | case GrGLRenderTarget::kCanResolve_ResolveType: |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2346 | this->onResolveRenderTarget(renderTarget); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2347 | // we don't track the state of the READ FBO ID. |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2348 | this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2349 | break; |
| 2350 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2351 | SK_ABORT("Unknown resolve type"); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2352 | } |
| 2353 | glvp = renderTarget->getViewport(); |
| 2354 | } else { |
| 2355 | // Use a temporary FBO. |
| 2356 | this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2357 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 2360 | // the read rect is viewport-relative |
| 2361 | GrGLIRect readRect; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2362 | readRect.setRelativeTo(glvp, left, top, width, height, origin); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2363 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2364 | int bytesPerPixel = GrBytesPerPixel(dstAsConfig); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 2365 | size_t tightRowBytes = bytesPerPixel * width; |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 2366 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2367 | size_t readDstRowBytes = tightRowBytes; |
| 2368 | void* readDst = buffer; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2369 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2370 | // determine if GL can read using the passed rowBytes or if we need |
| 2371 | // a scratch buffer. |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 2372 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2373 | if (rowBytes != tightRowBytes) { |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 2374 | if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) { |
skia.committer@gmail.com | 4677acc | 2013-10-17 07:02:33 +0000 | [diff] [blame] | 2375 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 2376 | static_cast<GrGLint>(rowBytes / bytesPerPixel))); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2377 | readDstRowBytes = rowBytes; |
| 2378 | } else { |
| 2379 | scratch.reset(tightRowBytes * height); |
| 2380 | readDst = scratch.get(); |
| 2381 | } |
| 2382 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2383 | if (flipY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2384 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1)); |
| 2385 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2386 | GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig))); |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 2387 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2388 | GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, |
| 2389 | readRect.fWidth, readRect.fHeight, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2390 | externalFormat, externalType, readDst)); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2391 | if (readDstRowBytes != tightRowBytes) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2392 | SkASSERT(this->glCaps().packRowLengthSupport()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2393 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 2394 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2395 | if (flipY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2396 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0)); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2397 | flipY = false; |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2398 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2399 | |
| 2400 | // now reverse the order of the rows, since GL's are bottom-to-top, but our |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2401 | // API presents top-to-bottom. We must preserve the padding contents. Note |
| 2402 | // that the above readPixels did not overwrite the padding. |
| 2403 | if (readDst == buffer) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2404 | SkASSERT(rowBytes == readDstRowBytes); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2405 | if (flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2406 | scratch.reset(tightRowBytes); |
| 2407 | void* tmpRow = scratch.get(); |
| 2408 | // flip y in-place by rows |
| 2409 | const int halfY = height >> 1; |
| 2410 | char* top = reinterpret_cast<char*>(buffer); |
| 2411 | char* bottom = top + (height - 1) * rowBytes; |
| 2412 | for (int y = 0; y < halfY; y++) { |
| 2413 | memcpy(tmpRow, top, tightRowBytes); |
| 2414 | memcpy(top, bottom, tightRowBytes); |
| 2415 | memcpy(bottom, tmpRow, tightRowBytes); |
| 2416 | top += rowBytes; |
| 2417 | bottom -= rowBytes; |
| 2418 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2419 | } |
| 2420 | } else { |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 2421 | SkASSERT(readDst != buffer); |
| 2422 | SkASSERT(rowBytes != tightRowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2423 | // copy from readDst to buffer while flipping y |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 2424 | // const int halfY = height >> 1; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2425 | const char* src = reinterpret_cast<const char*>(readDst); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2426 | char* dst = reinterpret_cast<char*>(buffer); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2427 | if (flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2428 | dst += (height-1) * rowBytes; |
| 2429 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2430 | for (int y = 0; y < height; y++) { |
| 2431 | memcpy(dst, src, tightRowBytes); |
| 2432 | src += readDstRowBytes; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2433 | if (!flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2434 | dst += rowBytes; |
| 2435 | } else { |
| 2436 | dst -= rowBytes; |
| 2437 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2438 | } |
| 2439 | } |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2440 | if (!renderTarget) { |
| 2441 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface); |
| 2442 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2443 | return true; |
| 2444 | } |
| 2445 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 2446 | GrGpuRTCommandBuffer* GrGLGpu::createCommandBuffer( |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 2447 | GrRenderTarget* rt, GrSurfaceOrigin origin, |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 2448 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 2449 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) { |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 2450 | return new GrGLGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | GrGpuTextureCommandBuffer* GrGLGpu::createCommandBuffer(GrTexture* texture, |
| 2454 | GrSurfaceOrigin origin) { |
| 2455 | return new GrGLGpuTextureCommandBuffer(this, texture, origin); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2458 | void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin, |
| 2459 | const SkIRect& bounds, bool disableSRGB) { |
| 2460 | this->flushRenderTargetNoColorWrites(target, disableSRGB); |
| 2461 | this->didWriteToSurface(target, origin, &bounds); |
| 2462 | } |
bsalomon | 6ba6fa1 | 2015-03-04 11:57:37 -0800 | [diff] [blame] | 2463 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2464 | void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, bool disableSRGB) { |
| 2465 | this->flushRenderTargetNoColorWrites(target, disableSRGB); |
| 2466 | this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr); |
| 2467 | } |
| 2468 | |
| 2469 | void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target, bool disableSRGB) { |
| 2470 | SkASSERT(target); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2471 | GrGpuResource::UniqueID rtID = target->uniqueID(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2472 | if (fHWBoundRenderTargetUniqueID != rtID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2473 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2474 | #ifdef SK_DEBUG |
| 2475 | // don't do this check in Chromium -- this is causing |
| 2476 | // lots of repeated command buffer flushes when the compositor is |
| 2477 | // rendering with Ganesh, which is really slow; even too slow for |
| 2478 | // Debug mode. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 2479 | if (kChromium_GrGLDriver != this->glContext().driver()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2480 | GrGLenum status; |
| 2481 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 2482 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 2483 | SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
| 2484 | } |
bsalomon | 160f24c | 2015-03-17 15:55:42 -0700 | [diff] [blame] | 2485 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2486 | #endif |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 2487 | if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) { |
| 2488 | fRequiresFlushBeforeNextInstancedDraw = false; |
| 2489 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2490 | fHWBoundRenderTargetUniqueID = rtID; |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2491 | this->flushViewport(target->getViewport()); |
brianosman | 64d094d | 2016-03-25 06:01:59 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
brianosman | 35b784d | 2016-05-05 11:52:53 -0700 | [diff] [blame] | 2494 | if (this->glCaps().srgbWriteControl()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2495 | this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB); |
bsalomon | 5cd020f | 2015-03-17 12:46:56 -0700 | [diff] [blame] | 2496 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2497 | } |
bsalomon | a990912 | 2016-01-23 10:41:40 -0800 | [diff] [blame] | 2498 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2499 | void GrGLGpu::flushFramebufferSRGB(bool enable) { |
| 2500 | if (enable && kYes_TriState != fHWSRGBFramebuffer) { |
| 2501 | GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2502 | fHWSRGBFramebuffer = kYes_TriState; |
| 2503 | } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) { |
| 2504 | GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2505 | fHWSRGBFramebuffer = kNo_TriState; |
| 2506 | } |
| 2507 | } |
| 2508 | |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2509 | void GrGLGpu::flushViewport(const GrGLIRect& viewport) { |
| 2510 | if (fHWViewport != viewport) { |
| 2511 | viewport.pushToGLViewport(this->glInterface()); |
| 2512 | fHWViewport = viewport; |
| 2513 | } |
| 2514 | } |
| 2515 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2516 | #define SWAP_PER_DRAW 0 |
| 2517 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 2518 | #if SWAP_PER_DRAW |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2519 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2520 | #include <AGL/agl.h> |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2521 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | ce7357d | 2012-06-25 17:49:25 +0000 | [diff] [blame] | 2522 | #include <gl/GL.h> |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2523 | void SwapBuf() { |
| 2524 | DWORD procID = GetCurrentProcessId(); |
| 2525 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 2526 | while(hwnd) { |
| 2527 | DWORD wndProcID = 0; |
| 2528 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 2529 | if(wndProcID == procID) { |
| 2530 | SwapBuffers(GetDC(hwnd)); |
| 2531 | } |
| 2532 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 2533 | } |
| 2534 | } |
| 2535 | #endif |
| 2536 | #endif |
| 2537 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2538 | void GrGLGpu::draw(const GrPipeline& pipeline, |
| 2539 | const GrPrimitiveProcessor& primProc, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2540 | const GrMesh meshes[], |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2541 | const GrPipeline::DynamicState dynamicStates[], |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2542 | int meshCount) { |
| 2543 | this->handleDirtyContext(); |
| 2544 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2545 | bool hasPoints = false; |
| 2546 | for (int i = 0; i < meshCount; ++i) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2547 | if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) { |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2548 | hasPoints = true; |
| 2549 | break; |
| 2550 | } |
| 2551 | } |
| 2552 | if (!this->flushGLState(pipeline, primProc, hasPoints)) { |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 2553 | return; |
| 2554 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2555 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 2556 | for (int i = 0; i < meshCount; ++i) { |
| 2557 | if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 2558 | this->xferBarrier(pipeline.renderTarget(), barrierType); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 2559 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2560 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2561 | if (dynamicStates) { |
| 2562 | if (pipeline.getScissorState().enabled()) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 2563 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget()); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2564 | this->flushScissor(dynamicStates[i].fScissorRect, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 2565 | glRT->getViewport(), pipeline.proxy()->origin()); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2566 | } |
| 2567 | } |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2568 | if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() && |
| 2569 | GrIsPrimTypeLines(meshes[i].primitiveType()) && |
| 2570 | !GrIsPrimTypeLines(fLastPrimitiveType)) { |
| 2571 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2572 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
| 2573 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2574 | meshes[i].sendToGpu(primProc, this); |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2575 | fLastPrimitiveType = meshes[i].primitiveType(); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2576 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2577 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2578 | #if SWAP_PER_DRAW |
| 2579 | glFlush(); |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2580 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2581 | aglSwapBuffers(aglGetCurrentContext()); |
| 2582 | int set_a_break_pt_here = 9; |
| 2583 | aglSwapBuffers(aglGetCurrentContext()); |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2584 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2585 | SwapBuf(); |
| 2586 | int set_a_break_pt_here = 9; |
| 2587 | SwapBuf(); |
| 2588 | #endif |
| 2589 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2592 | static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) { |
| 2593 | switch (primitiveType) { |
| 2594 | case GrPrimitiveType::kTriangles: |
| 2595 | return GR_GL_TRIANGLES; |
| 2596 | case GrPrimitiveType::kTriangleStrip: |
| 2597 | return GR_GL_TRIANGLE_STRIP; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2598 | case GrPrimitiveType::kPoints: |
| 2599 | return GR_GL_POINTS; |
| 2600 | case GrPrimitiveType::kLines: |
| 2601 | return GR_GL_LINES; |
| 2602 | case GrPrimitiveType::kLineStrip: |
| 2603 | return GR_GL_LINE_STRIP; |
| 2604 | case GrPrimitiveType::kLinesAdjacency: |
| 2605 | return GR_GL_LINES_ADJACENCY; |
| 2606 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2607 | SK_ABORT("invalid GrPrimitiveType"); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2608 | return GR_GL_TRIANGLES; |
| 2609 | } |
| 2610 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2611 | void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType, |
| 2612 | const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2613 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2614 | if (this->glCaps().drawArraysBaseVertexIsBroken()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2615 | this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2616 | GL_CALL(DrawArrays(glPrimType, 0, vertexCount)); |
| 2617 | } else { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2618 | this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2619 | GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount)); |
| 2620 | } |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 2621 | if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) { |
| 2622 | fRequiresFlushBeforeNextInstancedDraw = true; |
| 2623 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2624 | fStats.incNumDraws(); |
| 2625 | } |
| 2626 | |
| 2627 | void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 2628 | GrPrimitiveType primitiveType, const GrBuffer* indexBuffer, |
| 2629 | int indexCount, int baseIndex, uint16_t minIndexValue, |
| 2630 | uint16_t maxIndexValue, const GrBuffer* vertexBuffer, |
| 2631 | int baseVertex) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2632 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2633 | GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() + |
| 2634 | sizeof(uint16_t) * baseIndex); |
| 2635 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2636 | this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2637 | |
| 2638 | if (this->glCaps().drawRangeElementsSupport()) { |
| 2639 | GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, |
| 2640 | GR_GL_UNSIGNED_SHORT, indices)); |
| 2641 | } else { |
| 2642 | GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices)); |
| 2643 | } |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 2644 | if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) { |
| 2645 | fRequiresFlushBeforeNextInstancedDraw = true; |
| 2646 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2647 | fStats.incNumDraws(); |
| 2648 | } |
| 2649 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2650 | void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType |
| 2651 | primitiveType, const GrBuffer* vertexBuffer, |
| 2652 | int vertexCount, int baseVertex, |
| 2653 | const GrBuffer* instanceBuffer, int instanceCount, |
| 2654 | int baseInstance) { |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 2655 | if (fRequiresFlushBeforeNextInstancedDraw) { |
| 2656 | SkASSERT(this->glCaps().requiresFlushBetweenNonAndInstancedDraws()); |
| 2657 | GL_CALL(Flush()); |
| 2658 | fRequiresFlushBeforeNextInstancedDraw = false; |
| 2659 | } |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 2660 | GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
| 2661 | int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount); |
| 2662 | for (int i = 0; i < instanceCount; i += maxInstances) { |
| 2663 | this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i); |
| 2664 | GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, |
| 2665 | SkTMin(instanceCount - i, maxInstances))); |
| 2666 | fStats.incNumDraws(); |
| 2667 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 2671 | GrPrimitiveType primitiveType, |
| 2672 | const GrBuffer* indexBuffer, int indexCount, |
| 2673 | int baseIndex, const GrBuffer* vertexBuffer, |
| 2674 | int baseVertex, const GrBuffer* instanceBuffer, |
| 2675 | int instanceCount, int baseInstance) { |
Chris Dalton | da40cd2 | 2018-04-16 13:19:58 -0600 | [diff] [blame] | 2676 | if (fRequiresFlushBeforeNextInstancedDraw) { |
| 2677 | SkASSERT(this->glCaps().requiresFlushBetweenNonAndInstancedDraws()); |
| 2678 | GL_CALL(Flush()); |
| 2679 | fRequiresFlushBeforeNextInstancedDraw = false; |
| 2680 | } |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2681 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2682 | GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() + |
| 2683 | sizeof(uint16_t) * baseIndex); |
| 2684 | this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, |
| 2685 | instanceBuffer, baseInstance); |
| 2686 | GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices, |
| 2687 | instanceCount)); |
| 2688 | fStats.incNumDraws(); |
| 2689 | } |
| 2690 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2691 | void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 2692 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2693 | if (rt->needsResolve()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2694 | // Some extensions automatically resolves the texture when it is read. |
| 2695 | if (this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2696 | SkASSERT(rt->textureFBOID() != rt->renderFBOID()); |
Brian Osman | cfe83d1 | 2018-01-19 11:13:45 -0500 | [diff] [blame] | 2697 | SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2698 | this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()); |
| 2699 | this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()); |
| 2700 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2701 | // make sure we go through flushRenderTarget() since we've modified |
| 2702 | // the bound DRAW FBO ID. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2703 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2704 | const GrGLIRect& vp = rt->getViewport(); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 2705 | const SkIRect dirtyRect = rt->getResolveRect(); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2706 | // The dirty rect tracked on the RT is always stored in the native coordinates of the |
| 2707 | // surface. Choose kTopLeft so no adjustments are made |
| 2708 | static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin; |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2709 | if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2710 | // Apple's extension uses the scissor as the blit bounds. |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2711 | GrScissorState scissorState; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 2712 | scissorState.set(dirtyRect); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2713 | this->flushScissor(scissorState, vp, kDirtyRectOrigin); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2714 | this->disableWindowRectangles(); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2715 | GL_CALL(ResolveMultisampleFramebuffer()); |
| 2716 | } else { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2717 | int l, b, r, t; |
| 2718 | if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & |
| 2719 | this->glCaps().blitFramebufferSupportFlags()) { |
| 2720 | l = 0; |
| 2721 | b = 0; |
| 2722 | r = target->width(); |
| 2723 | t = target->height(); |
| 2724 | } else { |
| 2725 | GrGLIRect rect; |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2726 | rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin); |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2727 | l = rect.fLeft; |
| 2728 | b = rect.fBottom; |
| 2729 | r = rect.fLeft + rect.fWidth; |
| 2730 | t = rect.fBottom + rect.fHeight; |
| 2731 | } |
derekf | 8c8f71a | 2014-09-16 06:24:57 -0700 | [diff] [blame] | 2732 | |
| 2733 | // BlitFrameBuffer respects the scissor, so disable it. |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 2734 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2735 | this->disableWindowRectangles(); |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2736 | GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2737 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 2738 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2739 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2740 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2744 | namespace { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2745 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2746 | |
| 2747 | GrGLenum gr_to_gl_stencil_op(GrStencilOp op) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2748 | static const GrGLenum gTable[kGrStencilOpCount] = { |
| 2749 | GR_GL_KEEP, // kKeep |
| 2750 | GR_GL_ZERO, // kZero |
| 2751 | GR_GL_REPLACE, // kReplace |
| 2752 | GR_GL_INVERT, // kInvert |
| 2753 | GR_GL_INCR_WRAP, // kIncWrap |
| 2754 | GR_GL_DECR_WRAP, // kDecWrap |
| 2755 | GR_GL_INCR, // kIncClamp |
| 2756 | GR_GL_DECR, // kDecClamp |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2757 | }; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2758 | GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep); |
| 2759 | GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero); |
| 2760 | GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace); |
| 2761 | GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert); |
| 2762 | GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap); |
| 2763 | GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap); |
| 2764 | GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp); |
| 2765 | GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp); |
| 2766 | SkASSERT(op < (GrStencilOp)kGrStencilOpCount); |
| 2767 | return gTable[(int)op]; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | void set_gl_stencil(const GrGLInterface* gl, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2771 | const GrStencilSettings::Face& face, |
| 2772 | GrGLenum glFace) { |
| 2773 | GrGLenum glFunc = GrToGLStencilFunc(face.fTest); |
| 2774 | GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp); |
| 2775 | GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2776 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2777 | GrGLint ref = face.fRef; |
| 2778 | GrGLint mask = face.fTestMask; |
| 2779 | GrGLint writeMask = face.fWriteMask; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2780 | |
| 2781 | if (GR_GL_FRONT_AND_BACK == glFace) { |
| 2782 | // we call the combined func just in case separate stencil is not |
| 2783 | // supported. |
| 2784 | GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask)); |
| 2785 | GR_GL_CALL(gl, StencilMask(writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2786 | GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2787 | } else { |
| 2788 | GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask)); |
| 2789 | GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2790 | GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2794 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2795 | void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2796 | if (stencilSettings.isDisabled()) { |
| 2797 | this->disableStencil(); |
| 2798 | } else if (fHWStencilSettings != stencilSettings) { |
| 2799 | if (kYes_TriState != fHWStencilTestEnabled) { |
| 2800 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2801 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2802 | fHWStencilTestEnabled = kYes_TriState; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2803 | } |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2804 | if (stencilSettings.isTwoSided()) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2805 | set_gl_stencil(this->glInterface(), |
| 2806 | stencilSettings.front(), |
| 2807 | GR_GL_FRONT); |
| 2808 | set_gl_stencil(this->glInterface(), |
| 2809 | stencilSettings.back(), |
| 2810 | GR_GL_BACK); |
| 2811 | } else { |
| 2812 | set_gl_stencil(this->glInterface(), |
| 2813 | stencilSettings.front(), |
| 2814 | GR_GL_FRONT_AND_BACK); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2815 | } |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2816 | fHWStencilSettings = stencilSettings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2820 | void GrGLGpu::disableStencil() { |
| 2821 | if (kNo_TriState != fHWStencilTestEnabled) { |
| 2822 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2823 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2824 | fHWStencilTestEnabled = kNo_TriState; |
| 2825 | fHWStencilSettings.invalidate(); |
| 2826 | } |
| 2827 | } |
| 2828 | |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2829 | void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) { |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2830 | // rt is only optional if useHWAA is false. |
| 2831 | SkASSERT(rt || !useHWAA); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 2832 | SkASSERT(!useHWAA || rt->isStencilBufferMultisampled()); |
bsalomon@google.com | 202d139 | 2013-03-19 18:58:08 +0000 | [diff] [blame] | 2833 | |
csmartdalton | 2b5f2cb | 2016-06-10 14:06:32 -0700 | [diff] [blame] | 2834 | if (this->caps()->multisampleDisableSupport()) { |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 2835 | if (useHWAA) { |
| 2836 | if (kYes_TriState != fMSAAEnabled) { |
| 2837 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
| 2838 | fMSAAEnabled = kYes_TriState; |
| 2839 | } |
| 2840 | } else { |
| 2841 | if (kNo_TriState != fMSAAEnabled) { |
| 2842 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 2843 | fMSAAEnabled = kNo_TriState; |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | } |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2847 | |
| 2848 | if (0 != this->caps()->maxRasterSamples()) { |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 2849 | if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) { |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2850 | // Since stencil is disabled and we want more samples than are in the color buffer, we |
| 2851 | // need to tell the rasterizer explicitly how many to run. |
| 2852 | if (kYes_TriState != fHWRasterMultisampleEnabled) { |
| 2853 | GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE)); |
| 2854 | fHWRasterMultisampleEnabled = kYes_TriState; |
| 2855 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 2856 | int numStencilSamples = rt->numStencilSamples(); |
| 2857 | // convert to GL's understanding of sample counts where 0 means nonMSAA. |
| 2858 | numStencilSamples = 1 == numStencilSamples ? 0 : numStencilSamples; |
| 2859 | if (numStencilSamples != fHWNumRasterSamples) { |
| 2860 | SkASSERT(numStencilSamples <= this->caps()->maxRasterSamples()); |
| 2861 | GL_CALL(RasterSamples(numStencilSamples, GR_GL_TRUE)); |
| 2862 | fHWNumRasterSamples = numStencilSamples; |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2863 | } |
| 2864 | } else { |
| 2865 | if (kNo_TriState != fHWRasterMultisampleEnabled) { |
| 2866 | GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE)); |
| 2867 | fHWRasterMultisampleEnabled = kNo_TriState; |
| 2868 | } |
| 2869 | } |
| 2870 | } else { |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 2871 | SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled); |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2872 | } |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2875 | void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) { |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2876 | // Any optimization to disable blending should have already been applied and |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2877 | // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0). |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 2878 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2879 | GrBlendEquation equation = blendInfo.fEquation; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 2880 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 2881 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2882 | bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) && |
| 2883 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff; |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2884 | if (blendOff) { |
| 2885 | if (kNo_TriState != fHWBlendState.fEnabled) { |
| 2886 | GL_CALL(Disable(GR_GL_BLEND)); |
joel.liang | 9764c40 | 2015-07-09 19:46:18 -0700 | [diff] [blame] | 2887 | |
| 2888 | // Workaround for the ARM KHR_blend_equation_advanced blacklist issue |
| 2889 | // https://code.google.com/p/skia/issues/detail?id=3943 |
| 2890 | if (kARM_GrGLVendor == this->ctxInfo().vendor() && |
| 2891 | GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) { |
| 2892 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2893 | // Set to any basic blending equation. |
| 2894 | GrBlendEquation blend_equation = kAdd_GrBlendEquation; |
| 2895 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation])); |
| 2896 | fHWBlendState.fEquation = blend_equation; |
| 2897 | } |
| 2898 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2899 | fHWBlendState.fEnabled = kNo_TriState; |
| 2900 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2901 | return; |
| 2902 | } |
| 2903 | |
| 2904 | if (kYes_TriState != fHWBlendState.fEnabled) { |
| 2905 | GL_CALL(Enable(GR_GL_BLEND)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2906 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2907 | fHWBlendState.fEnabled = kYes_TriState; |
| 2908 | } |
| 2909 | |
| 2910 | if (fHWBlendState.fEquation != equation) { |
| 2911 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation])); |
| 2912 | fHWBlendState.fEquation = equation; |
| 2913 | } |
| 2914 | |
| 2915 | if (GrBlendEquationIsAdvanced(equation)) { |
| 2916 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2917 | // Advanced equations have no other blend state. |
| 2918 | return; |
| 2919 | } |
| 2920 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 2921 | if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2922 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 2923 | gXfermodeCoeff2Blend[dstCoeff])); |
| 2924 | fHWBlendState.fSrcCoeff = srcCoeff; |
| 2925 | fHWBlendState.fDstCoeff = dstCoeff; |
| 2926 | } |
| 2927 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2928 | if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) { |
| 2929 | GrColor blendConst = blendInfo.fBlendConstant; |
| 2930 | blendConst = swizzle.applyTo(blendConst); |
| 2931 | if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) { |
| 2932 | GrGLfloat c[4]; |
| 2933 | GrColorToRGBAFloat(blendConst, c); |
| 2934 | GL_CALL(BlendColor(c[0], c[1], c[2], c[3])); |
| 2935 | fHWBlendState.fConstColor = blendConst; |
| 2936 | fHWBlendState.fConstColorValid = true; |
| 2937 | } |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2940 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2941 | static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) { |
| 2942 | switch (wrapMode) { |
| 2943 | case GrSamplerState::WrapMode::kClamp: |
| 2944 | return GR_GL_CLAMP_TO_EDGE; |
| 2945 | case GrSamplerState::WrapMode::kRepeat: |
| 2946 | return GR_GL_REPEAT; |
| 2947 | case GrSamplerState::WrapMode::kMirrorRepeat: |
| 2948 | return GR_GL_MIRRORED_REPEAT; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2949 | }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2950 | SK_ABORT("Unknown wrap mode"); |
| 2951 | return 0; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2954 | static GrGLenum get_component_enum_from_char(char component) { |
| 2955 | switch (component) { |
| 2956 | case 'r': |
| 2957 | return GR_GL_RED; |
| 2958 | case 'g': |
| 2959 | return GR_GL_GREEN; |
| 2960 | case 'b': |
| 2961 | return GR_GL_BLUE; |
| 2962 | case 'a': |
| 2963 | return GR_GL_ALPHA; |
| 2964 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2965 | SK_ABORT("Unsupported component"); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2966 | return 0; |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 2971 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 2972 | static void get_tex_param_swizzle(GrPixelConfig config, |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2973 | const GrGLCaps& caps, |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2974 | GrGLenum* glSwizzle) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2975 | const GrSwizzle& swizzle = caps.configSwizzle(config); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2976 | for (int i = 0; i < 4; ++i) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2977 | glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2981 | static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) { |
| 2982 | switch (filter) { |
| 2983 | case GrSamplerState::Filter::kNearest: |
| 2984 | return GR_GL_NEAREST; |
| 2985 | case GrSamplerState::Filter::kBilerp: |
| 2986 | return GR_GL_LINEAR; |
| 2987 | case GrSamplerState::Filter::kMipMap: |
| 2988 | return GR_GL_LINEAR; |
| 2989 | } |
| 2990 | SK_ABORT("Unknown filter"); |
| 2991 | return 0; |
| 2992 | } |
| 2993 | |
| 2994 | static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) { |
| 2995 | switch (filter) { |
| 2996 | case GrSamplerState::Filter::kNearest: |
| 2997 | return GR_GL_NEAREST; |
| 2998 | case GrSamplerState::Filter::kBilerp: |
| 2999 | return GR_GL_LINEAR; |
| 3000 | case GrSamplerState::Filter::kMipMap: |
| 3001 | return GR_GL_LINEAR_MIPMAP_LINEAR; |
| 3002 | } |
| 3003 | SK_ABORT("Unknown filter"); |
| 3004 | return 0; |
| 3005 | } |
| 3006 | |
| 3007 | void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3008 | GrGLTexture* texture, GrSurfaceOrigin textureOrigin) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3009 | SkASSERT(texture); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 3010 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 3011 | #ifdef SK_DEBUG |
| 3012 | if (!this->caps()->npotTextureTileSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3013 | if (samplerState.isRepeated()) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 3014 | const int w = texture->width(); |
| 3015 | const int h = texture->height(); |
| 3016 | SkASSERT(SkIsPow2(w) && SkIsPow2(h)); |
| 3017 | } |
| 3018 | } |
| 3019 | #endif |
| 3020 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 3021 | // If we created a rt/tex and rendered to it without using a texture and now we're texturing |
| 3022 | // 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] | 3023 | // out of the "last != next" check. |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 3024 | GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3025 | if (texRT) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3026 | this->onResolveRenderTarget(texRT); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3029 | GrGpuResource::UniqueID textureID = texture->uniqueID(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3030 | GrGLenum target = texture->target(); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3031 | if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3032 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3033 | GL_CALL(BindTexture(target, texture->textureID())); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3034 | fHWBoundTextureUniqueIDs[unitIdx] = textureID; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3037 | ResetTimestamp timestamp; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3038 | const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(×tamp); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3039 | bool setAll = timestamp < this->getResetTimestamp(); |
| 3040 | GrGLTexture::TexParams newTexParams; |
| 3041 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3042 | GrSamplerState::Filter filterMode = samplerState.filter(); |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3043 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3044 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 3045 | if (!this->caps()->mipMapSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3046 | filterMode = GrSamplerState::Filter::kBilerp; |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3047 | } |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 3048 | } |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3049 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3050 | newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode); |
| 3051 | newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode); |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 3052 | |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 3053 | if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) { |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 3054 | newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3055 | if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) { |
brianosman | adf4fa6 | 2016-04-26 16:48:06 -0700 | [diff] [blame] | 3056 | this->setTextureUnit(unitIdx); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3057 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3058 | } |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 3059 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3060 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3061 | #ifdef SK_DEBUG |
| 3062 | // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3063 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3064 | SkASSERT(!texture->texturePriv().mipMapsAreDirty()); |
| 3065 | if (GrPixelConfigIsSRGB(texture->config())) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3066 | SkDestinationSurfaceColorMode colorMode = allowSRGBInputs |
| 3067 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 3068 | : SkDestinationSurfaceColorMode::kLegacy; |
| 3069 | SkASSERT(texture->texturePriv().mipColorMode() == colorMode); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3070 | } |
| 3071 | } |
| 3072 | #endif |
| 3073 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3074 | newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel(); |
| 3075 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3076 | newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX()); |
| 3077 | newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 3078 | get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA); |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 3079 | if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3080 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3081 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter)); |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 3082 | } |
| 3083 | if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) { |
| 3084 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3085 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3086 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3087 | if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) { |
cblume | 09bd2c0 | 2016-03-01 14:08:28 -0800 | [diff] [blame] | 3088 | // These are not supported in ES2 contexts |
| 3089 | if (this->glCaps().mipMapLevelAndLodControlSupport()) { |
| 3090 | if (newTexParams.fMaxMipMapLevel != 0) { |
| 3091 | this->setTextureUnit(unitIdx); |
| 3092 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0)); |
| 3093 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0)); |
| 3094 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD, |
| 3095 | newTexParams.fMaxMipMapLevel)); |
| 3096 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL, |
| 3097 | newTexParams.fMaxMipMapLevel)); |
| 3098 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3099 | } |
| 3100 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3101 | if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3102 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3103 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3104 | } |
| 3105 | if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3106 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3107 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3108 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 3109 | if (this->glCaps().textureSwizzleSupport() && |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3110 | (setAll || memcmp(newTexParams.fSwizzleRGBA, |
| 3111 | oldTexParams.fSwizzleRGBA, |
| 3112 | sizeof(newTexParams.fSwizzleRGBA)))) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3113 | this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3114 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3115 | texture->setCachedTexParams(newTexParams, this->getResetTimestamp()); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3118 | void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3119 | SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig)); |
| 3120 | SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3121 | |
| 3122 | BufferTexture& buffTex = fHWBufferTextures[unitIdx]; |
| 3123 | |
| 3124 | if (!buffTex.fKnownBound) { |
| 3125 | if (!buffTex.fTextureID) { |
| 3126 | GL_CALL(GenTextures(1, &buffTex.fTextureID)); |
| 3127 | if (!buffTex.fTextureID) { |
| 3128 | return; |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | this->setTextureUnit(unitIdx); |
| 3133 | GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID)); |
| 3134 | |
| 3135 | buffTex.fKnownBound = true; |
| 3136 | } |
| 3137 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 3138 | if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID || |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3139 | buffTex.fTexelConfig != texelConfig) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3140 | |
| 3141 | this->setTextureUnit(unitIdx); |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3142 | GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER, |
| 3143 | this->glCaps().configSizedInternalFormat(texelConfig), |
| 3144 | buffer->bufferID())); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3145 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3146 | buffTex.fTexelConfig = texelConfig; |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 3147 | buffTex.fAttachedBufferUniqueID = buffer->uniqueID(); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3148 | |
| 3149 | if (this->glCaps().textureSwizzleSupport() && |
| 3150 | this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) { |
| 3151 | GrGLenum glSwizzle[4]; |
| 3152 | get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle); |
| 3153 | this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle); |
| 3154 | buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig); |
| 3155 | } |
| 3156 | |
| 3157 | buffer->setHasAttachedToTexture(); |
| 3158 | fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit); |
| 3159 | } |
| 3160 | } |
| 3161 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3162 | void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3163 | GrGLTexture* texture, GrSurfaceOrigin textureOrigin) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3164 | SkASSERT(texture); |
| 3165 | |
| 3166 | // First, figure out if we need mips for this texture at all: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3167 | GrSamplerState::Filter filterMode = params.filter(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3168 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3169 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 3170 | if (!this->caps()->mipMapSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3171 | filterMode = GrSamplerState::Filter::kBilerp; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3175 | if (GrSamplerState::Filter::kMipMap != filterMode) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
| 3179 | // If this is an sRGB texture and the mips were previously built the "other" way |
| 3180 | // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for |
| 3181 | // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it. |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3182 | SkDestinationSurfaceColorMode colorMode = allowSRGBInputs |
| 3183 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 3184 | : SkDestinationSurfaceColorMode::kLegacy; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3185 | if (GrPixelConfigIsSRGB(texture->config()) && |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3186 | colorMode != texture->texturePriv().mipColorMode()) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 3187 | texture->texturePriv().markMipMapsDirty(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | // If the mips aren't dirty, we're done: |
| 3191 | if (!texture->texturePriv().mipMapsAreDirty()) { |
| 3192 | return; |
| 3193 | } |
| 3194 | |
| 3195 | // If we created a rt/tex and rendered to it without using a texture and now we're texturing |
| 3196 | // from the rt it will still be the last bound texture, but it needs resolving. |
| 3197 | GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget()); |
| 3198 | if (texRT) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3199 | this->onResolveRenderTarget(texRT); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | GrGLenum target = texture->target(); |
| 3203 | this->setScratchTextureUnit(); |
| 3204 | GL_CALL(BindTexture(target, texture->textureID())); |
| 3205 | |
| 3206 | // Configure sRGB decode, if necessary. This state is the only thing needed for the driver |
| 3207 | // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too. |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 3208 | if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) { |
Brian Osman | b040320 | 2016-12-09 10:55:23 -0500 | [diff] [blame] | 3209 | GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT; |
| 3210 | // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly. |
| 3211 | // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample, |
| 3212 | // but not the Linear -> sRGB when writing the next level. The result is that mip-chains |
| 3213 | // get progressively brighter as you go down. Forcing this to 'decode' gives predictable |
| 3214 | // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28) |
| 3215 | if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) { |
| 3216 | srgbDecode = GR_GL_DECODE_EXT; |
| 3217 | } |
| 3218 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | // Either do manual mipmap generation or (if that fails), just rely on the driver: |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3222 | if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3223 | GL_CALL(GenerateMipmap(target)); |
| 3224 | } |
| 3225 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 3226 | texture->texturePriv().markMipMapsClean(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3227 | texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount( |
| 3228 | texture->width(), texture->height())); |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3229 | texture->texturePriv().setMipColorMode(colorMode); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3230 | |
| 3231 | // We have potentially set lots of state on the texture. Easiest to dirty it all: |
| 3232 | texture->textureParamsModified(); |
| 3233 | } |
| 3234 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3235 | void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) { |
| 3236 | this->setTextureUnit(unitIdx); |
| 3237 | if (this->glStandard() == kGLES_GrGLStandard) { |
| 3238 | // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA. |
| 3239 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0])); |
| 3240 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1])); |
| 3241 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2])); |
| 3242 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3])); |
| 3243 | } else { |
| 3244 | GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint)); |
| 3245 | GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, |
| 3246 | reinterpret_cast<const GrGLint*>(swizzle))); |
| 3247 | } |
| 3248 | } |
| 3249 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 3250 | void GrGLGpu::flushColorWrite(bool writeColor) { |
| 3251 | if (!writeColor) { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 3252 | if (kNo_TriState != fHWWriteToColor) { |
| 3253 | GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE, |
| 3254 | GR_GL_FALSE, GR_GL_FALSE)); |
| 3255 | fHWWriteToColor = kNo_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3256 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 3257 | } else { |
| 3258 | if (kYes_TriState != fHWWriteToColor) { |
| 3259 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 3260 | fHWWriteToColor = kYes_TriState; |
| 3261 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3262 | } |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 3263 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3264 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 3265 | void GrGLGpu::setTextureUnit(int unit) { |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3266 | SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3267 | if (unit != fHWActiveTextureUnitIdx) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 3268 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 4920939 | 2012-06-05 15:13:46 +0000 | [diff] [blame] | 3269 | fHWActiveTextureUnitIdx = unit; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3270 | } |
| 3271 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 3272 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 3273 | void GrGLGpu::setScratchTextureUnit() { |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3274 | // Bind the last texture unit since it is the least likely to be used by GrGLProgram. |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3275 | int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1; |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3276 | if (lastUnitIdx != fHWActiveTextureUnitIdx) { |
| 3277 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx)); |
| 3278 | fHWActiveTextureUnitIdx = lastUnitIdx; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3279 | } |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3280 | // clear out the this field so that if a program does use this unit it will rebind the correct |
| 3281 | // texture. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3282 | fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3285 | // Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3286 | static inline bool can_blit_framebuffer_for_copy_surface( |
| 3287 | const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3288 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 3289 | const SkIRect& srcRect, |
| 3290 | const SkIPoint& dstPoint, |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3291 | const GrGLCaps& caps) { |
| 3292 | int dstSampleCnt = 0; |
| 3293 | int srcSampleCnt = 0; |
| 3294 | if (const GrRenderTarget* rt = dst->asRenderTarget()) { |
| 3295 | dstSampleCnt = rt->numColorSamples(); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3296 | } |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3297 | if (const GrRenderTarget* rt = src->asRenderTarget()) { |
| 3298 | srcSampleCnt = rt->numColorSamples(); |
| 3299 | } |
| 3300 | SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget())); |
| 3301 | SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget())); |
| 3302 | |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3303 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 3304 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3305 | |
| 3306 | bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false; |
| 3307 | bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false; |
| 3308 | |
| 3309 | return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D, |
| 3310 | dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex), |
| 3311 | srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint); |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 3312 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3313 | |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3314 | static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) { |
| 3315 | // A RT has a separate MSAA renderbuffer if: |
| 3316 | // 1) It's multisampled |
| 3317 | // 2) We're using an extension with separate MSAA renderbuffers |
| 3318 | // 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] | 3319 | return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0; |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3320 | } |
| 3321 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3322 | static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3323 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3324 | const GrGLCaps& caps) { |
| 3325 | |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3326 | const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget()); |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 3327 | const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget()); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3328 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3329 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 3330 | |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3331 | bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false; |
| 3332 | bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false; |
| 3333 | |
| 3334 | bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false; |
| 3335 | bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false; |
| 3336 | |
| 3337 | return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex), |
| 3338 | dstIsGLTexture2D, dstOrigin, src->config(), |
| 3339 | srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D, |
| 3340 | srcOrigin); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | // If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is |
| 3344 | // relative to is output. |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3345 | void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport, |
| 3346 | TempFBOTarget tempFBOTarget) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3347 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3348 | if (!rt) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3349 | SkASSERT(surface->asTexture()); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 3350 | GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture()); |
| 3351 | GrGLuint texID = texture->textureID(); |
| 3352 | GrGLenum target = texture->target(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3353 | GrGLuint* tempFBOID; |
| 3354 | tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID; |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3355 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3356 | if (0 == *tempFBOID) { |
| 3357 | GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3358 | } |
| 3359 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3360 | this->bindFramebuffer(fboTarget, *tempFBOID); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3361 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 3362 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3363 | target, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 3364 | texID, |
| 3365 | 0)); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 3366 | texture->baseLevelWasBoundToFBO(); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3367 | viewport->fLeft = 0; |
| 3368 | viewport->fBottom = 0; |
| 3369 | viewport->fWidth = surface->width(); |
| 3370 | viewport->fHeight = surface->height(); |
| 3371 | } else { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3372 | this->bindFramebuffer(fboTarget, rt->renderFBOID()); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3373 | *viewport = rt->getViewport(); |
| 3374 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3375 | } |
| 3376 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3377 | void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) { |
| 3378 | // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3379 | if (!surface->asRenderTarget()) { |
| 3380 | SkASSERT(surface->asTexture()); |
| 3381 | GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target(); |
| 3382 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
| 3383 | GR_GL_COLOR_ATTACHMENT0, |
| 3384 | textureTarget, |
| 3385 | 0, |
| 3386 | 0)); |
| 3387 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3390 | void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) { |
| 3391 | fStats.incRenderTargetBinds(); |
| 3392 | GL_CALL(BindFramebuffer(target, fboid)); |
| 3393 | if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) { |
| 3394 | fBoundDrawFramebuffer = fboid; |
| 3395 | } |
| 3396 | |
Adrienne Walker | 3ed3399 | 2018-05-15 11:44:34 -0700 | [diff] [blame] | 3397 | if (!this->caps()->workarounds().restore_scissor_on_fbo_change) { |
| 3398 | return; |
| 3399 | } |
| 3400 | |
| 3401 | // The driver forgets the correct scissor when modifying the FBO binding. |
Adrienne Walker | 3a69c74 | 2018-05-21 11:05:48 -0700 | [diff] [blame] | 3402 | if (!fHWScissorSettings.fRect.isInvalid()) { |
| 3403 | fHWScissorSettings.fRect.pushToGLScissor(this->glInterface()); |
| 3404 | } |
Adrienne Walker | 3ed3399 | 2018-05-15 11:44:34 -0700 | [diff] [blame] | 3405 | |
| 3406 | // crbug.com/222018 - Also on QualComm, the flush here avoids flicker, |
| 3407 | // it's unclear how this bug works. |
| 3408 | GL_CALL(Flush()); |
| 3409 | } |
| 3410 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3411 | void GrGLGpu::deleteFramebuffer(GrGLuint fboid) { |
| 3412 | if (fboid == fBoundDrawFramebuffer && |
| 3413 | this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) { |
| 3414 | // This workaround only applies to deleting currently bound framebuffers |
| 3415 | // on Adreno 420. Because this is a somewhat rare case, instead of |
| 3416 | // tracking all the attachments of every framebuffer instead just always |
| 3417 | // unbind all attachments. |
| 3418 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 3419 | GR_GL_RENDERBUFFER, 0)); |
| 3420 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, |
| 3421 | GR_GL_RENDERBUFFER, 0)); |
| 3422 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, |
| 3423 | GR_GL_RENDERBUFFER, 0)); |
| 3424 | } |
| 3425 | |
| 3426 | GL_CALL(DeleteFramebuffers(1, &fboid)); |
| 3427 | } |
| 3428 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3429 | bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3430 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 3431 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 3432 | bool canDiscardOutsideDstRect) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3433 | // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the |
| 3434 | // swizzle. |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3435 | if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) != |
| 3436 | this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3437 | return false; |
| 3438 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3439 | // 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] | 3440 | // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites(). |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3441 | bool preferCopy = SkToBool(dst->asRenderTarget()); |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3442 | if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3443 | if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3444 | return true; |
| 3445 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 3446 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 3447 | |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3448 | if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3449 | this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3450 | return true; |
| 3451 | } |
| 3452 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3453 | if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin, |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3454 | srcRect, dstPoint, this->glCaps())) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3455 | return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin, |
| 3456 | srcRect, dstPoint); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3459 | if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3460 | if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3461 | return true; |
| 3462 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3463 | } |
| 3464 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3465 | return false; |
| 3466 | } |
| 3467 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3468 | bool GrGLGpu::createCopyProgram(GrTexture* srcTex) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 3469 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 3470 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3471 | int progIdx = TextureToCopyProgramIdx(srcTex); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3472 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3473 | GrSLType samplerType = srcTex->texturePriv().samplerType(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3474 | |
| 3475 | if (!fCopyProgramArrayBuffer) { |
| 3476 | static const GrGLfloat vdata[] = { |
| 3477 | 0, 0, |
| 3478 | 0, 1, |
| 3479 | 1, 0, |
| 3480 | 1, 1 |
| 3481 | }; |
| 3482 | fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType, |
| 3483 | kStatic_GrAccessPattern, vdata)); |
| 3484 | } |
| 3485 | if (!fCopyProgramArrayBuffer) { |
| 3486 | return false; |
| 3487 | } |
| 3488 | |
| 3489 | SkASSERT(!fCopyPrograms[progIdx].fProgram); |
| 3490 | GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram()); |
| 3491 | if (!fCopyPrograms[progIdx].fProgram) { |
| 3492 | return false; |
| 3493 | } |
| 3494 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3495 | const char* version = shaderCaps->versionDeclString(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3496 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3497 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3498 | GrShaderVar::kUniform_TypeModifier); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3499 | GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3500 | GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3501 | GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier); |
| 3502 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3503 | |
| 3504 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3505 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3506 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3507 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3508 | } |
| 3509 | vTexCoord.addModifier("noperspective"); |
| 3510 | } |
| 3511 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3512 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3513 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3514 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3515 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3516 | uPosXform.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3517 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3518 | vTexCoord.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3519 | vshaderTxt.append(";"); |
| 3520 | |
| 3521 | vshaderTxt.append( |
| 3522 | "// Copy Program VS\n" |
| 3523 | "void main() {" |
| 3524 | " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3525 | " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;" |
| 3526 | " sk_Position.zw = half2(0, 1);" |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3527 | "}" |
| 3528 | ); |
| 3529 | |
| 3530 | SkString fshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3531 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3532 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3533 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3534 | } |
| 3535 | } |
Brian Salomon | f31ae49 | 2016-11-18 15:35:33 -0500 | [diff] [blame] | 3536 | vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3537 | vTexCoord.appendDecl(shaderCaps, &fshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3538 | fshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3539 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3540 | fshaderTxt.append(";"); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3541 | fshaderTxt.appendf( |
| 3542 | "// Copy Program FS\n" |
| 3543 | "void main() {" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3544 | " sk_FragColor = texture(u_texture, v_texCoord);" |
| 3545 | "}" |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3546 | ); |
| 3547 | |
| 3548 | const char* str; |
| 3549 | GrGLint length; |
| 3550 | |
| 3551 | str = vshaderTxt.c_str(); |
| 3552 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3553 | SkSL::Program::Settings settings; |
| 3554 | settings.fCaps = shaderCaps; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3555 | SkSL::String glsl; |
| 3556 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3557 | &str, &length, 1, settings, &glsl); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3558 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3559 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3560 | &fStats, settings); |
| 3561 | SkASSERT(program->fInputs.isEmpty()); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3562 | |
| 3563 | str = fshaderTxt.c_str(); |
| 3564 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3565 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3566 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3567 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3568 | &fStats, settings); |
| 3569 | SkASSERT(program->fInputs.isEmpty()); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3570 | |
| 3571 | GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram)); |
| 3572 | |
| 3573 | GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform, |
| 3574 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture")); |
| 3575 | GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform, |
| 3576 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform")); |
| 3577 | GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 3578 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3579 | |
| 3580 | GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3581 | |
| 3582 | GL_CALL(DeleteShader(vshader)); |
| 3583 | GL_CALL(DeleteShader(fshader)); |
| 3584 | |
| 3585 | return true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3588 | bool GrGLGpu::createMipmapProgram(int progIdx) { |
| 3589 | const bool oddWidth = SkToBool(progIdx & 0x2); |
| 3590 | const bool oddHeight = SkToBool(progIdx & 0x1); |
| 3591 | const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1); |
| 3592 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3593 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3594 | |
| 3595 | SkASSERT(!fMipmapPrograms[progIdx].fProgram); |
| 3596 | GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram()); |
| 3597 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 3598 | return false; |
| 3599 | } |
| 3600 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3601 | const char* version = shaderCaps->versionDeclString(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3602 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3603 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3604 | GrShaderVar::kUniform_TypeModifier); |
| 3605 | GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType, |
| 3606 | GrShaderVar::kUniform_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3607 | // 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] | 3608 | GrShaderVar vTexCoords[] = { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3609 | GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3610 | GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3611 | GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3612 | GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3613 | }; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3614 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3615 | |
| 3616 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3617 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3618 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3619 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3620 | } |
| 3621 | vTexCoords[0].addModifier("noperspective"); |
| 3622 | vTexCoords[1].addModifier("noperspective"); |
| 3623 | vTexCoords[2].addModifier("noperspective"); |
| 3624 | vTexCoords[3].addModifier("noperspective"); |
| 3625 | } |
| 3626 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3627 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3628 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3629 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3630 | vshaderTxt.append(";"); |
| 3631 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3632 | vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3633 | vshaderTxt.append(";"); |
| 3634 | } |
| 3635 | |
| 3636 | vshaderTxt.append( |
| 3637 | "// Mipmap Program VS\n" |
| 3638 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3639 | " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);" |
| 3640 | " sk_Position.zw = half2(0, 1);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3641 | ); |
| 3642 | |
| 3643 | // Insert texture coordinate computation: |
| 3644 | if (oddWidth && oddHeight) { |
| 3645 | vshaderTxt.append( |
| 3646 | " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3647 | " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);" |
| 3648 | " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3649 | " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;" |
| 3650 | ); |
| 3651 | } else if (oddWidth) { |
| 3652 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3653 | " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);" |
| 3654 | " 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] | 3655 | ); |
| 3656 | } else if (oddHeight) { |
| 3657 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3658 | " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);" |
| 3659 | " 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] | 3660 | ); |
| 3661 | } else { |
| 3662 | vshaderTxt.append( |
| 3663 | " v_texCoord0 = a_vertex.xy;" |
| 3664 | ); |
| 3665 | } |
| 3666 | |
| 3667 | vshaderTxt.append("}"); |
| 3668 | |
| 3669 | SkString fshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3670 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3671 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3672 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3673 | } |
| 3674 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3675 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | f31ae49 | 2016-11-18 15:35:33 -0500 | [diff] [blame] | 3676 | vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3677 | vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3678 | fshaderTxt.append(";"); |
| 3679 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3680 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3681 | fshaderTxt.append(";"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3682 | fshaderTxt.append( |
| 3683 | "// Mipmap Program FS\n" |
| 3684 | "void main() {" |
| 3685 | ); |
| 3686 | |
| 3687 | if (oddWidth && oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3688 | fshaderTxt.append( |
| 3689 | " sk_FragColor = (texture(u_texture, v_texCoord0) + " |
| 3690 | " texture(u_texture, v_texCoord1) + " |
| 3691 | " texture(u_texture, v_texCoord2) + " |
| 3692 | " texture(u_texture, v_texCoord3)) * 0.25;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3693 | ); |
| 3694 | } else if (oddWidth || oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3695 | fshaderTxt.append( |
| 3696 | " sk_FragColor = (texture(u_texture, v_texCoord0) + " |
| 3697 | " texture(u_texture, v_texCoord1)) * 0.5;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3698 | ); |
| 3699 | } else { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3700 | fshaderTxt.append( |
| 3701 | " sk_FragColor = texture(u_texture, v_texCoord0);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3702 | ); |
| 3703 | } |
| 3704 | |
| 3705 | fshaderTxt.append("}"); |
| 3706 | |
| 3707 | const char* str; |
| 3708 | GrGLint length; |
| 3709 | |
| 3710 | str = vshaderTxt.c_str(); |
| 3711 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3712 | SkSL::Program::Settings settings; |
| 3713 | settings.fCaps = shaderCaps; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3714 | SkSL::String glsl; |
| 3715 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3716 | &str, &length, 1, settings, &glsl); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3717 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3718 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3719 | &fStats, settings); |
| 3720 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3721 | |
| 3722 | str = fshaderTxt.c_str(); |
| 3723 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3724 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3725 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3726 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3727 | &fStats, settings); |
| 3728 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3729 | |
| 3730 | GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram)); |
| 3731 | |
| 3732 | GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform, |
| 3733 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture")); |
| 3734 | GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 3735 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3736 | |
| 3737 | GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3738 | |
| 3739 | GL_CALL(DeleteShader(vshader)); |
| 3740 | GL_CALL(DeleteShader(fshader)); |
| 3741 | |
| 3742 | return true; |
| 3743 | } |
| 3744 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3745 | bool GrGLGpu::createStencilClipClearProgram() { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 3746 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 3747 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3748 | if (!fStencilClipClearArrayBuffer) { |
| 3749 | static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1}; |
| 3750 | fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create( |
| 3751 | this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata)); |
| 3752 | if (!fStencilClipClearArrayBuffer) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3753 | return false; |
| 3754 | } |
| 3755 | } |
| 3756 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3757 | SkASSERT(!fStencilClipClearProgram); |
| 3758 | GL_CALL_RET(fStencilClipClearProgram, CreateProgram()); |
| 3759 | if (!fStencilClipClearProgram) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3760 | return false; |
| 3761 | } |
| 3762 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3763 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3764 | const char* version = this->caps()->shaderCaps()->versionDeclString(); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3765 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3766 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3767 | aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3768 | vshaderTxt.append(";"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3769 | vshaderTxt.append( |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3770 | "// Stencil Clip Clear Program VS\n" |
| 3771 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3772 | " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);" |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3773 | "}"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3774 | |
| 3775 | SkString fshaderTxt(version); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3776 | fshaderTxt.appendf( |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3777 | "// Stencil Clip Clear Program FS\n" |
| 3778 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3779 | " sk_FragColor = half4(0);" |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3780 | "}"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3781 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3782 | const char* str; |
| 3783 | GrGLint length; |
| 3784 | |
| 3785 | str = vshaderTxt.c_str(); |
| 3786 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3787 | SkSL::Program::Settings settings; |
| 3788 | settings.fCaps = this->caps()->shaderCaps(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3789 | SkSL::String glsl; |
| 3790 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3791 | &str, &length, 1, settings, &glsl); |
| 3792 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, |
| 3793 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3794 | &fStats, settings); |
| 3795 | SkASSERT(program->fInputs.isEmpty()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3796 | |
| 3797 | str = fshaderTxt.c_str(); |
| 3798 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3799 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
| 3800 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, |
| 3801 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3802 | &fStats, settings); |
| 3803 | SkASSERT(program->fInputs.isEmpty()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3804 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3805 | GL_CALL(LinkProgram(fStencilClipClearProgram)); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3806 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3807 | GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex")); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3808 | |
| 3809 | GL_CALL(DeleteShader(vshader)); |
| 3810 | GL_CALL(DeleteShader(fshader)); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3811 | |
| 3812 | return true; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3813 | } |
| 3814 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3815 | void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3816 | GrRenderTarget* rt, GrSurfaceOrigin origin) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3817 | // TODO: This should swizzle the output to match dst's config, though it is a debugging |
| 3818 | // visualization. |
| 3819 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3820 | this->handleDirtyContext(); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3821 | if (!fStencilClipClearProgram) { |
| 3822 | if (!this->createStencilClipClearProgram()) { |
| 3823 | SkDebugf("Failed to create stencil clip clear program.\n"); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3824 | return; |
| 3825 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3826 | } |
| 3827 | |
Mike Klein | 8b06ed7 | 2017-06-06 21:47:32 +0000 | [diff] [blame] | 3828 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3829 | this->flushRenderTarget(glRT); |
Mike Klein | 8b06ed7 | 2017-06-06 21:47:32 +0000 | [diff] [blame] | 3830 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3831 | GL_CALL(UseProgram(fStencilClipClearProgram)); |
| 3832 | fHWProgramID = fStencilClipClearProgram; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3833 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3834 | fHWVertexArrayState.setVertexArrayID(this, 0); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3835 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3836 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 3837 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 3838 | attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 3839 | 2 * sizeof(GrGLfloat), 0); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3840 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3841 | GrXferProcessor::BlendInfo blendInfo; |
| 3842 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3843 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3844 | this->flushColorWrite(false); |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 3845 | this->flushHWAAState(glRT, false, false); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3846 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
| 3847 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3848 | GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment(); |
| 3849 | // This should only be called internally when we know we have a stencil buffer. |
| 3850 | SkASSERT(sb); |
| 3851 | GrStencilSettings settings = GrStencilSettings( |
| 3852 | *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits()); |
| 3853 | this->flushStencil(settings); |
| 3854 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3855 | } |
| 3856 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3857 | bool GrGLGpu::createClearColorProgram() { |
| 3858 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 3859 | |
| 3860 | if (!fClearProgramArrayBuffer) { |
| 3861 | static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1}; |
| 3862 | fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType, |
| 3863 | kStatic_GrAccessPattern, vdata)); |
| 3864 | if (!fClearProgramArrayBuffer) { |
| 3865 | return false; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | SkASSERT(!fClearColorProgram.fProgram); |
| 3870 | GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram()); |
| 3871 | if (!fClearColorProgram.fProgram) { |
| 3872 | return false; |
| 3873 | } |
| 3874 | |
| 3875 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3876 | const char* version = this->caps()->shaderCaps()->versionDeclString(); |
| 3877 | |
| 3878 | SkString vshaderTxt(version); |
| 3879 | aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt); |
| 3880 | vshaderTxt.append(";"); |
| 3881 | vshaderTxt.append(R"( |
| 3882 | // Clear Color Program VS |
| 3883 | void main() { |
| 3884 | sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1); |
| 3885 | })"); |
| 3886 | |
| 3887 | GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier); |
| 3888 | SkString fshaderTxt(version); |
| 3889 | uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt); |
| 3890 | fshaderTxt.append(";"); |
| 3891 | fshaderTxt.appendf(R"( |
| 3892 | // Clear Color Program FS |
| 3893 | void main() { |
| 3894 | sk_FragColor = u_color; |
| 3895 | })"); |
| 3896 | |
| 3897 | const char* str; |
| 3898 | GrGLint length; |
| 3899 | |
| 3900 | str = vshaderTxt.c_str(); |
| 3901 | length = SkToInt(vshaderTxt.size()); |
| 3902 | SkSL::Program::Settings settings; |
| 3903 | settings.fCaps = this->caps()->shaderCaps(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3904 | SkSL::String glsl; |
| 3905 | GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, &str, &length, 1, settings, &glsl); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3906 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3907 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3908 | &fStats, settings); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3909 | |
| 3910 | str = fshaderTxt.c_str(); |
| 3911 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3912 | GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3913 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3914 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3915 | &fStats, settings); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3916 | |
| 3917 | GL_CALL(LinkProgram(fClearColorProgram.fProgram)); |
| 3918 | |
| 3919 | GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex")); |
| 3920 | |
| 3921 | GL_CALL_RET(fClearColorProgram.fColorUniform, |
| 3922 | GetUniformLocation(fClearColorProgram.fProgram, "u_color")); |
| 3923 | |
| 3924 | GL_CALL(DeleteShader(vshader)); |
| 3925 | GL_CALL(DeleteShader(fshader)); |
| 3926 | |
| 3927 | return true; |
| 3928 | } |
| 3929 | |
| 3930 | void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b, |
| 3931 | GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) { |
| 3932 | if (!fClearColorProgram.fProgram) { |
| 3933 | if (!this->createClearColorProgram()) { |
| 3934 | SkDebugf("Failed to create clear color program.\n"); |
| 3935 | return; |
| 3936 | } |
| 3937 | } |
| 3938 | |
| 3939 | GrGLIRect dstVP; |
| 3940 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
| 3941 | this->flushViewport(dstVP); |
| 3942 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
| 3943 | |
| 3944 | GL_CALL(UseProgram(fClearColorProgram.fProgram)); |
| 3945 | fHWProgramID = fClearColorProgram.fProgram; |
| 3946 | |
| 3947 | fHWVertexArrayState.setVertexArrayID(this, 0); |
| 3948 | |
| 3949 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
| 3950 | attribs->enableVertexArrays(this, 1); |
| 3951 | attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
| 3952 | 2 * sizeof(GrGLfloat), 0); |
| 3953 | |
| 3954 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst); |
| 3955 | this->flushScissor(clip.scissorState(), glrt->getViewport(), origin); |
| 3956 | this->flushWindowRectangles(clip.windowRectsState(), glrt, origin); |
| 3957 | |
| 3958 | GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a)); |
| 3959 | |
| 3960 | GrXferProcessor::BlendInfo blendInfo; |
| 3961 | blendInfo.reset(); |
| 3962 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
| 3963 | this->flushColorWrite(true); |
| 3964 | this->flushHWAAState(nullptr, false, false); |
| 3965 | this->disableStencil(); |
| 3966 | if (this->glCaps().srgbWriteControl()) { |
| 3967 | this->flushFramebufferSRGB(true); |
| 3968 | } |
| 3969 | |
| 3970 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 3971 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3972 | this->didWriteToSurface(dst, origin, clip.scissorEnabled() ? &clip.scissorRect() : nullptr); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3973 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3974 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3975 | bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3976 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3977 | const SkIRect& srcRect, |
| 3978 | const SkIPoint& dstPoint) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3979 | GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture()); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3980 | int progIdx = TextureToCopyProgramIdx(srcTex); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3981 | |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 3982 | if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) { |
| 3983 | return false; |
| 3984 | } |
| 3985 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3986 | if (!fCopyPrograms[progIdx].fProgram) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3987 | if (!this->createCopyProgram(srcTex)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3988 | SkDebugf("Failed to create copy program.\n"); |
| 3989 | return false; |
| 3990 | } |
| 3991 | } |
| 3992 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3993 | int w = srcRect.width(); |
| 3994 | int h = srcRect.height(); |
| 3995 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3996 | this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3997 | |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3998 | GrGLIRect dstVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3999 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4000 | this->flushViewport(dstVP); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4001 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4002 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4003 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4004 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 4005 | GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram)); |
| 4006 | fHWProgramID = fCopyPrograms[progIdx].fProgram; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4007 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4008 | fHWVertexArrayState.setVertexArrayID(this, 0); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4009 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4010 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 4011 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 4012 | attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 4013 | 2 * sizeof(GrGLfloat), 0); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4014 | |
| 4015 | // dst rect edges in NDC (-1 to 1) |
| 4016 | int dw = dst->width(); |
| 4017 | int dh = dst->height(); |
| 4018 | GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f; |
| 4019 | GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f; |
| 4020 | GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f; |
| 4021 | GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4022 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4023 | dy0 = -dy0; |
| 4024 | dy1 = -dy1; |
| 4025 | } |
| 4026 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 4027 | GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft; |
| 4028 | GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w); |
| 4029 | GrGLfloat sy0 = (GrGLfloat)srcRect.fTop; |
| 4030 | GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h); |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 4031 | int sw = src->width(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4032 | int sh = src->height(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4033 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 4034 | sy0 = sh - sy0; |
| 4035 | sy1 = sh - sy1; |
| 4036 | } |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 4037 | // src rect edges in normalized texture space (0 to 1) |
| 4038 | sx0 /= sw; |
| 4039 | sx1 /= sw; |
| 4040 | sy0 /= sh; |
| 4041 | sy1 /= sh; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4042 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 4043 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0)); |
| 4044 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 4045 | sx1 - sx0, sy1 - sy0, sx0, sy0)); |
| 4046 | GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4047 | |
| 4048 | GrXferProcessor::BlendInfo blendInfo; |
| 4049 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 4050 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4051 | this->flushColorWrite(true); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4052 | this->flushHWAAState(nullptr, false, false); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4053 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4054 | this->disableWindowRectangles(); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 4055 | this->disableStencil(); |
Brian Osman | f02fa6f | 2017-07-11 11:17:47 -0400 | [diff] [blame] | 4056 | if (this->glCaps().srgbWriteControl()) { |
| 4057 | this->flushFramebufferSRGB(true); |
| 4058 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4059 | |
| 4060 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4061 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4062 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4063 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4064 | return true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4067 | void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 4068 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4069 | const SkIRect& srcRect, |
| 4070 | const SkIPoint& dstPoint) { |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 4071 | SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4072 | GrGLIRect srcVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4073 | this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4074 | GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4075 | SkASSERT(dstTex); |
| 4076 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4077 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4078 | GrGLIRect srcGLRect; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4079 | srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4080 | |
| 4081 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 4082 | GL_CALL(BindTexture(dstTex->target(), dstTex->textureID())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4083 | GrGLint dstY; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4084 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4085 | dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight); |
| 4086 | } else { |
| 4087 | dstY = dstPoint.fY; |
| 4088 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 4089 | GL_CALL(CopyTexSubImage2D(dstTex->target(), 0, |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4090 | dstPoint.fX, dstY, |
| 4091 | srcGLRect.fLeft, srcGLRect.fBottom, |
| 4092 | srcGLRect.fWidth, srcGLRect.fHeight)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4093 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4094 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 4095 | srcRect.width(), srcRect.height()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4096 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4097 | } |
| 4098 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4099 | bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 4100 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4101 | const SkIRect& srcRect, |
| 4102 | const SkIPoint& dstPoint) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4103 | SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin, |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 4104 | srcRect, dstPoint, this->glCaps())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4105 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 4106 | srcRect.width(), srcRect.height()); |
| 4107 | if (dst == src) { |
| 4108 | if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) { |
| 4109 | return false; |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 4110 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 4111 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4112 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4113 | GrGLIRect dstVP; |
| 4114 | GrGLIRect srcVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4115 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
| 4116 | this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4117 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4118 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4119 | GrGLIRect srcGLRect; |
| 4120 | GrGLIRect dstGLRect; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4121 | srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin); |
| 4122 | dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4123 | |
| 4124 | // BlitFrameBuffer respects the scissor, so disable it. |
| 4125 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4126 | this->disableWindowRectangles(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4127 | |
| 4128 | GrGLint srcY0; |
| 4129 | GrGLint srcY1; |
| 4130 | // Does the blit need to y-mirror or not? |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4131 | if (srcOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4132 | srcY0 = srcGLRect.fBottom; |
| 4133 | srcY1 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 4134 | } else { |
| 4135 | srcY0 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 4136 | srcY1 = srcGLRect.fBottom; |
| 4137 | } |
| 4138 | GL_CALL(BlitFramebuffer(srcGLRect.fLeft, |
| 4139 | srcY0, |
| 4140 | srcGLRect.fLeft + srcGLRect.fWidth, |
| 4141 | srcY1, |
| 4142 | dstGLRect.fLeft, |
| 4143 | dstGLRect.fBottom, |
| 4144 | dstGLRect.fLeft + dstGLRect.fWidth, |
| 4145 | dstGLRect.fBottom + dstGLRect.fHeight, |
| 4146 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4147 | this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst); |
| 4148 | this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4149 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4150 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4153 | // Manual implementation of mipmap generation, to work around driver bugs w/sRGB. |
| 4154 | // Uses draw calls to do a series of downsample operations to successive mips. |
| 4155 | // If this returns false, then the calling code falls back to using glGenerateMipmap. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4156 | bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin, |
| 4157 | bool gammaCorrect) { |
brianosman | 09563ce | 2016-06-02 08:59:34 -0700 | [diff] [blame] | 4158 | // Our iterative downsample requires the ability to limit which level we're sampling: |
| 4159 | if (!this->glCaps().doManualMipmapping()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4160 | return false; |
| 4161 | } |
| 4162 | |
| 4163 | // Mipmaps are only supported on 2D textures: |
| 4164 | if (GR_GL_TEXTURE_2D != texture->target()) { |
| 4165 | return false; |
| 4166 | } |
| 4167 | |
| 4168 | // We need to be able to render to the texture for this to work: |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4169 | if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4170 | return false; |
| 4171 | } |
| 4172 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4173 | // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct: |
| 4174 | if (GrPixelConfigIsSRGB(texture->config())) { |
| 4175 | // If we have write-control, just set the state that we want: |
| 4176 | if (this->glCaps().srgbWriteControl()) { |
| 4177 | this->flushFramebufferSRGB(gammaCorrect); |
| 4178 | } else if (!gammaCorrect) { |
| 4179 | // If we don't have write-control we can't do non-gamma-correct mipmapping: |
| 4180 | return false; |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | int width = texture->width(); |
| 4185 | int height = texture->height(); |
| 4186 | int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1; |
| 4187 | |
| 4188 | // Define all mips, if we haven't previously done so: |
| 4189 | if (0 == texture->texturePriv().maxMipMapLevel()) { |
| 4190 | GrGLenum internalFormat; |
| 4191 | GrGLenum externalFormat; |
| 4192 | GrGLenum externalType; |
| 4193 | if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(), |
| 4194 | &internalFormat, &externalFormat, &externalType)) { |
| 4195 | return false; |
| 4196 | } |
| 4197 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 4198 | this->unbindCpuToGpuXferBuffer(); |
| 4199 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4200 | for (GrGLint level = 1; level < levelCount; ++level) { |
| 4201 | // Define the next mip: |
| 4202 | width = SkTMax(1, width / 2); |
| 4203 | height = SkTMax(1, height / 2); |
| 4204 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, |
| 4205 | width, height, 0, |
| 4206 | externalFormat, externalType, nullptr)); |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | // Create (if necessary), then bind temporary FBO: |
| 4211 | if (0 == fTempDstFBOID) { |
| 4212 | GL_CALL(GenFramebuffers(1, &fTempDstFBOID)); |
| 4213 | } |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4214 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4215 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4216 | |
| 4217 | // Bind the texture, to get things configured for filtering. |
| 4218 | // We'll be changing our base level further below: |
| 4219 | this->setTextureUnit(0); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 4220 | this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4221 | |
| 4222 | // Vertex data: |
| 4223 | if (!fMipmapProgramArrayBuffer) { |
| 4224 | static const GrGLfloat vdata[] = { |
| 4225 | 0, 0, |
| 4226 | 0, 1, |
| 4227 | 1, 0, |
| 4228 | 1, 1 |
| 4229 | }; |
| 4230 | fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), |
| 4231 | kVertex_GrBufferType, |
| 4232 | kStatic_GrAccessPattern, vdata)); |
| 4233 | } |
| 4234 | if (!fMipmapProgramArrayBuffer) { |
| 4235 | return false; |
| 4236 | } |
| 4237 | |
| 4238 | fHWVertexArrayState.setVertexArrayID(this, 0); |
| 4239 | |
| 4240 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 4241 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 4242 | attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4243 | 2 * sizeof(GrGLfloat), 0); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4244 | |
| 4245 | // Set "simple" state once: |
| 4246 | GrXferProcessor::BlendInfo blendInfo; |
| 4247 | blendInfo.reset(); |
| 4248 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
| 4249 | this->flushColorWrite(true); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4250 | this->flushHWAAState(nullptr, false, false); |
| 4251 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4252 | this->disableWindowRectangles(); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 4253 | this->disableStencil(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4254 | |
| 4255 | // Do all the blits: |
| 4256 | width = texture->width(); |
| 4257 | height = texture->height(); |
| 4258 | GrGLIRect viewport; |
| 4259 | viewport.fLeft = 0; |
| 4260 | viewport.fBottom = 0; |
| 4261 | for (GrGLint level = 1; level < levelCount; ++level) { |
| 4262 | // Get and bind the program for this particular downsample (filter shape can vary): |
| 4263 | int progIdx = TextureSizeToMipmapProgramIdx(width, height); |
| 4264 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 4265 | if (!this->createMipmapProgram(progIdx)) { |
| 4266 | SkDebugf("Failed to create mipmap program.\n"); |
| 4267 | return false; |
| 4268 | } |
| 4269 | } |
| 4270 | GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram)); |
| 4271 | fHWProgramID = fMipmapPrograms[progIdx].fProgram; |
| 4272 | |
| 4273 | // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h) |
| 4274 | const float invWidth = 1.0f / width; |
| 4275 | const float invHeight = 1.0f / height; |
| 4276 | GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 4277 | invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight)); |
| 4278 | GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0)); |
| 4279 | |
| 4280 | // Only sample from previous mip |
| 4281 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1)); |
| 4282 | |
| 4283 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 4284 | GR_GL_TEXTURE_2D, texture->textureID(), level)); |
| 4285 | |
| 4286 | width = SkTMax(1, width / 2); |
| 4287 | height = SkTMax(1, height / 2); |
| 4288 | viewport.fWidth = width; |
| 4289 | viewport.fHeight = height; |
| 4290 | this->flushViewport(viewport); |
| 4291 | |
| 4292 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 4293 | } |
| 4294 | |
| 4295 | // Unbind: |
| 4296 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 4297 | GR_GL_TEXTURE_2D, 0, 0)); |
| 4298 | |
| 4299 | return true; |
| 4300 | } |
| 4301 | |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4302 | void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) { |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 4303 | SkASSERT(type); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4304 | switch (type) { |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4305 | case kTexture_GrXferBarrierType: { |
| 4306 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt); |
Brian Osman | cfe83d1 | 2018-01-19 11:13:45 -0500 | [diff] [blame] | 4307 | SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0); |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4308 | if (glrt->textureFBOID() != glrt->renderFBOID()) { |
| 4309 | // The render target uses separate storage so no need for glTextureBarrier. |
| 4310 | // FIXME: The render target will resolve automatically when its texture is bound, |
| 4311 | // but we could resolve only the bounds that will be read if we do it here instead. |
| 4312 | return; |
| 4313 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4314 | SkASSERT(this->caps()->textureBarrierSupport()); |
| 4315 | GL_CALL(TextureBarrier()); |
| 4316 | return; |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4317 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 4318 | case kBlend_GrXferBarrierType: |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 4319 | SkASSERT(GrCaps::kAdvanced_BlendEquationSupport == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 4320 | this->caps()->blendEquationSupport()); |
| 4321 | GL_CALL(BlendBarrier()); |
| 4322 | return; |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 4323 | default: break; // placate compiler warnings that kNone not handled |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4327 | #if GR_TEST_UTILS |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 4328 | GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h, |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4329 | GrPixelConfig config, bool /*isRT*/, |
| 4330 | GrMipMapped mipMapped) { |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 4331 | this->handleDirtyContext(); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4332 | if (!this->caps()->isConfigTexturable(config)) { |
| 4333 | return GrBackendTexture(); // invalid |
| 4334 | } |
| 4335 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 4336 | if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) { |
| 4337 | return GrBackendTexture(); // invalid |
| 4338 | } |
| 4339 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4340 | // Currently we don't support uploading pixel data when mipped. |
| 4341 | if (pixels && GrMipMapped::kYes == mipMapped) { |
| 4342 | return GrBackendTexture(); // invalid |
| 4343 | } |
| 4344 | |
| 4345 | GrGLTextureInfo info; |
| 4346 | info.fTarget = GR_GL_TEXTURE_2D; |
| 4347 | info.fID = 0; |
| 4348 | GL_CALL(GenTextures(1, &info.fID)); |
| 4349 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0)); |
| 4350 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 4351 | GL_CALL(BindTexture(info.fTarget, info.fID)); |
| 4352 | fHWBoundTextureUniqueIDs[0].makeInvalid(); |
| 4353 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); |
| 4354 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 4355 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); |
| 4356 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE)); |
| 4357 | |
| 4358 | GrGLenum internalFormat; |
| 4359 | GrGLenum externalFormat; |
| 4360 | GrGLenum externalType; |
| 4361 | |
| 4362 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 4363 | &externalType)) { |
| 4364 | return GrBackendTexture(); // invalid |
| 4365 | } |
| 4366 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 4367 | info.fFormat = this->glCaps().configSizedInternalFormat(config); |
| 4368 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4369 | this->unbindCpuToGpuXferBuffer(); |
| 4370 | |
| 4371 | // Figure out the number of mip levels. |
| 4372 | int mipLevels = 1; |
| 4373 | if (GrMipMapped::kYes == mipMapped) { |
| 4374 | mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1; |
| 4375 | } |
| 4376 | |
| 4377 | size_t bpp = GrBytesPerPixel(config); |
| 4378 | size_t baseLayerSize = bpp * w * h; |
| 4379 | SkAutoMalloc defaultStorage(baseLayerSize); |
| 4380 | if (!pixels) { |
| 4381 | // Fill in the texture with all zeros so we don't have random garbage |
| 4382 | pixels = defaultStorage.get(); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 4383 | memset(defaultStorage.get(), 0, baseLayerSize); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | int width = w; |
| 4387 | int height = h; |
| 4388 | for (int i = 0; i < mipLevels; ++i) { |
| 4389 | GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat, |
| 4390 | externalType, pixels)); |
| 4391 | width = SkTMax(1, width / 2); |
| 4392 | height = SkTMax(1, height / 2); |
| 4393 | } |
| 4394 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 4395 | // unbind the texture from the texture unit to avoid asserts |
| 4396 | GL_CALL(BindTexture(info.fTarget, 0)); |
| 4397 | |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 4398 | return GrBackendTexture(w, h, mipMapped, info); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
| 4402 | SkASSERT(kOpenGL_GrBackend == tex.backend()); |
| 4403 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 4404 | GrGLTextureInfo info; |
| 4405 | if (!tex.getGLTextureInfo(&info)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4406 | return false; |
| 4407 | } |
| 4408 | |
| 4409 | GrGLboolean result; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 4410 | GL_CALL_RET(result, IsTexture(info.fID)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4411 | |
| 4412 | return (GR_GL_TRUE == result); |
| 4413 | } |
| 4414 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 4415 | void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) { |
| 4416 | SkASSERT(kOpenGL_GrBackend == tex.backend()); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4417 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 4418 | GrGLTextureInfo info; |
| 4419 | if (tex.getGLTextureInfo(&info)) { |
| 4420 | GL_CALL(DeleteTextures(1, &info.fID)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4424 | GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
| 4425 | GrColorType colorType, |
| 4426 | GrSRGBEncoded srgbEncoded) { |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 4427 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
| 4428 | return GrBackendRenderTarget(); // invalid |
| 4429 | } |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 4430 | this->handleDirtyContext(); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4431 | auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded); |
| 4432 | GrGLenum colorBufferFormat; |
| 4433 | if (!this->glCaps().getRenderbufferFormat(config, &colorBufferFormat)) { |
| 4434 | return {}; |
| 4435 | } |
| 4436 | int sFormatIdx = this->getCompatibleStencilIndex(config); |
| 4437 | if (sFormatIdx < 0) { |
| 4438 | return {}; |
| 4439 | } |
| 4440 | GrGLuint rbIDs[] = {0, 0}; |
| 4441 | GL_CALL(GenRenderbuffers(2, rbIDs)); |
| 4442 | if (!rbIDs[0] || !rbIDs[1]) { |
| 4443 | if (!rbIDs[0]) { |
| 4444 | GL_CALL(DeleteRenderbuffers(1, &rbIDs[0])); |
| 4445 | } |
| 4446 | if (!rbIDs[1]) { |
| 4447 | GL_CALL(DeleteRenderbuffers(1, &rbIDs[1])); |
| 4448 | } |
| 4449 | return {}; |
| 4450 | } |
| 4451 | GrGLFramebufferInfo info; |
| 4452 | info.fFBOID = 0; |
| 4453 | GL_CALL(GenFramebuffers(1, &info.fFBOID)); |
| 4454 | if (!info.fFBOID) { |
| 4455 | GL_CALL(DeleteRenderbuffers(2, rbIDs)); |
| 4456 | return GrBackendRenderTarget(); |
| 4457 | } |
| 4458 | |
| 4459 | this->invalidateBoundRenderTarget(); |
| 4460 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4461 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4462 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[0])); |
| 4463 | GL_ALLOC_CALL(this->glInterface(), |
| 4464 | RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h)); |
| 4465 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER, |
| 4466 | rbIDs[0])); |
| 4467 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[1])); |
| 4468 | auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat; |
| 4469 | GL_ALLOC_CALL(this->glInterface(), |
| 4470 | RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h)); |
| 4471 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, |
| 4472 | rbIDs[1])); |
| 4473 | if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) { |
| 4474 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, |
| 4475 | GR_GL_RENDERBUFFER, rbIDs[1])); |
| 4476 | } |
| 4477 | |
| 4478 | // We don't want to have to recover the renderbuffer IDs later to delete them. OpenGL has this |
| 4479 | // rule that if a renderbuffer is deleted and a FBO other than the current FBO has the RB |
| 4480 | // attached then deletion is delayed. So we unbind the FBO here and delete the renderbuffers. |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4481 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4482 | GL_CALL(DeleteRenderbuffers(2, rbIDs)); |
| 4483 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4484 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4485 | GrGLenum status; |
| 4486 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 4487 | if (GR_GL_FRAMEBUFFER_COMPLETE != status) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4488 | this->deleteFramebuffer(info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4489 | return {}; |
| 4490 | } |
| 4491 | auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits); |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 4492 | return {w, h, 1, stencilBits, config, info}; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
| 4496 | SkASSERT(kOpenGL_GrBackend == backendRT.backend()); |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 4497 | GrGLFramebufferInfo info; |
| 4498 | if (backendRT.getGLFramebufferInfo(&info)) { |
| 4499 | if (info.fFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 4500 | this->deleteFramebuffer(info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4501 | } |
| 4502 | } |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 4503 | } |
| 4504 | |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 4505 | void GrGLGpu::testingOnly_flushGpuAndSync() { |
| 4506 | GL_CALL(Finish()); |
| 4507 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4508 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 4509 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4510 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4511 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4512 | GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 4513 | const GrBuffer* ibuf) { |
robertphillips@google.com | 4f65a27 | 2013-03-26 19:40:46 +0000 | [diff] [blame] | 4514 | GrGLAttribArrayState* attribState; |
| 4515 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4516 | if (gpu->glCaps().isCoreProfile()) { |
| 4517 | if (!fCoreProfileVertexArray) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4518 | GrGLuint arrayID; |
| 4519 | GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID)); |
| 4520 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4521 | fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount); |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4522 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4523 | if (ibuf) { |
| 4524 | attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4525 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4526 | attribState = fCoreProfileVertexArray->bind(gpu); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4527 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4528 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4529 | if (ibuf) { |
| 4530 | // bindBuffer implicitly binds VAO 0 when binding an index buffer. |
| 4531 | gpu->bindBuffer(kIndex_GrBufferType, ibuf); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4532 | } else { |
| 4533 | this->setVertexArrayID(gpu, 0); |
| 4534 | } |
| 4535 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
| 4536 | if (fDefaultVertexArrayAttribState.count() != attrCount) { |
| 4537 | fDefaultVertexArrayAttribState.resize(attrCount); |
| 4538 | } |
| 4539 | attribState = &fDefaultVertexArrayAttribState; |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4540 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4541 | return attribState; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 4542 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 4543 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 4544 | void GrGLGpu::onFinishFlush(bool insertedSemaphore) { |
| 4545 | // If we inserted semaphores during the flush, we need to call GLFlush. |
| 4546 | if (insertedSemaphore) { |
| 4547 | GL_CALL(Flush()); |
| 4548 | } |
| 4549 | } |
| 4550 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4551 | GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4552 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4553 | GrGLsync sync; |
| 4554 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 4555 | GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync)); |
| 4556 | return (GrFence)sync; |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4559 | bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) { |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4560 | GrGLenum result; |
| 4561 | GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout)); |
| 4562 | return (GR_GL_CONDITION_SATISFIED == result); |
| 4563 | } |
| 4564 | |
| 4565 | void GrGLGpu::deleteFence(GrFence fence) const { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4566 | this->deleteSync((GrGLsync)fence); |
| 4567 | } |
| 4568 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4569 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4570 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4571 | return GrGLSemaphore::Make(this, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4572 | } |
| 4573 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4574 | sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 4575 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 4576 | GrWrapOwnership ownership) { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4577 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4578 | return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership); |
| 4579 | } |
| 4580 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4581 | void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4582 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get()); |
| 4583 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4584 | GrGLsync sync; |
| 4585 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 4586 | glSem->setSync(sync); |
Brian Osman | dc87c95 | 2017-04-28 13:57:38 -0400 | [diff] [blame] | 4587 | |
| 4588 | if (flush) { |
| 4589 | GL_CALL(Flush()); |
| 4590 | } |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4591 | } |
| 4592 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4593 | void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4594 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get()); |
| 4595 | |
| 4596 | GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED)); |
| 4597 | } |
| 4598 | |
| 4599 | void GrGLGpu::deleteSync(GrGLsync sync) const { |
| 4600 | GL_CALL(DeleteSync(sync)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4601 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4602 | |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 4603 | void GrGLGpu::insertEventMarker(const char* msg) { |
| 4604 | GL_CALL(InsertEventMarker(strlen(msg), msg)); |
| 4605 | } |
| 4606 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4607 | sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) { |
| 4608 | // 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] | 4609 | sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4610 | this->insertSemaphore(semaphore, true); |
| 4611 | |
| 4612 | return semaphore; |
| 4613 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4614 | |
| 4615 | int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) { |
| 4616 | switch (texture->texturePriv().samplerType()) { |
| 4617 | case kTexture2DSampler_GrSLType: |
| 4618 | return 0; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4619 | case kTexture2DRectSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4620 | return 1; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4621 | case kTextureExternalSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4622 | return 2; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4623 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 4624 | SK_ABORT("Unexpected samper type"); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4625 | return 0; |
| 4626 | } |
| 4627 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 4628 | |
| 4629 | void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const { |
| 4630 | // We are called by the base class, which has already called beginObject(). We choose to nest |
| 4631 | // all of our caps information in a named sub-object. |
| 4632 | writer->beginObject("GL GPU"); |
| 4633 | |
| 4634 | const GrGLubyte* str; |
| 4635 | GL_CALL_RET(str, GetString(GR_GL_VERSION)); |
| 4636 | writer->appendString("GL_VERSION", (const char*)(str)); |
| 4637 | GL_CALL_RET(str, GetString(GR_GL_RENDERER)); |
| 4638 | writer->appendString("GL_RENDERER", (const char*)(str)); |
| 4639 | GL_CALL_RET(str, GetString(GR_GL_VENDOR)); |
| 4640 | writer->appendString("GL_VENDOR", (const char*)(str)); |
| 4641 | GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 4642 | writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str)); |
| 4643 | |
| 4644 | writer->appendName("extensions"); |
| 4645 | glInterface()->fExtensions.dumpJSON(writer); |
| 4646 | |
| 4647 | writer->endObject(); |
| 4648 | } |