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) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 260 | GL_CALL(DeleteFramebuffers(1, &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) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 263 | GL_CALL(DeleteFramebuffers(1, &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) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 266 | GL_CALL(DeleteFramebuffers(1, &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) { |
| 299 | GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID)); |
| 300 | } |
| 301 | if (fTempDstFBOID) { |
| 302 | GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID)); |
| 303 | } |
| 304 | if (fStencilClearFBOID) { |
| 305 | GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID)); |
| 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(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 464 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 465 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 466 | if (resetBits & kRenderTarget_GrGLBackendState) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 467 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 468 | fHWSRGBFramebuffer = kUnknown_TriState; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 469 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 470 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 471 | if (resetBits & kPathRendering_GrGLBackendState) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 472 | if (this->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 473 | this->glPathRendering()->resetContext(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 474 | } |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 475 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 476 | |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 477 | // we assume these values |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 478 | if (resetBits & kPixelStore_GrGLBackendState) { |
| 479 | if (this->glCaps().unpackRowLengthSupport()) { |
| 480 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 481 | } |
| 482 | if (this->glCaps().packRowLengthSupport()) { |
| 483 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 484 | } |
| 485 | if (this->glCaps().unpackFlipYSupport()) { |
| 486 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 487 | } |
| 488 | if (this->glCaps().packFlipYSupport()) { |
| 489 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 490 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 491 | } |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 492 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 493 | if (resetBits & kProgram_GrGLBackendState) { |
| 494 | fHWProgramID = 0; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 495 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 498 | static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps, |
| 499 | GrGLTexture::IDDesc* idDesc) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 500 | GrGLTextureInfo info; |
| 501 | if (!backendTex.getGLTextureInfo(&info) || !info.fID) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 502 | return false; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 503 | } |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 504 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 505 | idDesc->fInfo = info; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 506 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 507 | if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) { |
| 508 | if (!caps.shaderCaps()->externalTextureSupport()) { |
| 509 | return false; |
| 510 | } |
| 511 | } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) { |
| 512 | if (!caps.rectangleTextureSupport()) { |
| 513 | return false; |
| 514 | } |
| 515 | } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) { |
| 516 | return false; |
| 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | |
| 521 | sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 522 | GrWrapOwnership ownership) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 523 | GrGLTexture::IDDesc idDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 524 | if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) { |
| 525 | return nullptr; |
| 526 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 527 | if (!idDesc.fInfo.fFormat) { |
| 528 | idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config()); |
| 529 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 530 | if (kBorrow_GrWrapOwnership == ownership) { |
| 531 | idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
| 532 | } else { |
| 533 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
| 534 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 535 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 536 | GrSurfaceDesc surfDesc; |
| 537 | surfDesc.fFlags = kNone_GrSurfaceFlags; |
| 538 | surfDesc.fWidth = backendTex.width(); |
| 539 | surfDesc.fHeight = backendTex.height(); |
| 540 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 541 | surfDesc.fSampleCnt = 1; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 542 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 543 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid |
| 544 | : GrMipMapsStatus::kNotAllocated; |
| 545 | |
| 546 | return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 550 | int sampleCnt, |
| 551 | GrWrapOwnership ownership) { |
| 552 | GrGLTexture::IDDesc idDesc; |
| 553 | if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 554 | return nullptr; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 555 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 556 | if (!idDesc.fInfo.fFormat) { |
| 557 | idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config()); |
| 558 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 559 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 560 | // We don't support rendering to a EXTERNAL texture. |
| 561 | if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) { |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 562 | return nullptr; |
| 563 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 564 | |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 565 | if (kBorrow_GrWrapOwnership == ownership) { |
Brian Osman | a6953f2 | 2017-03-10 20:14:05 +0000 | [diff] [blame] | 566 | idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 567 | } else { |
| 568 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 569 | } |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 570 | |
Ben Wagner | 18b61f9 | 2016-10-25 10:44:02 -0400 | [diff] [blame] | 571 | GrSurfaceDesc surfDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 572 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 573 | surfDesc.fWidth = backendTex.width(); |
| 574 | surfDesc.fHeight = backendTex.height(); |
| 575 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 576 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config()); |
| 577 | if (surfDesc.fSampleCnt < 1) { |
| 578 | return nullptr; |
| 579 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 580 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 581 | GrGLRenderTarget::IDDesc rtIDDesc; |
| 582 | if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) { |
| 583 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 584 | } |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 585 | |
| 586 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty |
| 587 | : GrMipMapsStatus::kNotAllocated; |
| 588 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 589 | sk_sp<GrGLTextureRenderTarget> texRT( |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 590 | GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 591 | texRT->baseLevelWasBoundToFBO(); |
| 592 | return std::move(texRT); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 595 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 596 | const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo(); |
| 597 | if (!info) { |
| 598 | return nullptr; |
| 599 | } |
| 600 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 601 | GrGLRenderTarget::IDDesc idDesc; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 602 | idDesc.fRTFBOID = info->fFBOID; |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 603 | idDesc.fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 604 | idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
Brian Osman | 0b791f5 | 2017-03-10 08:30:22 -0500 | [diff] [blame] | 605 | idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed; |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 606 | idDesc.fIsMixedSampled = false; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 607 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 608 | GrSurfaceDesc desc; |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 609 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 610 | desc.fWidth = backendRT.width(); |
| 611 | desc.fHeight = backendRT.height(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 612 | desc.fConfig = backendRT.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 613 | desc.fSampleCnt = |
| 614 | this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config()); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 615 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 616 | return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits()); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 619 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 620 | int sampleCnt) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 621 | GrGLTextureInfo info; |
| 622 | if (!tex.getGLTextureInfo(&info) || !info.fID) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 623 | return nullptr; |
| 624 | } |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 625 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 626 | if (GR_GL_TEXTURE_RECTANGLE != info.fTarget && |
| 627 | GR_GL_TEXTURE_2D != info.fTarget) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 628 | // Only texture rectangle and texture 2d are supported. We do not check whether texture |
| 629 | // rectangle is supported by Skia - if the caller provided us with a texture rectangle, |
| 630 | // we assume the necessary support exists. |
| 631 | return nullptr; |
| 632 | } |
| 633 | |
Ben Wagner | 18b61f9 | 2016-10-25 10:44:02 -0400 | [diff] [blame] | 634 | GrSurfaceDesc surfDesc; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 635 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 636 | surfDesc.fWidth = tex.width(); |
| 637 | surfDesc.fHeight = tex.height(); |
| 638 | surfDesc.fConfig = tex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 639 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config()); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 640 | |
| 641 | GrGLRenderTarget::IDDesc rtIDDesc; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 642 | if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 643 | return nullptr; |
| 644 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 645 | return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 646 | } |
| 647 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 648 | //////////////////////////////////////////////////////////////////////////////// |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 649 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 650 | bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width, |
| 651 | int height, GrColorType srcColorType, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 652 | DrawPreference* drawPreference, |
| 653 | WritePixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 654 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 655 | auto srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(dstSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 656 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 657 | // We assume the base class has only inserted a draw for sRGB reasons. So the temp surface |
| 658 | // 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] | 659 | SkASSERT(tempDrawInfo->fWriteColorType == srcColorType); |
| 660 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == srcColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 661 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 662 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 663 | srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
| 664 | } |
| 665 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
| 666 | srcConfigSRGBEncoded = GrSRGBEncoded::kNo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 667 | } |
| 668 | |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 669 | if (SkToBool(dstSurface->asRenderTarget())) { |
| 670 | if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) { |
| 671 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture()); |
| 676 | |
| 677 | if (texture) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 678 | if (GR_GL_TEXTURE_EXTERNAL == texture->target()) { |
| 679 | // We don't currently support writing pixels to EXTERNAL textures. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 680 | return false; |
| 681 | } |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 682 | if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() && |
| 683 | this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() && |
| 684 | (width < dstSurface->width() || height < dstSurface->height())) { |
| 685 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 686 | } |
| 687 | } else { |
| 688 | // This subclass only allows writes to textures. If the dst is not a texture we have to draw |
| 689 | // into it. We could use glDrawPixels on GLs that have it, but we don't today. |
| 690 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 693 | // 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] | 694 | if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 1) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 695 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 696 | } |
| 697 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 698 | auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srcConfigSRGBEncoded); |
| 699 | SkASSERT(srcAsConfig != kUnknown_GrPixelConfig); |
| 700 | auto dstColorType = GrPixelConfigToColorType(dstSurface->config()); |
| 701 | bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcAsConfig) == dstSurface->config(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 702 | |
| 703 | if (configsAreRBSwaps) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 704 | if (!this->caps()->isConfigTexturable(srcAsConfig)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 705 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 706 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 707 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 708 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | 88c7b98 | 2015-07-31 11:20:16 -0700 | [diff] [blame] | 709 | } else if (this->glCaps().rgba8888PixelsOpsAreSlow() && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 710 | kRGBA_8888_GrPixelConfig == srcAsConfig) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 711 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 712 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 713 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 714 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 715 | } else if (kGLES_GrGLStandard == this->glStandard() && |
| 716 | this->glCaps().bgraIsInternalFormat()) { |
| 717 | // The internal format and external formats must match texture uploads so we can't |
| 718 | // swizzle while uploading when BGRA is a distinct internal format. |
| 719 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 720 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 721 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 722 | tempDrawInfo->fWriteColorType = dstColorType; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 726 | if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 727 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 728 | } |
| 729 | |
| 730 | return true; |
| 731 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 732 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 733 | static bool check_write_and_transfer_input(GrGLTexture* glTex) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 734 | if (!glTex) { |
| 735 | return false; |
| 736 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 737 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 738 | // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures |
| 739 | if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 743 | return true; |
| 744 | } |
| 745 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 746 | bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 747 | int width, int height, GrColorType srcColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 748 | const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 749 | auto glTex = static_cast<GrGLTexture*>(surface->asTexture()); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 750 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 751 | if (!check_write_and_transfer_input(glTex)) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 755 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 756 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 757 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 758 | // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the |
| 759 | // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match. |
| 760 | // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know |
| 761 | // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our |
| 762 | // caps knows to make the external format be GL_RGBA. |
| 763 | auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config()); |
| 764 | auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 765 | return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), origin, |
| 766 | glTex->target(), kWrite_UploadType, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 767 | srcAsConfig, texels, mipLevelCount); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 770 | // For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType. |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 771 | static inline GrGLint config_alignment(GrPixelConfig config) { |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 772 | switch (config) { |
| 773 | case kAlpha_8_GrPixelConfig: |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 774 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 775 | case kAlpha_8_as_Red_GrPixelConfig: |
Brian Osman | 986563b | 2017-01-10 14:20:02 -0500 | [diff] [blame] | 776 | case kGray_8_GrPixelConfig: |
Greg Daniel | 7af060a | 2017-12-05 16:27:11 -0500 | [diff] [blame] | 777 | case kGray_8_as_Lum_GrPixelConfig: |
| 778 | case kGray_8_as_Red_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 779 | return 1; |
| 780 | case kRGB_565_GrPixelConfig: |
| 781 | case kRGBA_4444_GrPixelConfig: |
| 782 | case kAlpha_half_GrPixelConfig: |
Greg Daniel | ef59d87 | 2017-11-17 16:47:21 -0500 | [diff] [blame] | 783 | case kAlpha_half_as_Red_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 784 | case kRGBA_half_GrPixelConfig: |
| 785 | return 2; |
| 786 | case kRGBA_8888_GrPixelConfig: |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 787 | case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here. |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 788 | case kBGRA_8888_GrPixelConfig: |
| 789 | case kSRGBA_8888_GrPixelConfig: |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 790 | case kSBGRA_8888_GrPixelConfig: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 791 | case kRGBA_1010102_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 792 | case kRGBA_float_GrPixelConfig: |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 793 | case kRG_float_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 794 | return 4; |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 795 | case kUnknown_GrPixelConfig: |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 796 | return 0; |
| 797 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 798 | SK_ABORT("Invalid pixel config"); |
csmartdalton | 6aa0e11 | 2017-02-08 16:14:11 -0500 | [diff] [blame] | 799 | return 0; |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 802 | 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] | 803 | GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 804 | size_t rowBytes) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 805 | GrGLTexture* glTex = static_cast<GrGLTexture*>(texture); |
| 806 | GrPixelConfig texConfig = glTex->config(); |
| 807 | SkASSERT(this->caps()->isConfigTexturable(texConfig)); |
| 808 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 809 | if (!check_write_and_transfer_input(glTex)) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 810 | return false; |
| 811 | } |
| 812 | |
| 813 | if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) { |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | this->setScratchTextureUnit(); |
| 818 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
| 819 | |
| 820 | SkASSERT(!transferBuffer->isMapped()); |
| 821 | SkASSERT(!transferBuffer->isCPUBacked()); |
| 822 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer); |
| 823 | this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer); |
| 824 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 825 | SkDEBUGCODE( |
| 826 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 827 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 828 | SkASSERT(bounds.contains(subRect)); |
| 829 | ) |
| 830 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 831 | int bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 832 | const size_t trimRowBytes = width * bpp; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 833 | if (!rowBytes) { |
| 834 | rowBytes = trimRowBytes; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 835 | } |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 836 | const void* pixels = (void*)offset; |
Bruce Dawson | 71479b7 | 2017-07-05 14:30:20 -0700 | [diff] [blame] | 837 | if (width < 0 || height < 0) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 838 | return false; |
| 839 | } |
| 840 | |
| 841 | bool restoreGLRowLength = false; |
| 842 | if (trimRowBytes != rowBytes) { |
| 843 | // we should have checked for this support already |
| 844 | SkASSERT(this->glCaps().unpackRowLengthSupport()); |
| 845 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp)); |
| 846 | restoreGLRowLength = true; |
| 847 | } |
| 848 | |
| 849 | // Internal format comes from the texture desc. |
| 850 | GrGLenum internalFormat; |
| 851 | // External format and type come from the upload data. |
| 852 | GrGLenum externalFormat; |
| 853 | GrGLenum externalType; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 854 | auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo); |
| 855 | if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 856 | &externalFormat, &externalType)) { |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig))); |
| 861 | GL_CALL(TexSubImage2D(glTex->target(), |
| 862 | 0, |
| 863 | left, top, |
| 864 | width, |
| 865 | height, |
| 866 | externalFormat, externalType, |
| 867 | pixels)); |
| 868 | |
| 869 | if (restoreGLRowLength) { |
| 870 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 871 | } |
| 872 | |
| 873 | return true; |
| 874 | } |
| 875 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 876 | /** |
| 877 | * Creates storage space for the texture and fills it with texels. |
| 878 | * |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 879 | * @param config Pixel config of the texture. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 880 | * @param interface The GL interface in use. |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 881 | * @param caps The capabilities of the GL device. |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 882 | * @param internalFormat The data format used for the internal storage of the texture. May be sized. |
| 883 | * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 884 | * @param externalFormat The data format used for the external storage of the texture. |
| 885 | * @param externalType The type of the data used for the external storage of the texture. |
| 886 | * @param texels The texel data of the texture being created. |
| 887 | * @param baseWidth The width of the texture's base mipmap level |
| 888 | * @param baseHeight The height of the texture's base mipmap level |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 889 | */ |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 890 | static bool allocate_and_populate_texture(GrPixelConfig config, |
| 891 | const GrGLInterface& interface, |
| 892 | const GrGLCaps& caps, |
| 893 | GrGLenum target, |
| 894 | GrGLenum internalFormat, |
| 895 | GrGLenum internalFormatForTexStorage, |
| 896 | GrGLenum externalFormat, |
| 897 | GrGLenum externalType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 898 | const GrMipLevel texels[], int mipLevelCount, |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 899 | int baseWidth, int baseHeight) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 900 | CLEAR_ERROR_BEFORE_ALLOC(&interface); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 901 | |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 902 | bool useTexStorage = caps.isConfigTexSupportEnabled(config); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 903 | // We can only use TexStorage if we know we will not later change the storage requirements. |
| 904 | // This means if we may later want to add mipmaps, we cannot use TexStorage. |
| 905 | // Right now, we cannot know if we will later add mipmaps or not. |
| 906 | // 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] | 907 | // mipmaps. |
| 908 | useTexStorage &= mipLevelCount > 1; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 909 | |
| 910 | if (useTexStorage) { |
| 911 | // We never resize or change formats of textures. |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 912 | GL_ALLOC_CALL(&interface, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 913 | TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage, |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 914 | baseWidth, baseHeight)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 915 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 916 | if (error != GR_GL_NO_ERROR) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 917 | return false; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 918 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 919 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 920 | const void* currentMipData = texels[currentMipLevel].fPixels; |
| 921 | if (currentMipData == nullptr) { |
| 922 | continue; |
| 923 | } |
| 924 | int twoToTheMipLevel = 1 << currentMipLevel; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 925 | int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel); |
| 926 | int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 927 | |
| 928 | GR_GL_CALL(&interface, |
| 929 | TexSubImage2D(target, |
| 930 | currentMipLevel, |
| 931 | 0, // left |
| 932 | 0, // top |
| 933 | currentWidth, |
| 934 | currentHeight, |
| 935 | externalFormat, externalType, |
| 936 | currentMipData)); |
| 937 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 938 | return true; |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 939 | } |
| 940 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 941 | if (!mipLevelCount) { |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 942 | GL_ALLOC_CALL(&interface, |
| 943 | TexImage2D(target, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 944 | 0, |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 945 | internalFormat, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 946 | baseWidth, |
| 947 | baseHeight, |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 948 | 0, // border |
| 949 | externalFormat, externalType, |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 950 | nullptr)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 951 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 952 | if (error != GR_GL_NO_ERROR) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 953 | return false; |
| 954 | } |
| 955 | } else { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 956 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 957 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 958 | int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel); |
| 959 | int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel); |
Greg Daniel | 8b059bd | 2017-09-28 20:46:45 +0000 | [diff] [blame] | 960 | const void* currentMipData = texels[currentMipLevel].fPixels; |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 961 | // Even if curremtMipData is nullptr, continue to call TexImage2D. |
| 962 | // This will allocate texture memory which we can later populate. |
| 963 | GL_ALLOC_CALL(&interface, |
| 964 | TexImage2D(target, |
| 965 | currentMipLevel, |
| 966 | internalFormat, |
| 967 | currentWidth, |
| 968 | currentHeight, |
| 969 | 0, // border |
| 970 | externalFormat, externalType, |
| 971 | currentMipData)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 972 | GrGLenum error = CHECK_ALLOC_ERROR(&interface); |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 973 | if (error != GR_GL_NO_ERROR) { |
| 974 | return false; |
| 975 | } |
cblume | 790d513 | 2016-02-29 11:13:29 -0800 | [diff] [blame] | 976 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 979 | return true; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 983 | * After a texture is created, any state which was altered during its creation |
| 984 | * needs to be restored. |
| 985 | * |
| 986 | * @param interface The GL interface to use. |
| 987 | * @param caps The capabilities of the GL device. |
| 988 | * @param restoreGLRowLength Should the row length unpacking be restored? |
| 989 | * @param glFlipY Did GL flip the texture vertically? |
| 990 | */ |
| 991 | static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps, |
| 992 | bool restoreGLRowLength, bool glFlipY) { |
| 993 | if (restoreGLRowLength) { |
| 994 | SkASSERT(caps.unpackRowLengthSupport()); |
| 995 | GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 996 | } |
| 997 | if (glFlipY) { |
| 998 | GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 999 | } |
| 1000 | } |
| 1001 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1002 | void GrGLGpu::unbindCpuToGpuXferBuffer() { |
| 1003 | auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType]; |
| 1004 | if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) { |
| 1005 | GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0)); |
| 1006 | xferBufferState.invalidate(); |
| 1007 | } |
| 1008 | |
| 1009 | } |
| 1010 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1011 | // TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to |
| 1012 | // convert from GrColorType to externalFormat/externalType GLenum values. |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1013 | bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, |
| 1014 | GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType, |
| 1015 | int left, int top, int width, int height, GrPixelConfig dataConfig, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1016 | const GrMipLevel texels[], int mipLevelCount, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1017 | GrMipMapsStatus* mipMapsStatus) { |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1018 | SkASSERT(this->caps()->isConfigTexturable(texConfig)); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1019 | SkDEBUGCODE( |
| 1020 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 1021 | SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight); |
| 1022 | SkASSERT(bounds.contains(subRect)); |
| 1023 | ) |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1024 | SkASSERT(1 == mipLevelCount || |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1025 | (0 == left && 0 == top && width == texWidth && height == texHeight)); |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1026 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1027 | this->unbindCpuToGpuXferBuffer(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1028 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1029 | // texels is const. |
| 1030 | // But we may need to flip the texture vertically to prepare it. |
| 1031 | // Rather than flip in place and alter the incoming data, |
| 1032 | // we allocate a new buffer to flip into. |
| 1033 | // 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] | 1034 | SkAutoTMalloc<GrMipLevel> texelsShallowCopy; |
| 1035 | |
| 1036 | if (mipLevelCount) { |
| 1037 | texelsShallowCopy.reset(mipLevelCount); |
| 1038 | memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel)); |
| 1039 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1040 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1041 | const GrGLInterface* interface = this->glInterface(); |
| 1042 | const GrGLCaps& caps = this->glCaps(); |
| 1043 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1044 | size_t bpp = GrBytesPerPixel(dataConfig); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1045 | |
| 1046 | if (width == 0 || height == 0) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 1047 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1048 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1049 | |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1050 | // Internal format comes from the texture desc. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1051 | GrGLenum internalFormat; |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1052 | // External format and type come from the upload data. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1053 | GrGLenum externalFormat; |
| 1054 | GrGLenum externalType; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1055 | if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat, |
| 1056 | &externalType)) { |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 1059 | // TexStorage requires a sized format, and internalFormat may or may not be |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1060 | GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig); |
brianosman | 81a8485 | 2016-09-12 09:05:14 -0700 | [diff] [blame] | 1061 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1062 | /* |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 1063 | * Check whether to allocate a temporary buffer for flipping y or |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1064 | * because our srcData has extra bytes past each row. If so, we need |
| 1065 | * to trim those off here, since GL ES may not let us specify |
| 1066 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1067 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1068 | bool restoreGLRowLength = false; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 1069 | bool swFlipY = false; |
| 1070 | bool glFlipY = false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1071 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1072 | if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1073 | if (caps.unpackFlipYSupport()) { |
| 1074 | glFlipY = true; |
| 1075 | } else { |
| 1076 | swFlipY = true; |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1077 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // in case we need a temporary, trimmed copy of the src pixels |
| 1081 | SkAutoSMalloc<128 * 128> tempStorage; |
| 1082 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1083 | if (mipMapsStatus) { |
| 1084 | *mipMapsStatus = GrMipMapsStatus::kValid; |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1087 | const bool usesMips = mipLevelCount > 1; |
| 1088 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1089 | // find the combined size of all the mip levels and the relative offset of |
| 1090 | // each into the collective buffer |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1091 | bool willNeedData = false; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1092 | size_t combinedBufferSize = 0; |
| 1093 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1094 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1095 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 1096 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1097 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1098 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1099 | const size_t trimRowBytes = currentWidth * bpp; |
| 1100 | const size_t trimmedSize = trimRowBytes * currentHeight; |
| 1101 | |
| 1102 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 1103 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 1104 | : trimRowBytes; |
| 1105 | |
| 1106 | |
| 1107 | if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes) || |
| 1108 | swFlipY) { |
| 1109 | willNeedData = true; |
| 1110 | } |
| 1111 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1112 | individualMipOffsets.push_back(combinedBufferSize); |
| 1113 | combinedBufferSize += trimmedSize; |
| 1114 | } else { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1115 | if (mipMapsStatus) { |
| 1116 | *mipMapsStatus = GrMipMapsStatus::kDirty; |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1117 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1118 | individualMipOffsets.push_back(0); |
| 1119 | } |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1120 | } |
| 1121 | if (mipMapsStatus && mipLevelCount <= 1) { |
| 1122 | *mipMapsStatus = GrMipMapsStatus::kNotAllocated; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1123 | } |
Robert Phillips | 931f751 | 2017-11-02 12:57:18 -0400 | [diff] [blame] | 1124 | char* buffer = nullptr; |
| 1125 | if (willNeedData) { |
| 1126 | buffer = (char*)tempStorage.reset(combinedBufferSize); |
| 1127 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1128 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1129 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1130 | if (!texelsShallowCopy[currentMipLevel].fPixels) { |
| 1131 | continue; |
| 1132 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1133 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1134 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1135 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
| 1136 | const size_t trimRowBytes = currentWidth * bpp; |
| 1137 | |
| 1138 | /* |
| 1139 | * check whether to allocate a temporary buffer for flipping y or |
| 1140 | * because our srcData has extra bytes past each row. If so, we need |
| 1141 | * to trim those off here, since GL ES may not let us specify |
| 1142 | * GL_UNPACK_ROW_LENGTH. |
| 1143 | */ |
| 1144 | restoreGLRowLength = false; |
| 1145 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1146 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 1147 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 1148 | : trimRowBytes; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 1149 | |
ericrk | 154349b | 2016-05-10 14:36:53 -0700 | [diff] [blame] | 1150 | // TODO: This optimization should be enabled with or without mips. |
| 1151 | // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per |
| 1152 | // mip level, before calling glTexImage2D. |
ericrk | 154349b | 2016-05-10 14:36:53 -0700 | [diff] [blame] | 1153 | if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1154 | // can't use this for flipping, only non-neg values allowed. :( |
| 1155 | if (rowBytes != trimRowBytes) { |
| 1156 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1157 | GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1158 | restoreGLRowLength = true; |
| 1159 | } |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1160 | } else if (trimRowBytes != rowBytes || swFlipY) { |
| 1161 | // copy data into our new storage, skipping the trailing bytes |
| 1162 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
| 1163 | if (swFlipY && currentHeight >= 1) { |
| 1164 | src += (currentHeight - 1) * rowBytes; |
Jim Van Verth | f7037d0 | 2017-06-21 10:52:43 -0400 | [diff] [blame] | 1165 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1166 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1167 | for (int y = 0; y < currentHeight; y++) { |
| 1168 | memcpy(dst, src, trimRowBytes); |
| 1169 | if (swFlipY) { |
| 1170 | src -= rowBytes; |
| 1171 | } else { |
| 1172 | src += rowBytes; |
| 1173 | } |
| 1174 | dst += trimRowBytes; |
| 1175 | } |
| 1176 | // now point data to our copied version |
| 1177 | texelsShallowCopy[currentMipLevel].fPixels = buffer + |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1178 | individualMipOffsets[currentMipLevel]; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 1179 | texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1180 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1183 | if (mipLevelCount) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1184 | if (glFlipY) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1185 | 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] | 1186 | } |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1187 | 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] | 1188 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1189 | |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1190 | bool succeeded = true; |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1191 | if (kNewTexture_UploadType == uploadType) { |
| 1192 | if (0 == left && 0 == top && texWidth == width && texHeight == height) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 1193 | succeeded = allocate_and_populate_texture( |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1194 | texConfig, *interface, caps, target, internalFormat, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1195 | internalFormatForTexStorage, externalFormat, externalType, |
| 1196 | texelsShallowCopy, mipLevelCount, width, height); |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1197 | } else { |
| 1198 | succeeded = false; |
| 1199 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1200 | } else { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 1201 | if (swFlipY || glFlipY) { |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1202 | top = texHeight - (top + height); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1203 | } |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1204 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 1205 | if (!texelsShallowCopy[currentMipLevel].fPixels) { |
| 1206 | continue; |
| 1207 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1208 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1209 | int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1210 | int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1211 | |
| 1212 | GL_CALL(TexSubImage2D(target, |
| 1213 | currentMipLevel, |
| 1214 | left, top, |
| 1215 | currentWidth, |
| 1216 | currentHeight, |
| 1217 | externalFormat, externalType, |
| 1218 | texelsShallowCopy[currentMipLevel].fPixels)); |
| 1219 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1222 | restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY); |
| 1223 | |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1224 | return succeeded; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1227 | static bool renderbuffer_storage_msaa(const GrGLContext& ctx, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1228 | int sampleCount, |
| 1229 | GrGLenum format, |
| 1230 | int width, int height) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 1231 | CLEAR_ERROR_BEFORE_ALLOC(ctx.interface()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1232 | SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1233 | switch (ctx.caps()->msFBOType()) { |
Brian Salomon | 00731b4 | 2016-10-14 11:30:51 -0400 | [diff] [blame] | 1234 | case GrGLCaps::kStandard_MSFBOType: |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1235 | case GrGLCaps::kMixedSamples_MSFBOType: |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1236 | GL_ALLOC_CALL(ctx.interface(), |
| 1237 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 1238 | sampleCount, |
| 1239 | format, |
| 1240 | width, height)); |
| 1241 | break; |
| 1242 | case GrGLCaps::kES_Apple_MSFBOType: |
| 1243 | GL_ALLOC_CALL(ctx.interface(), |
| 1244 | RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER, |
| 1245 | sampleCount, |
| 1246 | format, |
| 1247 | width, height)); |
| 1248 | break; |
| 1249 | case GrGLCaps::kES_EXT_MsToTexture_MSFBOType: |
| 1250 | case GrGLCaps::kES_IMG_MsToTexture_MSFBOType: |
| 1251 | GL_ALLOC_CALL(ctx.interface(), |
| 1252 | RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER, |
| 1253 | sampleCount, |
| 1254 | format, |
| 1255 | width, height)); |
| 1256 | break; |
| 1257 | case GrGLCaps::kNone_MSFBOType: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 1258 | 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] | 1259 | break; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1260 | } |
Brian Salomon | 9251d4e | 2015-03-17 16:03:19 -0400 | [diff] [blame] | 1261 | return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface())); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1264 | bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1265 | const GrGLTextureInfo& texInfo, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1266 | GrGLRenderTarget::IDDesc* idDesc) { |
| 1267 | idDesc->fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1268 | idDesc->fRTFBOID = 0; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1269 | idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1270 | idDesc->fTexFBOID = 0; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1271 | SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) == |
| 1272 | this->caps()->usesMixedSamples()); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1273 | idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1274 | |
| 1275 | GrGLenum status; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1276 | |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1277 | GrGLenum colorRenderbufferFormat = 0; // suppress warning |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 1278 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1279 | if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 1280 | goto FAILED; |
| 1281 | } |
| 1282 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1283 | GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID)); |
| 1284 | if (!idDesc->fTexFBOID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1285 | goto FAILED; |
| 1286 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1287 | |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1288 | // If we are using multisampling we will create two FBOS. We render to one and then resolve to |
| 1289 | // the texture bound to the other. The exception is the IMG multisample extension. With this |
| 1290 | // extension the texture is multisampled when rendered to and then auto-resolves it when it is |
| 1291 | // rendered from. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1292 | if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1293 | GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID)); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1294 | GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1295 | if (!idDesc->fRTFBOID || |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1296 | !idDesc->fMSColorRenderbufferID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1297 | goto FAILED; |
| 1298 | } |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1299 | if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) { |
| 1300 | return false; |
| 1301 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1302 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1303 | idDesc->fRTFBOID = idDesc->fTexFBOID; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1306 | // below here we may bind the FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1307 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1308 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1309 | SkASSERT(desc.fSampleCnt > 1); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1310 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID)); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1311 | if (!renderbuffer_storage_msaa(*fGLContext, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1312 | desc.fSampleCnt, |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1313 | colorRenderbufferFormat, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1314 | desc.fWidth, desc.fHeight)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1315 | goto FAILED; |
| 1316 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1317 | fStats.incRenderTargetBinds(); |
| 1318 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID)); |
| 1319 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1320 | GR_GL_COLOR_ATTACHMENT0, |
| 1321 | GR_GL_RENDERBUFFER, |
| 1322 | idDesc->fMSColorRenderbufferID)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1323 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1324 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1325 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1326 | goto FAILED; |
| 1327 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1328 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1331 | fStats.incRenderTargetBinds(); |
| 1332 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1333 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1334 | if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1335 | GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1336 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1337 | texInfo.fTarget, |
| 1338 | texInfo.fID, 0, desc.fSampleCnt)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1339 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1340 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1341 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1342 | texInfo.fTarget, |
| 1343 | texInfo.fID, 0)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1344 | } |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1345 | if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1346 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1347 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1348 | goto FAILED; |
| 1349 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1350 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | return true; |
| 1354 | |
| 1355 | FAILED: |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1356 | if (idDesc->fMSColorRenderbufferID) { |
| 1357 | GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1358 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1359 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
| 1360 | GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1361 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1362 | if (idDesc->fTexFBOID) { |
| 1363 | GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1364 | } |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1368 | // good to set a break-point here to know when createTexture fails |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1369 | static sk_sp<GrTexture> return_null_texture() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1370 | // SkDEBUGFAIL("null texture"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1371 | return nullptr; |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 1374 | #if 0 && defined(SK_DEBUG) |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1375 | static size_t as_size_t(int x) { |
| 1376 | return x; |
| 1377 | } |
| 1378 | #endif |
| 1379 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1380 | static void set_initial_texture_params(const GrGLInterface* interface, |
| 1381 | const GrGLTextureInfo& info, |
| 1382 | GrGLTexture::TexParams* initialTexParams) { |
| 1383 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1384 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1385 | // texture that is not mipmap complete (considering the filter in use). |
| 1386 | // we only set a subset here so invalidate first |
| 1387 | initialTexParams->invalidate(); |
| 1388 | initialTexParams->fMinFilter = GR_GL_NEAREST; |
| 1389 | initialTexParams->fMagFilter = GR_GL_NEAREST; |
| 1390 | initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1391 | initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE; |
| 1392 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1393 | GR_GL_TEXTURE_MAG_FILTER, |
| 1394 | initialTexParams->fMagFilter)); |
| 1395 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1396 | GR_GL_TEXTURE_MIN_FILTER, |
| 1397 | initialTexParams->fMinFilter)); |
| 1398 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1399 | GR_GL_TEXTURE_WRAP_S, |
| 1400 | initialTexParams->fWrapS)); |
| 1401 | GR_GL_CALL(interface, TexParameteri(info.fTarget, |
| 1402 | GR_GL_TEXTURE_WRAP_T, |
| 1403 | initialTexParams->fWrapT)); |
| 1404 | } |
| 1405 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1406 | sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc, |
| 1407 | SkBudgeted budgeted, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1408 | const GrMipLevel texels[], |
| 1409 | int mipLevelCount) { |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1410 | // We fail if the MSAA was requested and is not available. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1411 | if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1412 | //SkDebugf("MSAA RT requested but not supported on this platform."); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1413 | return return_null_texture(); |
| 1414 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1415 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1416 | bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1417 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1418 | GrMipLevel zeroLevel; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1419 | std::unique_ptr<uint8_t[]> zeros; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1420 | if (performClear && !this->glCaps().clearTextureSupport() && |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 1421 | !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1422 | size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth; |
| 1423 | size_t size = rowSize * desc.fHeight; |
| 1424 | zeros.reset(new uint8_t[size]); |
| 1425 | memset(zeros.get(), 0, size); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1426 | zeroLevel.fPixels = zeros.get(); |
| 1427 | zeroLevel.fRowBytes = 0; |
| 1428 | texels = &zeroLevel; |
| 1429 | mipLevelCount = 1; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1430 | performClear = false; |
| 1431 | } |
| 1432 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 1433 | bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1434 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1435 | GrGLTexture::IDDesc idDesc; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1436 | idDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1437 | GrMipMapsStatus mipMapsStatus; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1438 | GrGLTexture::TexParams initialTexParams; |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1439 | if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels, |
| 1440 | mipLevelCount, &mipMapsStatus)) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1441 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1442 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1443 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1444 | sk_sp<GrGLTexture> tex; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 1445 | if (isRenderTarget) { |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1446 | // 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] | 1447 | GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1448 | GrGLRenderTarget::IDDesc rtIDDesc; |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1449 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 1450 | if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1451 | GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1452 | return return_null_texture(); |
| 1453 | } |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1454 | tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1455 | mipMapsStatus); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 1456 | tex->baseLevelWasBoundToFBO(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1457 | } else { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1458 | tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1459 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1460 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1461 | #ifdef TRACE_TEXTURE_CREATION |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1462 | SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n", |
brianosman | c4459f6 | 2016-07-19 08:02:20 -0700 | [diff] [blame] | 1463 | idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1464 | #endif |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1465 | if (tex && performClear) { |
| 1466 | if (this->glCaps().clearTextureSupport()) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1467 | static constexpr uint32_t kZero = 0; |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 1468 | 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] | 1469 | } else { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1470 | GrGLIRect viewport; |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1471 | this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport, |
| 1472 | kDst_TempFBOTarget); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1473 | this->disableScissor(); |
| 1474 | this->disableWindowRectangles(); |
| 1475 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 1476 | fHWWriteToColor = kYes_TriState; |
| 1477 | GL_CALL(ClearColor(0, 0, 0, 0)); |
| 1478 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1479 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get()); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1480 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
| 1481 | } |
| 1482 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1483 | return tex; |
| 1484 | } |
| 1485 | |
| 1486 | namespace { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1487 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1488 | const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1489 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1490 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1491 | GrGLStencilAttachment::Format* format) { |
sugoi@google.com | 6ba0b0f | 2013-05-03 13:52:32 +0000 | [diff] [blame] | 1492 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1493 | // we shouldn't ever know one size and not the other |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1494 | SkASSERT((kUnknownBitCount == format->fStencilBits) == |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1495 | (kUnknownBitCount == format->fTotalBits)); |
| 1496 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1497 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1498 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1499 | (GrGLint*)&format->fStencilBits); |
| 1500 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1501 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1502 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1503 | (GrGLint*)&format->fTotalBits); |
| 1504 | format->fTotalBits += format->fStencilBits; |
| 1505 | } else { |
| 1506 | format->fTotalBits = format->fStencilBits; |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1512 | int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) { |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1513 | static const int kSize = 16; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1514 | SkASSERT(this->caps()->isConfigRenderable(config)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1515 | if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) { |
| 1516 | // Default to unsupported, set this if we find a stencil format that works. |
| 1517 | int firstWorkingStencilFormatIndex = -1; |
Brian Osman | 91f9a2c | 2017-09-05 15:02:46 -0400 | [diff] [blame] | 1518 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1519 | // Create color texture |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1520 | GrGLuint colorID = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1521 | GL_CALL(GenTextures(1, &colorID)); |
| 1522 | this->setScratchTextureUnit(); |
| 1523 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID)); |
| 1524 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1525 | GR_GL_TEXTURE_MAG_FILTER, |
| 1526 | GR_GL_NEAREST)); |
| 1527 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1528 | GR_GL_TEXTURE_MIN_FILTER, |
| 1529 | GR_GL_NEAREST)); |
| 1530 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1531 | GR_GL_TEXTURE_WRAP_S, |
| 1532 | GR_GL_CLAMP_TO_EDGE)); |
| 1533 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1534 | GR_GL_TEXTURE_WRAP_T, |
| 1535 | GR_GL_CLAMP_TO_EDGE)); |
| 1536 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1537 | GrGLenum internalFormat; |
| 1538 | GrGLenum externalFormat; |
| 1539 | GrGLenum externalType; |
| 1540 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 1541 | &externalType)) { |
| 1542 | return false; |
| 1543 | } |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 1544 | this->unbindCpuToGpuXferBuffer(); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1545 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1546 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, |
bsalomon | 926cb02 | 2015-12-17 18:15:11 -0800 | [diff] [blame] | 1547 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1548 | internalFormat, |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1549 | kSize, |
| 1550 | kSize, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1551 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1552 | externalFormat, |
| 1553 | externalType, |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1554 | nullptr)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1555 | if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1556 | GL_CALL(DeleteTextures(1, &colorID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1557 | return -1; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | // unbind the texture from the texture unit before binding it to the frame buffer |
| 1561 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 1562 | |
| 1563 | // Create Framebuffer |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1564 | GrGLuint fb = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1565 | GL_CALL(GenFramebuffers(1, &fb)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1566 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb)); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1567 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1568 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 1569 | GR_GL_COLOR_ATTACHMENT0, |
| 1570 | GR_GL_TEXTURE_2D, |
| 1571 | colorID, |
| 1572 | 0)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1573 | GrGLuint sbRBID = 0; |
| 1574 | GL_CALL(GenRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1575 | |
| 1576 | // look over formats till I find a compatible one |
| 1577 | int stencilFmtCnt = this->glCaps().stencilFormats().count(); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1578 | if (sbRBID) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1579 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1580 | for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) { |
| 1581 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i]; |
| 1582 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1583 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1584 | sFmt.fInternalFormat, |
| 1585 | kSize, kSize)); |
| 1586 | if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1587 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1588 | GR_GL_STENCIL_ATTACHMENT, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1589 | GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1590 | if (sFmt.fPacked) { |
| 1591 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1592 | GR_GL_DEPTH_ATTACHMENT, |
| 1593 | GR_GL_RENDERBUFFER, sbRBID)); |
| 1594 | } else { |
| 1595 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1596 | GR_GL_DEPTH_ATTACHMENT, |
| 1597 | GR_GL_RENDERBUFFER, 0)); |
| 1598 | } |
| 1599 | GrGLenum status; |
| 1600 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1601 | if (status == GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1602 | firstWorkingStencilFormatIndex = i; |
| 1603 | break; |
| 1604 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1605 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1606 | GR_GL_STENCIL_ATTACHMENT, |
| 1607 | GR_GL_RENDERBUFFER, 0)); |
| 1608 | if (sFmt.fPacked) { |
| 1609 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1610 | GR_GL_DEPTH_ATTACHMENT, |
| 1611 | GR_GL_RENDERBUFFER, 0)); |
| 1612 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1613 | } |
| 1614 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1615 | GL_CALL(DeleteRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1616 | } |
| 1617 | GL_CALL(DeleteTextures(1, &colorID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1618 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); |
| 1619 | GL_CALL(DeleteFramebuffers(1, &fb)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1620 | fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1621 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1622 | return this->glCaps().getStencilFormatIndexForConfig(config); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1625 | bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1626 | GrGLTexture::TexParams* initialTexParams, const GrMipLevel texels[], |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1627 | int mipLevelCount, GrMipMapsStatus* mipMapsStatus) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1628 | info->fID = 0; |
| 1629 | info->fTarget = GR_GL_TEXTURE_2D; |
| 1630 | GL_CALL(GenTextures(1, &(info->fID))); |
| 1631 | |
| 1632 | if (!info->fID) { |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
| 1636 | this->setScratchTextureUnit(); |
| 1637 | GL_CALL(BindTexture(info->fTarget, info->fID)); |
| 1638 | |
| 1639 | if (renderTarget && this->glCaps().textureUsageSupport()) { |
| 1640 | // provides a hint about how this texture will be used |
| 1641 | GL_CALL(TexParameteri(info->fTarget, |
| 1642 | GR_GL_TEXTURE_USAGE, |
| 1643 | GR_GL_FRAMEBUFFER_ATTACHMENT)); |
| 1644 | } |
| 1645 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1646 | if (info) { |
| 1647 | set_initial_texture_params(this->glInterface(), *info, initialTexParams); |
| 1648 | } |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1649 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1650 | if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, kTopLeft_GrSurfaceOrigin, |
| 1651 | info->fTarget, kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, |
| 1652 | desc.fConfig, texels, mipLevelCount, mipMapsStatus)) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1653 | GL_CALL(DeleteTextures(1, &(info->fID))); |
| 1654 | return false; |
| 1655 | } |
Greg Daniel | e7d8da4 | 2017-12-04 11:23:19 -0500 | [diff] [blame] | 1656 | info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig); |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1660 | GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 1661 | int width, int height) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1662 | SkASSERT(width >= rt->width()); |
| 1663 | SkASSERT(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1664 | |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1665 | int samples = rt->numStencilSamples(); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1666 | GrGLStencilAttachment::IDDesc sbDesc; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1667 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1668 | int sIdx = this->getCompatibleStencilIndex(rt->config()); |
bsalomon | 62a627b | 2015-12-17 09:50:47 -0800 | [diff] [blame] | 1669 | if (sIdx < 0) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1670 | return nullptr; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1671 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1672 | |
| 1673 | if (!sbDesc.fRenderbufferID) { |
| 1674 | GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID)); |
| 1675 | } |
| 1676 | if (!sbDesc.fRenderbufferID) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1677 | return nullptr; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1678 | } |
| 1679 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID)); |
| 1680 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx]; |
| 1681 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1682 | // we do this "if" so that we don't call the multisample |
| 1683 | // version on a GL that doesn't have an MSAA extension. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1684 | if (samples > 1) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1685 | SkAssertResult(renderbuffer_storage_msaa(*fGLContext, |
| 1686 | samples, |
| 1687 | sFmt.fInternalFormat, |
| 1688 | width, height)); |
| 1689 | } else { |
| 1690 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1691 | sFmt.fInternalFormat, |
| 1692 | width, height)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1693 | SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1694 | } |
| 1695 | fStats.incStencilAttachmentCreates(); |
| 1696 | // After sized formats we attempt an unsized format and take |
| 1697 | // whatever sizes GL gives us. In that case we query for the size. |
| 1698 | GrGLStencilAttachment::Format format = sFmt; |
| 1699 | get_stencil_rb_sizes(this->glInterface(), &format); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1700 | GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this, |
| 1701 | sbDesc, |
| 1702 | width, |
| 1703 | height, |
| 1704 | samples, |
| 1705 | format); |
| 1706 | return stencil; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1709 | //////////////////////////////////////////////////////////////////////////////// |
| 1710 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1711 | // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer |
| 1712 | // objects are implemented as client-side-arrays on tile-deferred architectures. |
| 1713 | #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW |
| 1714 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1715 | GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType, |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 1716 | GrAccessPattern accessPattern, const void* data) { |
| 1717 | return GrGLBuffer::Create(this, size, intendedType, accessPattern, data); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1718 | } |
| 1719 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 1720 | void GrGLGpu::flushScissor(const GrScissorState& scissorState, |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1721 | const GrGLIRect& rtViewport, |
| 1722 | GrSurfaceOrigin rtOrigin) { |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 1723 | if (scissorState.enabled()) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1724 | GrGLIRect scissor; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1725 | scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1726 | // if the scissor fully contains the viewport then we fall through and |
| 1727 | // disable the scissor test. |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1728 | if (!scissor.contains(rtViewport)) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1729 | if (fHWScissorSettings.fRect != scissor) { |
| 1730 | scissor.pushToGLScissor(this->glInterface()); |
| 1731 | fHWScissorSettings.fRect = scissor; |
| 1732 | } |
| 1733 | if (kYes_TriState != fHWScissorSettings.fEnabled) { |
| 1734 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1735 | fHWScissorSettings.fEnabled = kYes_TriState; |
| 1736 | } |
| 1737 | return; |
| 1738 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1739 | } |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1740 | |
| 1741 | // See fall through note above |
| 1742 | this->disableScissor(); |
| 1743 | } |
| 1744 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1745 | void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1746 | const GrGLRenderTarget* rt, GrSurfaceOrigin origin) { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1747 | #ifndef USE_NSIGHT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1748 | typedef GrWindowRectsState::Mode Mode; |
| 1749 | SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen. |
| 1750 | SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1751 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1752 | if (!this->caps()->maxWindowRectangles() || |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1753 | fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1754 | return; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1757 | // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above |
| 1758 | // 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] | 1759 | int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows)); |
| 1760 | SkASSERT(windowState.numWindows() == numWindows); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1761 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1762 | GrGLIRect glwindows[GrWindowRectangles::kMaxWindows]; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1763 | const SkIRect* skwindows = windowState.windows().data(); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1764 | for (int i = 0; i < numWindows; ++i) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1765 | glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1768 | GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE; |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1769 | GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts())); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1770 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1771 | fHWWindowRectsState.set(origin, rt->getViewport(), windowState); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1772 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | void GrGLGpu::disableWindowRectangles() { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1776 | #ifndef USE_NSIGHT |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1777 | if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1781 | fHWWindowRectsState.setDisabled(); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1782 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 1785 | void GrGLGpu::flushMinSampleShading(float minSampleShading) { |
| 1786 | if (fHWMinSampleShading != minSampleShading) { |
| 1787 | if (minSampleShading > 0.0) { |
| 1788 | GL_CALL(Enable(GR_GL_SAMPLE_SHADING)); |
| 1789 | GL_CALL(MinSampleShading(minSampleShading)); |
| 1790 | } |
| 1791 | else { |
| 1792 | GL_CALL(Disable(GR_GL_SAMPLE_SHADING)); |
| 1793 | } |
| 1794 | fHWMinSampleShading = minSampleShading; |
| 1795 | } |
| 1796 | } |
| 1797 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 1798 | bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc, |
| 1799 | bool willDrawPoints) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 1800 | sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 1801 | if (!program) { |
| 1802 | GrCapsDebugf(this->caps(), "Failed to create program!\n"); |
| 1803 | return false; |
| 1804 | } |
| 1805 | |
| 1806 | program->generateMipmaps(primProc, pipeline); |
| 1807 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1808 | GrXferProcessor::BlendInfo blendInfo; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1809 | pipeline.getXferProcessor().getBlendInfo(&blendInfo); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1810 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1811 | this->flushColorWrite(blendInfo.fWriteColor); |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 1812 | this->flushMinSampleShading(primProc.getSampleShading()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1813 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 1814 | GrGLuint programID = program->programID(); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1815 | if (fHWProgramID != programID) { |
| 1816 | GL_CALL(UseProgram(programID)); |
| 1817 | fHWProgramID = programID; |
| 1818 | } |
| 1819 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1820 | if (blendInfo.fWriteColor) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1821 | // Swizzle the blend to match what the shader will output. |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1822 | const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle( |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 1823 | pipeline.proxy()->config()); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1824 | this->flushBlend(blendInfo, swizzle); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1825 | } |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1826 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1827 | program->setData(primProc, pipeline); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1828 | |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 1829 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 1830 | GrStencilSettings stencil; |
| 1831 | if (pipeline.isStencilEnabled()) { |
| 1832 | // TODO: attach stencil and create settings during render target flush. |
| 1833 | SkASSERT(glRT->renderTargetPriv().getStencilAttachment()); |
| 1834 | stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), |
| 1835 | glRT->renderTargetPriv().numStencilBits()); |
| 1836 | } |
| 1837 | this->flushStencil(stencil); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1838 | this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin()); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1839 | this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin()); |
csmartdalton | dabc91b | 2016-11-15 14:26:27 -0700 | [diff] [blame] | 1840 | this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1841 | |
| 1842 | // This must come after textures are flushed because a texture may need |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1843 | // to be msaa-resolved (which will modify bound FBO state). |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1844 | this->flushRenderTarget(glRT, pipeline.getDisableOutputConversionToSRGB()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1845 | |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1849 | void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc, |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1850 | const GrBuffer* indexBuffer, |
| 1851 | const GrBuffer* vertexBuffer, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1852 | int baseVertex, |
| 1853 | const GrBuffer* instanceBuffer, |
| 1854 | int baseInstance) { |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 1855 | using EnablePrimitiveRestart = GrGLAttribArrayState::EnablePrimitiveRestart; |
| 1856 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1857 | GrGLAttribArrayState* attribState; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1858 | if (indexBuffer) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1859 | SkASSERT(indexBuffer && !indexBuffer->isMapped()); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1860 | attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1861 | } else { |
| 1862 | attribState = fHWVertexArrayState.bindInternalVertexArray(this); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1863 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1864 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1865 | struct { |
| 1866 | const GrBuffer* fBuffer; |
| 1867 | int fStride; |
| 1868 | size_t fBufferOffset; |
| 1869 | } bindings[2]; |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 1870 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1871 | if (int vertexStride = primProc.getVertexStride()) { |
| 1872 | SkASSERT(vertexBuffer && !vertexBuffer->isMapped()); |
| 1873 | bindings[0].fBuffer = vertexBuffer; |
| 1874 | bindings[0].fStride = vertexStride; |
| 1875 | bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride; |
| 1876 | } |
| 1877 | if (int instanceStride = primProc.getInstanceStride()) { |
| 1878 | SkASSERT(instanceBuffer && !instanceBuffer->isMapped()); |
| 1879 | bindings[1].fBuffer = instanceBuffer; |
| 1880 | bindings[1].fStride = instanceStride; |
| 1881 | bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride; |
| 1882 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1883 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1884 | int numAttribs = primProc.numAttribs(); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 1885 | auto enableRestart = EnablePrimitiveRestart(primProc.willUsePrimitiveRestart() && indexBuffer); |
| 1886 | attribState->enableVertexArrays(this, numAttribs, enableRestart); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1887 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1888 | for (int i = 0; i < numAttribs; ++i) { |
| 1889 | using InputRate = GrPrimitiveProcessor::Attribute::InputRate; |
| 1890 | const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i); |
| 1891 | const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0; |
| 1892 | const auto& binding = bindings[divisor]; |
| 1893 | attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride, |
| 1894 | binding.fBufferOffset + attrib.fOffsetInRecord, divisor); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1898 | GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1899 | this->handleDirtyContext(); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1900 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1901 | // Index buffer state is tied to the vertex array. |
| 1902 | if (kIndex_GrBufferType == type) { |
| 1903 | this->bindVertexArray(0); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1904 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1905 | |
| 1906 | SkASSERT(type >= 0 && type <= kLast_GrBufferType); |
| 1907 | auto& bufferState = fHWBufferState[type]; |
| 1908 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 1909 | if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) { |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1910 | if (buffer->isCPUBacked()) { |
| 1911 | if (!bufferState.fBufferZeroKnownBound) { |
| 1912 | GL_CALL(BindBuffer(bufferState.fGLTarget, 0)); |
| 1913 | } |
| 1914 | } else { |
| 1915 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer); |
| 1916 | GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID())); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1917 | } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 1918 | bufferState.fBufferZeroKnownBound = buffer->isCPUBacked(); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 1919 | bufferState.fBoundBufferUniqueID = buffer->uniqueID(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | return bufferState.fGLTarget; |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1925 | void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) { |
| 1926 | if (buffer->hasAttachedToTexture()) { |
| 1927 | // 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] | 1928 | GrGpuResource::UniqueID uniqueID = buffer->uniqueID(); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 1929 | for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) { |
| 1930 | auto& buffTex = fHWBufferTextures[i]; |
| 1931 | if (uniqueID != buffTex.fAttachedBufferUniqueID) { |
| 1932 | continue; |
| 1933 | } |
| 1934 | if (i == fHWMaxUsedBufferTextureUnit) { |
| 1935 | --fHWMaxUsedBufferTextureUnit; |
| 1936 | } |
| 1937 | |
| 1938 | this->setTextureUnit(i); |
| 1939 | if (!buffTex.fKnownBound) { |
| 1940 | SkASSERT(buffTex.fTextureID); |
| 1941 | GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID)); |
| 1942 | buffTex.fKnownBound = true; |
| 1943 | } |
| 1944 | GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER, |
| 1945 | this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0)); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1950 | void GrGLGpu::disableScissor() { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1951 | if (kNo_TriState != fHWScissorSettings.fEnabled) { |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1952 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1953 | fHWScissorSettings.fEnabled = kNo_TriState; |
| 1954 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 1958 | void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, |
| 1959 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1960 | // parent class should never let us get here with no RT |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1961 | SkASSERT(target); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1962 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1963 | this->handleDirtyContext(); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1964 | |
| 1965 | GrGLfloat r, g, b, a; |
| 1966 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 1967 | a = GrColorUnpackA(color) * scale255; |
| 1968 | GrGLfloat scaleRGB = scale255; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1969 | r = GrColorUnpackR(color) * scaleRGB; |
| 1970 | g = GrColorUnpackG(color) * scaleRGB; |
| 1971 | b = GrColorUnpackB(color) * scaleRGB; |
| 1972 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1973 | if (this->glCaps().useDrawToClearColor()) { |
| 1974 | this->clearColorAsDraw(clip, r, g, b, a, target, origin); |
| 1975 | return; |
| 1976 | } |
| 1977 | |
| 1978 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
| 1979 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1980 | if (clip.scissorEnabled()) { |
| 1981 | this->flushRenderTarget(glRT, origin, clip.scissorRect()); |
| 1982 | } else { |
| 1983 | this->flushRenderTarget(glRT); |
| 1984 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1985 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
| 1986 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
| 1987 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1988 | 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] | 1989 | fHWWriteToColor = kYes_TriState; |
Brian Salomon | 028a9a5 | 2017-05-11 11:39:08 -0400 | [diff] [blame] | 1990 | |
Eric Karl | aeaf22b | 2017-05-18 15:08:09 -0700 | [diff] [blame] | 1991 | if (this->glCaps().clearToBoundaryValuesIsBroken() && |
| 1992 | (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] | 1993 | static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f); |
| 1994 | static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f); |
Eric Karl | aeaf22b | 2017-05-18 15:08:09 -0700 | [diff] [blame] | 1995 | a = (1 == a) ? safeAlpha1 : safeAlpha0; |
Brian Salomon | 028a9a5 | 2017-05-11 11:39:08 -0400 | [diff] [blame] | 1996 | } |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1997 | GL_CALL(ClearColor(r, g, b, a)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1998 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2001 | void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) { |
| 2002 | if (!target) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 2003 | return; |
| 2004 | } |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 2005 | |
| 2006 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
| 2007 | // this should only be called internally when we know we have a |
| 2008 | // stencil buffer. |
| 2009 | SkASSERT(sb); |
| 2010 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 2011 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2012 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 2013 | |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 2014 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2015 | this->disableWindowRectangles(); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 2016 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2017 | GL_CALL(StencilMask(0xffffffff)); |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2018 | GL_CALL(ClearStencil(clearValue)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2019 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 2020 | fHWStencilSettings.invalidate(); |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 2021 | if (!clearValue) { |
| 2022 | sb->cleared(); |
| 2023 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 2026 | void GrGLGpu::clearStencilClip(const GrFixedClip& clip, |
| 2027 | bool insideStencilMask, |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 2028 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2029 | SkASSERT(target); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2030 | this->handleDirtyContext(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 2031 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 2032 | if (this->glCaps().useDrawToClearStencilClip()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2033 | this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 2034 | return; |
| 2035 | } |
| 2036 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 2037 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 2038 | // this should only be called internally when we know we have a |
| 2039 | // stencil buffer. |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 2040 | SkASSERT(sb); |
| 2041 | GrGLint stencilBitCount = sb->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2042 | #if 0 |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2043 | SkASSERT(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2044 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2045 | #else |
| 2046 | // 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] | 2047 | // ANGLE a partial stencil mask will cause clears to be |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 2048 | // turned into draws. Our contract on GrOpList says that |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2049 | // changing the clip between stencil passes may or may not |
| 2050 | // 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] | 2051 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2052 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2053 | GrGLint value; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 2054 | if (insideStencilMask) { |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2055 | value = (1 << (stencilBitCount - 1)); |
| 2056 | } else { |
| 2057 | value = 0; |
| 2058 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 2059 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2060 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2061 | |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 2062 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2063 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2064 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 2065 | GL_CALL(StencilMask((uint32_t) clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2066 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2067 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 2068 | fHWStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2071 | static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps, |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2072 | int width, int height, GrPixelConfig config, |
| 2073 | size_t rowBytes) { |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2074 | // If the surface is already TopLeft, we don't need to flip. |
| 2075 | if (kTopLeft_GrSurfaceOrigin == origin) { |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 2079 | // 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] | 2080 | static const int kMinSize = 32; |
| 2081 | if (width < kMinSize || height < kMinSize) { |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 2082 | return false; |
| 2083 | } |
| 2084 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2085 | // if GL can do the flip then we'll never pay for it. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2086 | if (caps.packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2087 | return false; |
| 2088 | } |
| 2089 | |
| 2090 | // 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] | 2091 | // get the flip for free. Otherwise it costs. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2092 | // Note that we're assuming that 0 rowBytes has already been handled and that the width has been |
| 2093 | // clipped. |
| 2094 | return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes; |
| 2095 | } |
| 2096 | |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2097 | bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) { |
Brian Salomon | 625cd9e | 2016-12-15 09:35:19 -0500 | [diff] [blame] | 2098 | #ifdef SK_BUILD_FOR_MAC |
| 2099 | // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's |
| 2100 | // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer |
| 2101 | // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium |
| 2102 | // we always use a temporary surface to test for read pixels support. |
| 2103 | // https://www.crbug.com/662802 |
| 2104 | if (this->glContext().driver() == kChromium_GrGLDriver) { |
| 2105 | return this->readPixelsSupported(target->config(), readConfig); |
| 2106 | } |
| 2107 | #endif |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2108 | auto bindRenderTarget = [this, target]() -> bool { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2109 | this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target)); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2110 | return true; |
| 2111 | }; |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2112 | auto unbindRenderTarget = []{}; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2113 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 2114 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 2115 | }; |
| 2116 | GrPixelConfig rtConfig = target->config(); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2117 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget, |
| 2118 | unbindRenderTarget); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) { |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2122 | sk_sp<GrTexture> temp; |
| 2123 | auto bindRenderTarget = [this, rtConfig, &temp]() -> bool { |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 2124 | GrSurfaceDesc desc; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2125 | desc.fConfig = rtConfig; |
| 2126 | desc.fWidth = desc.fHeight = 16; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 2127 | if (this->glCaps().isConfigRenderable(rtConfig)) { |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2128 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 2129 | temp = this->createTexture(desc, SkBudgeted::kNo); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2130 | if (!temp) { |
| 2131 | return false; |
| 2132 | } |
| 2133 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2134 | this->flushRenderTargetNoColorWrites(glrt); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2135 | return true; |
| 2136 | } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 2137 | temp = this->createTexture(desc, SkBudgeted::kNo); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2138 | if (!temp) { |
| 2139 | return false; |
| 2140 | } |
| 2141 | GrGLIRect vp; |
| 2142 | this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2143 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2144 | return true; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2145 | } |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2146 | return false; |
| 2147 | }; |
| 2148 | auto unbindRenderTarget = [this, &temp]() { |
| 2149 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get()); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2150 | }; |
| 2151 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 2152 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 2153 | }; |
bsalomon | 2c3db32 | 2016-11-08 13:26:24 -0800 | [diff] [blame] | 2154 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget, |
| 2155 | unbindRenderTarget); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) { |
| 2159 | if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) { |
| 2160 | return this->readPixelsSupported(rt, readConfig); |
| 2161 | } else { |
| 2162 | GrPixelConfig config = surfaceForConfig->config(); |
| 2163 | return this->readPixelsSupported(config, readConfig); |
| 2164 | } |
| 2165 | } |
| 2166 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2167 | bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2168 | int height, size_t rowBytes, GrColorType dstColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2169 | DrawPreference* drawPreference, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2170 | ReadPixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2171 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 2172 | auto dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(srcSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2173 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 2174 | // We assume the base class has only inserted a draw for sRGB reasons. So the |
| 2175 | // the temp surface has the config of the dst data. There is no swizzling, nor dst config |
| 2176 | // spoofing. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2177 | SkASSERT(tempDrawInfo->fReadColorType == dstColorType); |
| 2178 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == dstColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2179 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2180 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 2181 | dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
| 2182 | } |
| 2183 | if (GrColorTypeIsAlphaOnly(dstColorType)) { |
| 2184 | dstConfigSRGBEncoded = GrSRGBEncoded::kNo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2185 | } |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2186 | GrPixelConfig srcConfig = srcSurface->config(); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2187 | |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 2188 | tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact |
| 2189 | : SkBackingFit::kApprox; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2190 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2191 | // TODO: Update this logic to use color type. |
| 2192 | auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, dstConfigSRGBEncoded); |
| 2193 | |
| 2194 | if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == dstAsConfig && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2195 | this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2196 | tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2197 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2198 | tempDrawInfo->fReadColorType = GrColorType::kBGRA_8888; |
bsalomon | b411b3b | 2015-07-31 09:34:24 -0700 | [diff] [blame] | 2199 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
ericrk | b4ecabd | 2016-03-11 15:18:20 -0800 | [diff] [blame] | 2200 | } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2201 | GrBytesPerPixel(dstAsConfig) == 4 && |
| 2202 | GrPixelConfigSwapRAndB(dstAsConfig) == srcConfig && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2203 | this->readPixelsSupported(srcSurface, srcConfig)) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2204 | // 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] | 2205 | // Better to do a draw with a R/B swap and then read as the original config. |
| 2206 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2207 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2208 | tempDrawInfo->fReadColorType = GrPixelConfigToColorType(srcConfig); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2209 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2210 | } else if (!this->readPixelsSupported(srcSurface, dstAsConfig)) { |
| 2211 | if (kBGRA_8888_GrPixelConfig == dstAsConfig && |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2212 | this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2213 | this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2214 | // We're trying to read BGRA but it's not supported. If RGBA is renderable and |
| 2215 | // we can read it back, then do a swizzling draw to a RGBA and read it back (which |
| 2216 | // will effectively be BGRA). |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2217 | tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 2218 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2219 | tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2220 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2221 | } else if (kSBGRA_8888_GrPixelConfig == dstAsConfig && |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2222 | this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) && |
| 2223 | this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, |
| 2224 | kSRGBA_8888_GrPixelConfig)) { |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 2225 | // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and |
| 2226 | // we can read it back, then do a swizzling draw to a sRGBA and read it back (which |
| 2227 | // will effectively be sBGRA). |
| 2228 | tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig; |
| 2229 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2230 | tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888; |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 2231 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2232 | } else if (kAlpha_8_GrPixelConfig == dstAsConfig) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2233 | // onReadPixels implements a fallback for cases where we want to read kAlpha_8, |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2234 | // it's unsupported, but 32bit RGBA reads are supported. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2235 | if (!this->readPixelsSupported(srcSurface, kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2236 | // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888) |
| 2237 | // first and then onReadPixels will read that to a 32bit temporary buffer. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2238 | if (this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2239 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2240 | tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2241 | tempDrawInfo->fReadColorType = GrColorType::kAlpha_8; |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2242 | } else { |
| 2243 | return false; |
| 2244 | } |
| 2245 | } else { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 2246 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2247 | SkASSERT(tempDrawInfo->fReadColorType == GrColorType::kAlpha_8); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2248 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2249 | } else if (kRGBA_half_GrPixelConfig == dstAsConfig && |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2250 | this->readPixelsSupported(srcSurface, kRGBA_float_GrPixelConfig)) { |
| 2251 | // If reading in half float format is not supported, then read in float format. |
| 2252 | return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2253 | } else if (this->glCaps().canConfigBeFBOColorAttachment(dstAsConfig) && |
| 2254 | this->readPixelsSupported(dstAsConfig, dstAsConfig)) { |
bsalomon | e6d665e | 2016-03-10 07:22:25 -0800 | [diff] [blame] | 2255 | // Do a draw to convert from the src config to the read config. |
| 2256 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2257 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstAsConfig; |
| 2258 | tempDrawInfo->fReadColorType = dstColorType; |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2259 | } else { |
| 2260 | return false; |
| 2261 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2264 | if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2265 | read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, dstAsConfig, |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2266 | rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2267 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2270 | return true; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2273 | bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width, |
| 2274 | int height, GrColorType dstColorType, void* buffer, size_t rowBytes) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2275 | SkASSERT(surface); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2276 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2277 | GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2278 | if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2279 | return false; |
| 2280 | } |
| 2281 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2282 | // TODO: Avoid this conversion by making GrGLCaps work with color types. |
| 2283 | auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo); |
| 2284 | |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2285 | // We have a special case fallback for reading eight bit alpha. We will read back all four 8 |
| 2286 | // bit channels as RGBA and then extract A. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2287 | if (!this->readPixelsSupported(surface, dstAsConfig)) { |
| 2288 | if (kAlpha_8_GrPixelConfig == dstAsConfig && |
| 2289 | this->readPixelsSupported(surface, kRGBA_8888_GrPixelConfig)) { |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 2290 | std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2291 | if (this->onReadPixels(surface, origin, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2292 | GrColorType::kRGBA_8888, temp.get(), width * 4)) { |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2293 | uint8_t* dst = reinterpret_cast<uint8_t*>(buffer); |
| 2294 | for (int j = 0; j < height; ++j) { |
| 2295 | for (int i = 0; i < width; ++i) { |
| 2296 | dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24; |
| 2297 | } |
| 2298 | } |
| 2299 | return true; |
| 2300 | } |
| 2301 | } |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2302 | |
| 2303 | // If reading in half float format is not supported, then read in a temporary float buffer |
| 2304 | // and convert to half float. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2305 | if (kRGBA_half_GrPixelConfig == dstAsConfig && |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2306 | this->readPixelsSupported(surface, kRGBA_float_GrPixelConfig)) { |
| 2307 | std::unique_ptr<float[]> temp(new float[width * height * 4]); |
| 2308 | if (this->onReadPixels(surface, origin, left, top, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2309 | GrColorType::kRGBA_F32, temp.get(), width * sizeof(float) * 4)) { |
Stan Iliev | 0078ab2 | 2017-11-08 14:16:26 -0500 | [diff] [blame] | 2310 | uint8_t* dst = reinterpret_cast<uint8_t*>(buffer); |
| 2311 | float* src = temp.get(); |
| 2312 | for (int j = 0; j < height; ++j) { |
| 2313 | SkHalf* dstRow = reinterpret_cast<SkHalf*>(dst); |
| 2314 | for (int i = 0; i < width; ++i) { |
| 2315 | for (int color = 0; color < 4; color++) { |
| 2316 | *dstRow++ = SkFloatToHalf(*src++); |
| 2317 | } |
| 2318 | } |
| 2319 | dst += rowBytes; |
| 2320 | } |
| 2321 | return true; |
| 2322 | } |
| 2323 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 2324 | return false; |
| 2325 | } |
| 2326 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2327 | GrGLenum externalFormat; |
| 2328 | GrGLenum externalType; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2329 | if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2330 | &externalType)) { |
| 2331 | return false; |
| 2332 | } |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2333 | bool flipY = kBottomLeft_GrSurfaceOrigin == origin; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2334 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2335 | GrGLIRect glvp; |
| 2336 | if (renderTarget) { |
| 2337 | // resolve the render target if necessary |
| 2338 | switch (renderTarget->getResolveType()) { |
| 2339 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 2340 | return false; |
| 2341 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2342 | this->flushRenderTargetNoColorWrites(renderTarget); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2343 | break; |
| 2344 | case GrGLRenderTarget::kCanResolve_ResolveType: |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2345 | this->onResolveRenderTarget(renderTarget); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2346 | // we don't track the state of the READ FBO ID. |
| 2347 | fStats.incRenderTargetBinds(); |
| 2348 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID())); |
| 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) { |
bsalomon | 1e0bf7e | 2015-03-14 12:08:51 -0700 | [diff] [blame] | 2473 | fStats.incRenderTargetBinds(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2474 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID())); |
| 2475 | #ifdef SK_DEBUG |
| 2476 | // don't do this check in Chromium -- this is causing |
| 2477 | // lots of repeated command buffer flushes when the compositor is |
| 2478 | // rendering with Ganesh, which is really slow; even too slow for |
| 2479 | // Debug mode. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 2480 | if (kChromium_GrGLDriver != this->glContext().driver()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2481 | GrGLenum status; |
| 2482 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 2483 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 2484 | SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
| 2485 | } |
bsalomon | 160f24c | 2015-03-17 15:55:42 -0700 | [diff] [blame] | 2486 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2487 | #endif |
| 2488 | fHWBoundRenderTargetUniqueID = rtID; |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2489 | this->flushViewport(target->getViewport()); |
brianosman | 64d094d | 2016-03-25 06:01:59 -0700 | [diff] [blame] | 2490 | } |
| 2491 | |
brianosman | 35b784d | 2016-05-05 11:52:53 -0700 | [diff] [blame] | 2492 | if (this->glCaps().srgbWriteControl()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2493 | this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB); |
bsalomon | 5cd020f | 2015-03-17 12:46:56 -0700 | [diff] [blame] | 2494 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2495 | } |
bsalomon | a990912 | 2016-01-23 10:41:40 -0800 | [diff] [blame] | 2496 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2497 | void GrGLGpu::flushFramebufferSRGB(bool enable) { |
| 2498 | if (enable && kYes_TriState != fHWSRGBFramebuffer) { |
| 2499 | GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2500 | fHWSRGBFramebuffer = kYes_TriState; |
| 2501 | } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) { |
| 2502 | GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2503 | fHWSRGBFramebuffer = kNo_TriState; |
| 2504 | } |
| 2505 | } |
| 2506 | |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2507 | void GrGLGpu::flushViewport(const GrGLIRect& viewport) { |
| 2508 | if (fHWViewport != viewport) { |
| 2509 | viewport.pushToGLViewport(this->glInterface()); |
| 2510 | fHWViewport = viewport; |
| 2511 | } |
| 2512 | } |
| 2513 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2514 | #define SWAP_PER_DRAW 0 |
| 2515 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 2516 | #if SWAP_PER_DRAW |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2517 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2518 | #include <AGL/agl.h> |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2519 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | ce7357d | 2012-06-25 17:49:25 +0000 | [diff] [blame] | 2520 | #include <gl/GL.h> |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2521 | void SwapBuf() { |
| 2522 | DWORD procID = GetCurrentProcessId(); |
| 2523 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 2524 | while(hwnd) { |
| 2525 | DWORD wndProcID = 0; |
| 2526 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 2527 | if(wndProcID == procID) { |
| 2528 | SwapBuffers(GetDC(hwnd)); |
| 2529 | } |
| 2530 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 2531 | } |
| 2532 | } |
| 2533 | #endif |
| 2534 | #endif |
| 2535 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2536 | void GrGLGpu::draw(const GrPipeline& pipeline, |
| 2537 | const GrPrimitiveProcessor& primProc, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2538 | const GrMesh meshes[], |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2539 | const GrPipeline::DynamicState dynamicStates[], |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2540 | int meshCount) { |
| 2541 | this->handleDirtyContext(); |
| 2542 | |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2543 | bool hasPoints = false; |
| 2544 | for (int i = 0; i < meshCount; ++i) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2545 | if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) { |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2546 | hasPoints = true; |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | if (!this->flushGLState(pipeline, primProc, hasPoints)) { |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 2551 | return; |
| 2552 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2553 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 2554 | for (int i = 0; i < meshCount; ++i) { |
| 2555 | if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 2556 | this->xferBarrier(pipeline.renderTarget(), barrierType); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 2557 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2558 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2559 | if (dynamicStates) { |
| 2560 | if (pipeline.getScissorState().enabled()) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 2561 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget()); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2562 | this->flushScissor(dynamicStates[i].fScissorRect, |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 2563 | glRT->getViewport(), pipeline.proxy()->origin()); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2564 | } |
| 2565 | } |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2566 | if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() && |
| 2567 | GrIsPrimTypeLines(meshes[i].primitiveType()) && |
| 2568 | !GrIsPrimTypeLines(fLastPrimitiveType)) { |
| 2569 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2570 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
| 2571 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2572 | meshes[i].sendToGpu(primProc, this); |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2573 | fLastPrimitiveType = meshes[i].primitiveType(); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2574 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2575 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2576 | #if SWAP_PER_DRAW |
| 2577 | glFlush(); |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2578 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2579 | aglSwapBuffers(aglGetCurrentContext()); |
| 2580 | int set_a_break_pt_here = 9; |
| 2581 | aglSwapBuffers(aglGetCurrentContext()); |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2582 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2583 | SwapBuf(); |
| 2584 | int set_a_break_pt_here = 9; |
| 2585 | SwapBuf(); |
| 2586 | #endif |
| 2587 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2590 | static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) { |
| 2591 | switch (primitiveType) { |
| 2592 | case GrPrimitiveType::kTriangles: |
| 2593 | return GR_GL_TRIANGLES; |
| 2594 | case GrPrimitiveType::kTriangleStrip: |
| 2595 | return GR_GL_TRIANGLE_STRIP; |
| 2596 | case GrPrimitiveType::kTriangleFan: |
| 2597 | return GR_GL_TRIANGLE_FAN; |
| 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 | } |
| 2621 | fStats.incNumDraws(); |
| 2622 | } |
| 2623 | |
| 2624 | void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 2625 | GrPrimitiveType primitiveType, const GrBuffer* indexBuffer, |
| 2626 | int indexCount, int baseIndex, uint16_t minIndexValue, |
| 2627 | uint16_t maxIndexValue, const GrBuffer* vertexBuffer, |
| 2628 | int baseVertex) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2629 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2630 | GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() + |
| 2631 | sizeof(uint16_t) * baseIndex); |
| 2632 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2633 | this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2634 | |
| 2635 | if (this->glCaps().drawRangeElementsSupport()) { |
| 2636 | GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, |
| 2637 | GR_GL_UNSIGNED_SHORT, indices)); |
| 2638 | } else { |
| 2639 | GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices)); |
| 2640 | } |
| 2641 | fStats.incNumDraws(); |
| 2642 | } |
| 2643 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2644 | void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType |
| 2645 | primitiveType, const GrBuffer* vertexBuffer, |
| 2646 | int vertexCount, int baseVertex, |
| 2647 | const GrBuffer* instanceBuffer, int instanceCount, |
| 2648 | int baseInstance) { |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 2649 | GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
| 2650 | int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount); |
| 2651 | for (int i = 0; i < instanceCount; i += maxInstances) { |
| 2652 | this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i); |
| 2653 | GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, |
| 2654 | SkTMin(instanceCount - i, maxInstances))); |
| 2655 | fStats.incNumDraws(); |
| 2656 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, |
| 2660 | GrPrimitiveType primitiveType, |
| 2661 | const GrBuffer* indexBuffer, int indexCount, |
| 2662 | int baseIndex, const GrBuffer* vertexBuffer, |
| 2663 | int baseVertex, const GrBuffer* instanceBuffer, |
| 2664 | int instanceCount, int baseInstance) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2665 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2666 | GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() + |
| 2667 | sizeof(uint16_t) * baseIndex); |
| 2668 | this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, |
| 2669 | instanceBuffer, baseInstance); |
| 2670 | GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices, |
| 2671 | instanceCount)); |
| 2672 | fStats.incNumDraws(); |
| 2673 | } |
| 2674 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2675 | void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 2676 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2677 | if (rt->needsResolve()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2678 | // Some extensions automatically resolves the texture when it is read. |
| 2679 | if (this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2680 | SkASSERT(rt->textureFBOID() != rt->renderFBOID()); |
Brian Osman | cfe83d1 | 2018-01-19 11:13:45 -0500 | [diff] [blame] | 2681 | SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2682 | fStats.incRenderTargetBinds(); |
| 2683 | fStats.incRenderTargetBinds(); |
| 2684 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID())); |
| 2685 | GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID())); |
| 2686 | // make sure we go through flushRenderTarget() since we've modified |
| 2687 | // the bound DRAW FBO ID. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2688 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2689 | const GrGLIRect& vp = rt->getViewport(); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 2690 | const SkIRect dirtyRect = rt->getResolveRect(); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2691 | // The dirty rect tracked on the RT is always stored in the native coordinates of the |
| 2692 | // surface. Choose kTopLeft so no adjustments are made |
| 2693 | static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin; |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2694 | if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2695 | // Apple's extension uses the scissor as the blit bounds. |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2696 | GrScissorState scissorState; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 2697 | scissorState.set(dirtyRect); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2698 | this->flushScissor(scissorState, vp, kDirtyRectOrigin); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2699 | this->disableWindowRectangles(); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2700 | GL_CALL(ResolveMultisampleFramebuffer()); |
| 2701 | } else { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2702 | int l, b, r, t; |
| 2703 | if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & |
| 2704 | this->glCaps().blitFramebufferSupportFlags()) { |
| 2705 | l = 0; |
| 2706 | b = 0; |
| 2707 | r = target->width(); |
| 2708 | t = target->height(); |
| 2709 | } else { |
| 2710 | GrGLIRect rect; |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2711 | rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin); |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2712 | l = rect.fLeft; |
| 2713 | b = rect.fBottom; |
| 2714 | r = rect.fLeft + rect.fWidth; |
| 2715 | t = rect.fBottom + rect.fHeight; |
| 2716 | } |
derekf | 8c8f71a | 2014-09-16 06:24:57 -0700 | [diff] [blame] | 2717 | |
| 2718 | // BlitFrameBuffer respects the scissor, so disable it. |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 2719 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 2720 | this->disableWindowRectangles(); |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2721 | GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2722 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 2723 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2724 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2725 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2729 | namespace { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2730 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2731 | |
| 2732 | GrGLenum gr_to_gl_stencil_op(GrStencilOp op) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2733 | static const GrGLenum gTable[kGrStencilOpCount] = { |
| 2734 | GR_GL_KEEP, // kKeep |
| 2735 | GR_GL_ZERO, // kZero |
| 2736 | GR_GL_REPLACE, // kReplace |
| 2737 | GR_GL_INVERT, // kInvert |
| 2738 | GR_GL_INCR_WRAP, // kIncWrap |
| 2739 | GR_GL_DECR_WRAP, // kDecWrap |
| 2740 | GR_GL_INCR, // kIncClamp |
| 2741 | GR_GL_DECR, // kDecClamp |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2742 | }; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2743 | GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep); |
| 2744 | GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero); |
| 2745 | GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace); |
| 2746 | GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert); |
| 2747 | GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap); |
| 2748 | GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap); |
| 2749 | GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp); |
| 2750 | GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp); |
| 2751 | SkASSERT(op < (GrStencilOp)kGrStencilOpCount); |
| 2752 | return gTable[(int)op]; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | void set_gl_stencil(const GrGLInterface* gl, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2756 | const GrStencilSettings::Face& face, |
| 2757 | GrGLenum glFace) { |
| 2758 | GrGLenum glFunc = GrToGLStencilFunc(face.fTest); |
| 2759 | GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp); |
| 2760 | GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2761 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2762 | GrGLint ref = face.fRef; |
| 2763 | GrGLint mask = face.fTestMask; |
| 2764 | GrGLint writeMask = face.fWriteMask; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2765 | |
| 2766 | if (GR_GL_FRONT_AND_BACK == glFace) { |
| 2767 | // we call the combined func just in case separate stencil is not |
| 2768 | // supported. |
| 2769 | GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask)); |
| 2770 | GR_GL_CALL(gl, StencilMask(writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2771 | GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2772 | } else { |
| 2773 | GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask)); |
| 2774 | GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2775 | GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2779 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2780 | void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2781 | if (stencilSettings.isDisabled()) { |
| 2782 | this->disableStencil(); |
| 2783 | } else if (fHWStencilSettings != stencilSettings) { |
| 2784 | if (kYes_TriState != fHWStencilTestEnabled) { |
| 2785 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2786 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2787 | fHWStencilTestEnabled = kYes_TriState; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2788 | } |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2789 | if (stencilSettings.isTwoSided()) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2790 | set_gl_stencil(this->glInterface(), |
| 2791 | stencilSettings.front(), |
| 2792 | GR_GL_FRONT); |
| 2793 | set_gl_stencil(this->glInterface(), |
| 2794 | stencilSettings.back(), |
| 2795 | GR_GL_BACK); |
| 2796 | } else { |
| 2797 | set_gl_stencil(this->glInterface(), |
| 2798 | stencilSettings.front(), |
| 2799 | GR_GL_FRONT_AND_BACK); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2800 | } |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2801 | fHWStencilSettings = stencilSettings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2805 | void GrGLGpu::disableStencil() { |
| 2806 | if (kNo_TriState != fHWStencilTestEnabled) { |
| 2807 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2808 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2809 | fHWStencilTestEnabled = kNo_TriState; |
| 2810 | fHWStencilSettings.invalidate(); |
| 2811 | } |
| 2812 | } |
| 2813 | |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2814 | void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) { |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2815 | // rt is only optional if useHWAA is false. |
| 2816 | SkASSERT(rt || !useHWAA); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 2817 | SkASSERT(!useHWAA || rt->isStencilBufferMultisampled()); |
bsalomon@google.com | 202d139 | 2013-03-19 18:58:08 +0000 | [diff] [blame] | 2818 | |
csmartdalton | 2b5f2cb | 2016-06-10 14:06:32 -0700 | [diff] [blame] | 2819 | if (this->caps()->multisampleDisableSupport()) { |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 2820 | if (useHWAA) { |
| 2821 | if (kYes_TriState != fMSAAEnabled) { |
| 2822 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
| 2823 | fMSAAEnabled = kYes_TriState; |
| 2824 | } |
| 2825 | } else { |
| 2826 | if (kNo_TriState != fMSAAEnabled) { |
| 2827 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 2828 | fMSAAEnabled = kNo_TriState; |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | } |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2832 | |
| 2833 | if (0 != this->caps()->maxRasterSamples()) { |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 2834 | if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) { |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2835 | // Since stencil is disabled and we want more samples than are in the color buffer, we |
| 2836 | // need to tell the rasterizer explicitly how many to run. |
| 2837 | if (kYes_TriState != fHWRasterMultisampleEnabled) { |
| 2838 | GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE)); |
| 2839 | fHWRasterMultisampleEnabled = kYes_TriState; |
| 2840 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 2841 | int numStencilSamples = rt->numStencilSamples(); |
| 2842 | // convert to GL's understanding of sample counts where 0 means nonMSAA. |
| 2843 | numStencilSamples = 1 == numStencilSamples ? 0 : numStencilSamples; |
| 2844 | if (numStencilSamples != fHWNumRasterSamples) { |
| 2845 | SkASSERT(numStencilSamples <= this->caps()->maxRasterSamples()); |
| 2846 | GL_CALL(RasterSamples(numStencilSamples, GR_GL_TRUE)); |
| 2847 | fHWNumRasterSamples = numStencilSamples; |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } else { |
| 2850 | if (kNo_TriState != fHWRasterMultisampleEnabled) { |
| 2851 | GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE)); |
| 2852 | fHWRasterMultisampleEnabled = kNo_TriState; |
| 2853 | } |
| 2854 | } |
| 2855 | } else { |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 2856 | SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled); |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 2857 | } |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2860 | void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) { |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2861 | // Any optimization to disable blending should have already been applied and |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2862 | // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0). |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 2863 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2864 | GrBlendEquation equation = blendInfo.fEquation; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 2865 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 2866 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2867 | bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) && |
| 2868 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff; |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2869 | if (blendOff) { |
| 2870 | if (kNo_TriState != fHWBlendState.fEnabled) { |
| 2871 | GL_CALL(Disable(GR_GL_BLEND)); |
joel.liang | 9764c40 | 2015-07-09 19:46:18 -0700 | [diff] [blame] | 2872 | |
| 2873 | // Workaround for the ARM KHR_blend_equation_advanced blacklist issue |
| 2874 | // https://code.google.com/p/skia/issues/detail?id=3943 |
| 2875 | if (kARM_GrGLVendor == this->ctxInfo().vendor() && |
| 2876 | GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) { |
| 2877 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2878 | // Set to any basic blending equation. |
| 2879 | GrBlendEquation blend_equation = kAdd_GrBlendEquation; |
| 2880 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation])); |
| 2881 | fHWBlendState.fEquation = blend_equation; |
| 2882 | } |
| 2883 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2884 | fHWBlendState.fEnabled = kNo_TriState; |
| 2885 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2886 | return; |
| 2887 | } |
| 2888 | |
| 2889 | if (kYes_TriState != fHWBlendState.fEnabled) { |
| 2890 | GL_CALL(Enable(GR_GL_BLEND)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2891 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2892 | fHWBlendState.fEnabled = kYes_TriState; |
| 2893 | } |
| 2894 | |
| 2895 | if (fHWBlendState.fEquation != equation) { |
| 2896 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation])); |
| 2897 | fHWBlendState.fEquation = equation; |
| 2898 | } |
| 2899 | |
| 2900 | if (GrBlendEquationIsAdvanced(equation)) { |
| 2901 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2902 | // Advanced equations have no other blend state. |
| 2903 | return; |
| 2904 | } |
| 2905 | |
bsalomon | e63ffef | 2016-02-05 07:17:34 -0800 | [diff] [blame] | 2906 | if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2907 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 2908 | gXfermodeCoeff2Blend[dstCoeff])); |
| 2909 | fHWBlendState.fSrcCoeff = srcCoeff; |
| 2910 | fHWBlendState.fDstCoeff = dstCoeff; |
| 2911 | } |
| 2912 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2913 | if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) { |
| 2914 | GrColor blendConst = blendInfo.fBlendConstant; |
| 2915 | blendConst = swizzle.applyTo(blendConst); |
| 2916 | if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) { |
| 2917 | GrGLfloat c[4]; |
| 2918 | GrColorToRGBAFloat(blendConst, c); |
| 2919 | GL_CALL(BlendColor(c[0], c[1], c[2], c[3])); |
| 2920 | fHWBlendState.fConstColor = blendConst; |
| 2921 | fHWBlendState.fConstColorValid = true; |
| 2922 | } |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2923 | } |
| 2924 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2925 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2926 | static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) { |
| 2927 | switch (wrapMode) { |
| 2928 | case GrSamplerState::WrapMode::kClamp: |
| 2929 | return GR_GL_CLAMP_TO_EDGE; |
| 2930 | case GrSamplerState::WrapMode::kRepeat: |
| 2931 | return GR_GL_REPEAT; |
| 2932 | case GrSamplerState::WrapMode::kMirrorRepeat: |
| 2933 | return GR_GL_MIRRORED_REPEAT; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2934 | }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2935 | SK_ABORT("Unknown wrap mode"); |
| 2936 | return 0; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2939 | static GrGLenum get_component_enum_from_char(char component) { |
| 2940 | switch (component) { |
| 2941 | case 'r': |
| 2942 | return GR_GL_RED; |
| 2943 | case 'g': |
| 2944 | return GR_GL_GREEN; |
| 2945 | case 'b': |
| 2946 | return GR_GL_BLUE; |
| 2947 | case 'a': |
| 2948 | return GR_GL_ALPHA; |
| 2949 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2950 | SK_ABORT("Unsupported component"); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 2956 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 2957 | static void get_tex_param_swizzle(GrPixelConfig config, |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2958 | const GrGLCaps& caps, |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2959 | GrGLenum* glSwizzle) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2960 | const GrSwizzle& swizzle = caps.configSwizzle(config); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2961 | for (int i = 0; i < 4; ++i) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2962 | glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2966 | static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) { |
| 2967 | switch (filter) { |
| 2968 | case GrSamplerState::Filter::kNearest: |
| 2969 | return GR_GL_NEAREST; |
| 2970 | case GrSamplerState::Filter::kBilerp: |
| 2971 | return GR_GL_LINEAR; |
| 2972 | case GrSamplerState::Filter::kMipMap: |
| 2973 | return GR_GL_LINEAR; |
| 2974 | } |
| 2975 | SK_ABORT("Unknown filter"); |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) { |
| 2980 | switch (filter) { |
| 2981 | case GrSamplerState::Filter::kNearest: |
| 2982 | return GR_GL_NEAREST; |
| 2983 | case GrSamplerState::Filter::kBilerp: |
| 2984 | return GR_GL_LINEAR; |
| 2985 | case GrSamplerState::Filter::kMipMap: |
| 2986 | return GR_GL_LINEAR_MIPMAP_LINEAR; |
| 2987 | } |
| 2988 | SK_ABORT("Unknown filter"); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
| 2992 | void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2993 | GrGLTexture* texture, GrSurfaceOrigin textureOrigin) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2994 | SkASSERT(texture); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 2995 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 2996 | #ifdef SK_DEBUG |
| 2997 | if (!this->caps()->npotTextureTileSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2998 | if (samplerState.isRepeated()) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 2999 | const int w = texture->width(); |
| 3000 | const int h = texture->height(); |
| 3001 | SkASSERT(SkIsPow2(w) && SkIsPow2(h)); |
| 3002 | } |
| 3003 | } |
| 3004 | #endif |
| 3005 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 3006 | // If we created a rt/tex and rendered to it without using a texture and now we're texturing |
| 3007 | // 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] | 3008 | // out of the "last != next" check. |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 3009 | GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3010 | if (texRT) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3011 | this->onResolveRenderTarget(texRT); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3014 | GrGpuResource::UniqueID textureID = texture->uniqueID(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3015 | GrGLenum target = texture->target(); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3016 | if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3017 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3018 | GL_CALL(BindTexture(target, texture->textureID())); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3019 | fHWBoundTextureUniqueIDs[unitIdx] = textureID; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3022 | ResetTimestamp timestamp; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3023 | const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(×tamp); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3024 | bool setAll = timestamp < this->getResetTimestamp(); |
| 3025 | GrGLTexture::TexParams newTexParams; |
| 3026 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3027 | GrSamplerState::Filter filterMode = samplerState.filter(); |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3028 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3029 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 3030 | if (!this->caps()->mipMapSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3031 | filterMode = GrSamplerState::Filter::kBilerp; |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3032 | } |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 3033 | } |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 3034 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3035 | newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode); |
| 3036 | newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode); |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 3037 | |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 3038 | if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) { |
brianosman | fe19987 | 2016-06-13 07:59:48 -0700 | [diff] [blame] | 3039 | newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3040 | if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) { |
brianosman | adf4fa6 | 2016-04-26 16:48:06 -0700 | [diff] [blame] | 3041 | this->setTextureUnit(unitIdx); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3042 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3043 | } |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 3044 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3045 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3046 | #ifdef SK_DEBUG |
| 3047 | // 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] | 3048 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3049 | SkASSERT(!texture->texturePriv().mipMapsAreDirty()); |
| 3050 | if (GrPixelConfigIsSRGB(texture->config())) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3051 | SkDestinationSurfaceColorMode colorMode = allowSRGBInputs |
| 3052 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 3053 | : SkDestinationSurfaceColorMode::kLegacy; |
| 3054 | SkASSERT(texture->texturePriv().mipColorMode() == colorMode); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | #endif |
| 3058 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3059 | newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel(); |
| 3060 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3061 | newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX()); |
| 3062 | newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 3063 | get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA); |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 3064 | if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3065 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3066 | 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] | 3067 | } |
| 3068 | if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) { |
| 3069 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3070 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3071 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3072 | if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) { |
cblume | 09bd2c0 | 2016-03-01 14:08:28 -0800 | [diff] [blame] | 3073 | // These are not supported in ES2 contexts |
| 3074 | if (this->glCaps().mipMapLevelAndLodControlSupport()) { |
| 3075 | if (newTexParams.fMaxMipMapLevel != 0) { |
| 3076 | this->setTextureUnit(unitIdx); |
| 3077 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0)); |
| 3078 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0)); |
| 3079 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD, |
| 3080 | newTexParams.fMaxMipMapLevel)); |
| 3081 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL, |
| 3082 | newTexParams.fMaxMipMapLevel)); |
| 3083 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 3084 | } |
| 3085 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3086 | if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3087 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3088 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3089 | } |
| 3090 | if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3091 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3092 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3093 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 3094 | if (this->glCaps().textureSwizzleSupport() && |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3095 | (setAll || memcmp(newTexParams.fSwizzleRGBA, |
| 3096 | oldTexParams.fSwizzleRGBA, |
| 3097 | sizeof(newTexParams.fSwizzleRGBA)))) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3098 | this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3099 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 3100 | texture->setCachedTexParams(newTexParams, this->getResetTimestamp()); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3103 | void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3104 | SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig)); |
| 3105 | SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count()); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3106 | |
| 3107 | BufferTexture& buffTex = fHWBufferTextures[unitIdx]; |
| 3108 | |
| 3109 | if (!buffTex.fKnownBound) { |
| 3110 | if (!buffTex.fTextureID) { |
| 3111 | GL_CALL(GenTextures(1, &buffTex.fTextureID)); |
| 3112 | if (!buffTex.fTextureID) { |
| 3113 | return; |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | this->setTextureUnit(unitIdx); |
| 3118 | GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID)); |
| 3119 | |
| 3120 | buffTex.fKnownBound = true; |
| 3121 | } |
| 3122 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 3123 | if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID || |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3124 | buffTex.fTexelConfig != texelConfig) { |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3125 | |
| 3126 | this->setTextureUnit(unitIdx); |
csmartdalton | 1897cfd | 2016-06-03 08:50:54 -0700 | [diff] [blame] | 3127 | GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER, |
| 3128 | this->glCaps().configSizedInternalFormat(texelConfig), |
| 3129 | buffer->bufferID())); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3130 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3131 | buffTex.fTexelConfig = texelConfig; |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 3132 | buffTex.fAttachedBufferUniqueID = buffer->uniqueID(); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3133 | |
| 3134 | if (this->glCaps().textureSwizzleSupport() && |
| 3135 | this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) { |
| 3136 | GrGLenum glSwizzle[4]; |
| 3137 | get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle); |
| 3138 | this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle); |
| 3139 | buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig); |
| 3140 | } |
| 3141 | |
| 3142 | buffer->setHasAttachedToTexture(); |
| 3143 | fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit); |
| 3144 | } |
| 3145 | } |
| 3146 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3147 | void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3148 | GrGLTexture* texture, GrSurfaceOrigin textureOrigin) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3149 | SkASSERT(texture); |
| 3150 | |
| 3151 | // First, figure out if we need mips for this texture at all: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3152 | GrSamplerState::Filter filterMode = params.filter(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3153 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3154 | if (GrSamplerState::Filter::kMipMap == filterMode) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 3155 | if (!this->caps()->mipMapSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3156 | filterMode = GrSamplerState::Filter::kBilerp; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 3160 | if (GrSamplerState::Filter::kMipMap != filterMode) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3161 | return; |
| 3162 | } |
| 3163 | |
| 3164 | // If this is an sRGB texture and the mips were previously built the "other" way |
| 3165 | // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for |
| 3166 | // 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] | 3167 | SkDestinationSurfaceColorMode colorMode = allowSRGBInputs |
| 3168 | ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware |
| 3169 | : SkDestinationSurfaceColorMode::kLegacy; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3170 | if (GrPixelConfigIsSRGB(texture->config()) && |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3171 | colorMode != texture->texturePriv().mipColorMode()) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 3172 | texture->texturePriv().markMipMapsDirty(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | // If the mips aren't dirty, we're done: |
| 3176 | if (!texture->texturePriv().mipMapsAreDirty()) { |
| 3177 | return; |
| 3178 | } |
| 3179 | |
| 3180 | // If we created a rt/tex and rendered to it without using a texture and now we're texturing |
| 3181 | // from the rt it will still be the last bound texture, but it needs resolving. |
| 3182 | GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget()); |
| 3183 | if (texRT) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3184 | this->onResolveRenderTarget(texRT); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | GrGLenum target = texture->target(); |
| 3188 | this->setScratchTextureUnit(); |
| 3189 | GL_CALL(BindTexture(target, texture->textureID())); |
| 3190 | |
| 3191 | // Configure sRGB decode, if necessary. This state is the only thing needed for the driver |
| 3192 | // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too. |
brianosman | 2047189 | 2016-12-02 06:43:32 -0800 | [diff] [blame] | 3193 | if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) { |
Brian Osman | b040320 | 2016-12-09 10:55:23 -0500 | [diff] [blame] | 3194 | GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT; |
| 3195 | // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly. |
| 3196 | // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample, |
| 3197 | // but not the Linear -> sRGB when writing the next level. The result is that mip-chains |
| 3198 | // get progressively brighter as you go down. Forcing this to 'decode' gives predictable |
| 3199 | // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28) |
| 3200 | if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) { |
| 3201 | srgbDecode = GR_GL_DECODE_EXT; |
| 3202 | } |
| 3203 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | // 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] | 3207 | if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3208 | GL_CALL(GenerateMipmap(target)); |
| 3209 | } |
| 3210 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 3211 | texture->texturePriv().markMipMapsClean(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3212 | texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount( |
| 3213 | texture->width(), texture->height())); |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 3214 | texture->texturePriv().setMipColorMode(colorMode); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3215 | |
| 3216 | // We have potentially set lots of state on the texture. Easiest to dirty it all: |
| 3217 | texture->textureParamsModified(); |
| 3218 | } |
| 3219 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 3220 | void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) { |
| 3221 | this->setTextureUnit(unitIdx); |
| 3222 | if (this->glStandard() == kGLES_GrGLStandard) { |
| 3223 | // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA. |
| 3224 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0])); |
| 3225 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1])); |
| 3226 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2])); |
| 3227 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3])); |
| 3228 | } else { |
| 3229 | GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint)); |
| 3230 | GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, |
| 3231 | reinterpret_cast<const GrGLint*>(swizzle))); |
| 3232 | } |
| 3233 | } |
| 3234 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 3235 | void GrGLGpu::flushColorWrite(bool writeColor) { |
| 3236 | if (!writeColor) { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 3237 | if (kNo_TriState != fHWWriteToColor) { |
| 3238 | GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE, |
| 3239 | GR_GL_FALSE, GR_GL_FALSE)); |
| 3240 | fHWWriteToColor = kNo_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3241 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 3242 | } else { |
| 3243 | if (kYes_TriState != fHWWriteToColor) { |
| 3244 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 3245 | fHWWriteToColor = kYes_TriState; |
| 3246 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3247 | } |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 3248 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 3249 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 3250 | void GrGLGpu::setTextureUnit(int unit) { |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 3251 | SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3252 | if (unit != fHWActiveTextureUnitIdx) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 3253 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 4920939 | 2012-06-05 15:13:46 +0000 | [diff] [blame] | 3254 | fHWActiveTextureUnitIdx = unit; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3255 | } |
| 3256 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 3257 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 3258 | void GrGLGpu::setScratchTextureUnit() { |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3259 | // 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] | 3260 | int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1; |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3261 | if (lastUnitIdx != fHWActiveTextureUnitIdx) { |
| 3262 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx)); |
| 3263 | fHWActiveTextureUnitIdx = lastUnitIdx; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3264 | } |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 3265 | // clear out the this field so that if a program does use this unit it will rebind the correct |
| 3266 | // texture. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3267 | fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3270 | // Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3271 | static inline bool can_blit_framebuffer_for_copy_surface( |
| 3272 | const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3273 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 3274 | const SkIRect& srcRect, |
| 3275 | const SkIPoint& dstPoint, |
| 3276 | const GrGLGpu* gpu) { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3277 | auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags(); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3278 | if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) || |
| 3279 | !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3280 | return false; |
| 3281 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3282 | // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should |
| 3283 | // have filtered such cases out. |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3284 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 3285 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3286 | const GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 3287 | const GrRenderTarget* srcRT = src->asRenderTarget(); |
| 3288 | if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) { |
| 3289 | return false; |
| 3290 | } |
| 3291 | if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) { |
| 3292 | return false; |
| 3293 | } |
| 3294 | if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) { |
| 3295 | return false; |
| 3296 | } |
| 3297 | if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) { |
| 3298 | // We would mirror to compensate for origin changes. Note that copySurface is |
| 3299 | // specified such that the src and dst rects are the same. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3300 | if (dstOrigin != srcOrigin) { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3301 | return false; |
| 3302 | } |
| 3303 | } |
| 3304 | if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 3305 | if (srcRT && srcRT->numColorSamples() > 1) { |
| 3306 | if (dstRT && 1 == dstRT->numColorSamples()) { |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 3307 | return false; |
| 3308 | } |
| 3309 | if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) { |
| 3310 | return false; |
| 3311 | } |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3312 | } |
| 3313 | } |
| 3314 | if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 3315 | if (dstRT && dstRT->numColorSamples() > 1) { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3316 | return false; |
| 3317 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 3318 | } |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3319 | if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) { |
| 3320 | if (dst->config() != src->config()) { |
| 3321 | return false; |
| 3322 | } |
| 3323 | } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) { |
| 3324 | const GrRenderTarget* srcRT = src->asRenderTarget(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 3325 | if (srcRT && srcRT->numColorSamples() > 1 && dst->config() != src->config()) { |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3326 | return false; |
| 3327 | } |
| 3328 | } |
| 3329 | if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 3330 | if (srcRT && srcRT->numColorSamples() > 1) { |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 3331 | if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) { |
| 3332 | return false; |
| 3333 | } |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3334 | if (dstOrigin != srcOrigin) { |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 3335 | return false; |
| 3336 | } |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 3340 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3341 | |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3342 | static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) { |
| 3343 | // A RT has a separate MSAA renderbuffer if: |
| 3344 | // 1) It's multisampled |
| 3345 | // 2) We're using an extension with separate MSAA renderbuffers |
| 3346 | // 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] | 3347 | return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0; |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3348 | } |
| 3349 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3350 | static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3351 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3352 | const GrGLGpu* gpu) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3353 | // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage |
| 3354 | // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps |
| 3355 | // many drivers would allow it to work, but ANGLE does not. |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 3356 | if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() && |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3357 | (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) { |
| 3358 | return false; |
| 3359 | } |
| 3360 | const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget()); |
| 3361 | // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer) |
| 3362 | // then we don't want to copy to the texture but to the MSAA buffer. |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3363 | if (dstRT && rt_has_msaa_render_buffer(dstRT, gpu->glCaps())) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3364 | return false; |
| 3365 | } |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 3366 | const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget()); |
| 3367 | // If the src is multisampled (and uses an extension where there is a separate MSAA |
| 3368 | // renderbuffer) then it is an invalid operation to call CopyTexSubImage |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 3369 | if (srcRT && rt_has_msaa_render_buffer(srcRT, gpu->glCaps())) { |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 3370 | return false; |
| 3371 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3372 | |
| 3373 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
| 3374 | // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a |
| 3375 | // texture. |
| 3376 | if (!dstTex) { |
| 3377 | return false; |
| 3378 | } |
| 3379 | |
| 3380 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 3381 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3382 | // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring |
| 3383 | // is required. |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3384 | if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) && |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3385 | (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D && |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3386 | dstOrigin == srcOrigin) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3387 | return true; |
| 3388 | } else { |
| 3389 | return false; |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | // If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is |
| 3394 | // relative to is output. |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3395 | void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport, |
| 3396 | TempFBOTarget tempFBOTarget) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3397 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3398 | if (!rt) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3399 | SkASSERT(surface->asTexture()); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 3400 | GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture()); |
| 3401 | GrGLuint texID = texture->textureID(); |
| 3402 | GrGLenum target = texture->target(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3403 | GrGLuint* tempFBOID; |
| 3404 | tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID; |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3405 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3406 | if (0 == *tempFBOID) { |
| 3407 | GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3408 | } |
| 3409 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3410 | fStats.incRenderTargetBinds(); |
| 3411 | GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID)); |
| 3412 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 3413 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3414 | target, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 3415 | texID, |
| 3416 | 0)); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 3417 | texture->baseLevelWasBoundToFBO(); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3418 | viewport->fLeft = 0; |
| 3419 | viewport->fBottom = 0; |
| 3420 | viewport->fWidth = surface->width(); |
| 3421 | viewport->fHeight = surface->height(); |
| 3422 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 3423 | fStats.incRenderTargetBinds(); |
| 3424 | GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID())); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3425 | *viewport = rt->getViewport(); |
| 3426 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 3429 | void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) { |
| 3430 | // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3431 | if (!surface->asRenderTarget()) { |
| 3432 | SkASSERT(surface->asTexture()); |
| 3433 | GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target(); |
| 3434 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
| 3435 | GR_GL_COLOR_ATTACHMENT0, |
| 3436 | textureTarget, |
| 3437 | 0, |
| 3438 | 0)); |
| 3439 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3442 | bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3443 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 3444 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 3445 | bool canDiscardOutsideDstRect) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3446 | // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the |
| 3447 | // swizzle. |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3448 | if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) != |
| 3449 | this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3450 | return false; |
| 3451 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3452 | // 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] | 3453 | // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites(). |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3454 | bool preferCopy = SkToBool(dst->asRenderTarget()); |
| 3455 | if (preferCopy && src->asTexture()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3456 | if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3457 | return true; |
| 3458 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 3459 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 3460 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3461 | if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this)) { |
| 3462 | this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3463 | return true; |
| 3464 | } |
| 3465 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3466 | if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin, |
| 3467 | srcRect, dstPoint, this)) { |
| 3468 | return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin, |
| 3469 | srcRect, dstPoint); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3472 | if (!preferCopy && src->asTexture()) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3473 | if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3474 | return true; |
| 3475 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3476 | } |
| 3477 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3478 | return false; |
| 3479 | } |
| 3480 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3481 | bool GrGLGpu::createCopyProgram(GrTexture* srcTex) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 3482 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 3483 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3484 | int progIdx = TextureToCopyProgramIdx(srcTex); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3485 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3486 | GrSLType samplerType = srcTex->texturePriv().samplerType(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3487 | |
| 3488 | if (!fCopyProgramArrayBuffer) { |
| 3489 | static const GrGLfloat vdata[] = { |
| 3490 | 0, 0, |
| 3491 | 0, 1, |
| 3492 | 1, 0, |
| 3493 | 1, 1 |
| 3494 | }; |
| 3495 | fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType, |
| 3496 | kStatic_GrAccessPattern, vdata)); |
| 3497 | } |
| 3498 | if (!fCopyProgramArrayBuffer) { |
| 3499 | return false; |
| 3500 | } |
| 3501 | |
| 3502 | SkASSERT(!fCopyPrograms[progIdx].fProgram); |
| 3503 | GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram()); |
| 3504 | if (!fCopyPrograms[progIdx].fProgram) { |
| 3505 | return false; |
| 3506 | } |
| 3507 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3508 | const char* version = shaderCaps->versionDeclString(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3509 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3510 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3511 | GrShaderVar::kUniform_TypeModifier); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3512 | GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3513 | GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3514 | GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier); |
| 3515 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3516 | |
| 3517 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3518 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3519 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3520 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3521 | } |
| 3522 | vTexCoord.addModifier("noperspective"); |
| 3523 | } |
| 3524 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3525 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3526 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3527 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3528 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3529 | uPosXform.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3530 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3531 | vTexCoord.appendDecl(shaderCaps, &vshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3532 | vshaderTxt.append(";"); |
| 3533 | |
| 3534 | vshaderTxt.append( |
| 3535 | "// Copy Program VS\n" |
| 3536 | "void main() {" |
| 3537 | " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3538 | " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;" |
| 3539 | " sk_Position.zw = half2(0, 1);" |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3540 | "}" |
| 3541 | ); |
| 3542 | |
| 3543 | SkString fshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3544 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3545 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3546 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3547 | } |
| 3548 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3549 | if (samplerType == kTextureExternalSampler_GrSLType) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3550 | fshaderTxt.appendf("#extension %s : require\n", |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3551 | shaderCaps->externalTextureExtensionString()); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3552 | } |
Brian Salomon | f31ae49 | 2016-11-18 15:35:33 -0500 | [diff] [blame] | 3553 | vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3554 | vTexCoord.appendDecl(shaderCaps, &fshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3555 | fshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3556 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3557 | fshaderTxt.append(";"); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3558 | fshaderTxt.appendf( |
| 3559 | "// Copy Program FS\n" |
| 3560 | "void main() {" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3561 | " sk_FragColor = texture(u_texture, v_texCoord);" |
| 3562 | "}" |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3563 | ); |
| 3564 | |
| 3565 | const char* str; |
| 3566 | GrGLint length; |
| 3567 | |
| 3568 | str = vshaderTxt.c_str(); |
| 3569 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3570 | SkSL::Program::Settings settings; |
| 3571 | settings.fCaps = shaderCaps; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3572 | SkSL::String glsl; |
| 3573 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3574 | &str, &length, 1, settings, &glsl); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3575 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3576 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3577 | &fStats, settings); |
| 3578 | SkASSERT(program->fInputs.isEmpty()); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3579 | |
| 3580 | str = fshaderTxt.c_str(); |
| 3581 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3582 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3583 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3584 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3585 | &fStats, settings); |
| 3586 | SkASSERT(program->fInputs.isEmpty()); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3587 | |
| 3588 | GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram)); |
| 3589 | |
| 3590 | GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform, |
| 3591 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture")); |
| 3592 | GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform, |
| 3593 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform")); |
| 3594 | GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 3595 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3596 | |
| 3597 | GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3598 | |
| 3599 | GL_CALL(DeleteShader(vshader)); |
| 3600 | GL_CALL(DeleteShader(fshader)); |
| 3601 | |
| 3602 | return true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3603 | } |
| 3604 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3605 | bool GrGLGpu::createMipmapProgram(int progIdx) { |
| 3606 | const bool oddWidth = SkToBool(progIdx & 0x2); |
| 3607 | const bool oddHeight = SkToBool(progIdx & 0x1); |
| 3608 | const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1); |
| 3609 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3610 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3611 | |
| 3612 | SkASSERT(!fMipmapPrograms[progIdx].fProgram); |
| 3613 | GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram()); |
| 3614 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 3615 | return false; |
| 3616 | } |
| 3617 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3618 | const char* version = shaderCaps->versionDeclString(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3619 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3620 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3621 | GrShaderVar::kUniform_TypeModifier); |
| 3622 | GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType, |
| 3623 | GrShaderVar::kUniform_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3624 | // 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] | 3625 | GrShaderVar vTexCoords[] = { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3626 | GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3627 | GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3628 | GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3629 | GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3630 | }; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3631 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3632 | |
| 3633 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3634 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3635 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3636 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3637 | } |
| 3638 | vTexCoords[0].addModifier("noperspective"); |
| 3639 | vTexCoords[1].addModifier("noperspective"); |
| 3640 | vTexCoords[2].addModifier("noperspective"); |
| 3641 | vTexCoords[3].addModifier("noperspective"); |
| 3642 | } |
| 3643 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3644 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3645 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3646 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3647 | vshaderTxt.append(";"); |
| 3648 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3649 | vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3650 | vshaderTxt.append(";"); |
| 3651 | } |
| 3652 | |
| 3653 | vshaderTxt.append( |
| 3654 | "// Mipmap Program VS\n" |
| 3655 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3656 | " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);" |
| 3657 | " sk_Position.zw = half2(0, 1);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3658 | ); |
| 3659 | |
| 3660 | // Insert texture coordinate computation: |
| 3661 | if (oddWidth && oddHeight) { |
| 3662 | vshaderTxt.append( |
| 3663 | " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3664 | " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);" |
| 3665 | " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3666 | " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;" |
| 3667 | ); |
| 3668 | } else if (oddWidth) { |
| 3669 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3670 | " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);" |
| 3671 | " 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] | 3672 | ); |
| 3673 | } else if (oddHeight) { |
| 3674 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3675 | " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);" |
| 3676 | " 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] | 3677 | ); |
| 3678 | } else { |
| 3679 | vshaderTxt.append( |
| 3680 | " v_texCoord0 = a_vertex.xy;" |
| 3681 | ); |
| 3682 | } |
| 3683 | |
| 3684 | vshaderTxt.append("}"); |
| 3685 | |
| 3686 | SkString fshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3687 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3688 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3689 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3690 | } |
| 3691 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3692 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | f31ae49 | 2016-11-18 15:35:33 -0500 | [diff] [blame] | 3693 | vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3694 | vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3695 | fshaderTxt.append(";"); |
| 3696 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3697 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3698 | fshaderTxt.append(";"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3699 | fshaderTxt.append( |
| 3700 | "// Mipmap Program FS\n" |
| 3701 | "void main() {" |
| 3702 | ); |
| 3703 | |
| 3704 | if (oddWidth && oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3705 | fshaderTxt.append( |
| 3706 | " sk_FragColor = (texture(u_texture, v_texCoord0) + " |
| 3707 | " texture(u_texture, v_texCoord1) + " |
| 3708 | " texture(u_texture, v_texCoord2) + " |
| 3709 | " texture(u_texture, v_texCoord3)) * 0.25;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3710 | ); |
| 3711 | } else if (oddWidth || oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3712 | fshaderTxt.append( |
| 3713 | " sk_FragColor = (texture(u_texture, v_texCoord0) + " |
| 3714 | " texture(u_texture, v_texCoord1)) * 0.5;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3715 | ); |
| 3716 | } else { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3717 | fshaderTxt.append( |
| 3718 | " sk_FragColor = texture(u_texture, v_texCoord0);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3719 | ); |
| 3720 | } |
| 3721 | |
| 3722 | fshaderTxt.append("}"); |
| 3723 | |
| 3724 | const char* str; |
| 3725 | GrGLint length; |
| 3726 | |
| 3727 | str = vshaderTxt.c_str(); |
| 3728 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3729 | SkSL::Program::Settings settings; |
| 3730 | settings.fCaps = shaderCaps; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3731 | SkSL::String glsl; |
| 3732 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3733 | &str, &length, 1, settings, &glsl); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3734 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3735 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3736 | &fStats, settings); |
| 3737 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3738 | |
| 3739 | str = fshaderTxt.c_str(); |
| 3740 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3741 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3742 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3743 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3744 | &fStats, settings); |
| 3745 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3746 | |
| 3747 | GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram)); |
| 3748 | |
| 3749 | GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform, |
| 3750 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture")); |
| 3751 | GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 3752 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3753 | |
| 3754 | GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3755 | |
| 3756 | GL_CALL(DeleteShader(vshader)); |
| 3757 | GL_CALL(DeleteShader(fshader)); |
| 3758 | |
| 3759 | return true; |
| 3760 | } |
| 3761 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3762 | bool GrGLGpu::createStencilClipClearProgram() { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 3763 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 3764 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3765 | if (!fStencilClipClearArrayBuffer) { |
| 3766 | static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1}; |
| 3767 | fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create( |
| 3768 | this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata)); |
| 3769 | if (!fStencilClipClearArrayBuffer) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3770 | return false; |
| 3771 | } |
| 3772 | } |
| 3773 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3774 | SkASSERT(!fStencilClipClearProgram); |
| 3775 | GL_CALL_RET(fStencilClipClearProgram, CreateProgram()); |
| 3776 | if (!fStencilClipClearProgram) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3777 | return false; |
| 3778 | } |
| 3779 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3780 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3781 | const char* version = this->caps()->shaderCaps()->versionDeclString(); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3782 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3783 | SkString vshaderTxt(version); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3784 | aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3785 | vshaderTxt.append(";"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3786 | vshaderTxt.append( |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3787 | "// Stencil Clip Clear Program VS\n" |
| 3788 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3789 | " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);" |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3790 | "}"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3791 | |
| 3792 | SkString fshaderTxt(version); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3793 | fshaderTxt.appendf( |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3794 | "// Stencil Clip Clear Program FS\n" |
| 3795 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3796 | " sk_FragColor = half4(0);" |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3797 | "}"); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3798 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3799 | const char* str; |
| 3800 | GrGLint length; |
| 3801 | |
| 3802 | str = vshaderTxt.c_str(); |
| 3803 | length = SkToInt(vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3804 | SkSL::Program::Settings settings; |
| 3805 | settings.fCaps = this->caps()->shaderCaps(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3806 | SkSL::String glsl; |
| 3807 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, |
| 3808 | &str, &length, 1, settings, &glsl); |
| 3809 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, |
| 3810 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3811 | &fStats, settings); |
| 3812 | SkASSERT(program->fInputs.isEmpty()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3813 | |
| 3814 | str = fshaderTxt.c_str(); |
| 3815 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3816 | program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
| 3817 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, |
| 3818 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3819 | &fStats, settings); |
| 3820 | SkASSERT(program->fInputs.isEmpty()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3821 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3822 | GL_CALL(LinkProgram(fStencilClipClearProgram)); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3823 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3824 | GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex")); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3825 | |
| 3826 | GL_CALL(DeleteShader(vshader)); |
| 3827 | GL_CALL(DeleteShader(fshader)); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3828 | |
| 3829 | return true; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3830 | } |
| 3831 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3832 | void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3833 | GrRenderTarget* rt, GrSurfaceOrigin origin) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3834 | // TODO: This should swizzle the output to match dst's config, though it is a debugging |
| 3835 | // visualization. |
| 3836 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3837 | this->handleDirtyContext(); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3838 | if (!fStencilClipClearProgram) { |
| 3839 | if (!this->createStencilClipClearProgram()) { |
| 3840 | SkDebugf("Failed to create stencil clip clear program.\n"); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3841 | return; |
| 3842 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3843 | } |
| 3844 | |
Mike Klein | 8b06ed7 | 2017-06-06 21:47:32 +0000 | [diff] [blame] | 3845 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3846 | this->flushRenderTarget(glRT); |
Mike Klein | 8b06ed7 | 2017-06-06 21:47:32 +0000 | [diff] [blame] | 3847 | |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3848 | GL_CALL(UseProgram(fStencilClipClearProgram)); |
| 3849 | fHWProgramID = fStencilClipClearProgram; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3850 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3851 | fHWVertexArrayState.setVertexArrayID(this, 0); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3852 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3853 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 3854 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 3855 | attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 3856 | 2 * sizeof(GrGLfloat), 0); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3857 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3858 | GrXferProcessor::BlendInfo blendInfo; |
| 3859 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3860 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3861 | this->flushColorWrite(false); |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 3862 | this->flushHWAAState(glRT, false, false); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3863 | this->flushScissor(clip.scissorState(), glRT->getViewport(), origin); |
| 3864 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
Mike Klein | 31550db | 2017-06-06 23:29:53 +0000 | [diff] [blame] | 3865 | GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment(); |
| 3866 | // This should only be called internally when we know we have a stencil buffer. |
| 3867 | SkASSERT(sb); |
| 3868 | GrStencilSettings settings = GrStencilSettings( |
| 3869 | *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits()); |
| 3870 | this->flushStencil(settings); |
| 3871 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3874 | bool GrGLGpu::createClearColorProgram() { |
| 3875 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 3876 | |
| 3877 | if (!fClearProgramArrayBuffer) { |
| 3878 | static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1}; |
| 3879 | fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType, |
| 3880 | kStatic_GrAccessPattern, vdata)); |
| 3881 | if (!fClearProgramArrayBuffer) { |
| 3882 | return false; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | SkASSERT(!fClearColorProgram.fProgram); |
| 3887 | GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram()); |
| 3888 | if (!fClearColorProgram.fProgram) { |
| 3889 | return false; |
| 3890 | } |
| 3891 | |
| 3892 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3893 | const char* version = this->caps()->shaderCaps()->versionDeclString(); |
| 3894 | |
| 3895 | SkString vshaderTxt(version); |
| 3896 | aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt); |
| 3897 | vshaderTxt.append(";"); |
| 3898 | vshaderTxt.append(R"( |
| 3899 | // Clear Color Program VS |
| 3900 | void main() { |
| 3901 | sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1); |
| 3902 | })"); |
| 3903 | |
| 3904 | GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier); |
| 3905 | SkString fshaderTxt(version); |
| 3906 | uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt); |
| 3907 | fshaderTxt.append(";"); |
| 3908 | fshaderTxt.appendf(R"( |
| 3909 | // Clear Color Program FS |
| 3910 | void main() { |
| 3911 | sk_FragColor = u_color; |
| 3912 | })"); |
| 3913 | |
| 3914 | const char* str; |
| 3915 | GrGLint length; |
| 3916 | |
| 3917 | str = vshaderTxt.c_str(); |
| 3918 | length = SkToInt(vshaderTxt.size()); |
| 3919 | SkSL::Program::Settings settings; |
| 3920 | settings.fCaps = this->caps()->shaderCaps(); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3921 | SkSL::String glsl; |
| 3922 | GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, &str, &length, 1, settings, &glsl); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3923 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3924 | GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(), |
| 3925 | &fStats, settings); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3926 | |
| 3927 | str = fshaderTxt.c_str(); |
| 3928 | length = SkToInt(fshaderTxt.size()); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3929 | GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3930 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3931 | GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(), |
| 3932 | &fStats, settings); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3933 | |
| 3934 | GL_CALL(LinkProgram(fClearColorProgram.fProgram)); |
| 3935 | |
| 3936 | GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex")); |
| 3937 | |
| 3938 | GL_CALL_RET(fClearColorProgram.fColorUniform, |
| 3939 | GetUniformLocation(fClearColorProgram.fProgram, "u_color")); |
| 3940 | |
| 3941 | GL_CALL(DeleteShader(vshader)); |
| 3942 | GL_CALL(DeleteShader(fshader)); |
| 3943 | |
| 3944 | return true; |
| 3945 | } |
| 3946 | |
| 3947 | void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b, |
| 3948 | GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) { |
| 3949 | if (!fClearColorProgram.fProgram) { |
| 3950 | if (!this->createClearColorProgram()) { |
| 3951 | SkDebugf("Failed to create clear color program.\n"); |
| 3952 | return; |
| 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | GrGLIRect dstVP; |
| 3957 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
| 3958 | this->flushViewport(dstVP); |
| 3959 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
| 3960 | |
| 3961 | GL_CALL(UseProgram(fClearColorProgram.fProgram)); |
| 3962 | fHWProgramID = fClearColorProgram.fProgram; |
| 3963 | |
| 3964 | fHWVertexArrayState.setVertexArrayID(this, 0); |
| 3965 | |
| 3966 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
| 3967 | attribs->enableVertexArrays(this, 1); |
| 3968 | attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
| 3969 | 2 * sizeof(GrGLfloat), 0); |
| 3970 | |
| 3971 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst); |
| 3972 | this->flushScissor(clip.scissorState(), glrt->getViewport(), origin); |
| 3973 | this->flushWindowRectangles(clip.windowRectsState(), glrt, origin); |
| 3974 | |
| 3975 | GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a)); |
| 3976 | |
| 3977 | GrXferProcessor::BlendInfo blendInfo; |
| 3978 | blendInfo.reset(); |
| 3979 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
| 3980 | this->flushColorWrite(true); |
| 3981 | this->flushHWAAState(nullptr, false, false); |
| 3982 | this->disableStencil(); |
| 3983 | if (this->glCaps().srgbWriteControl()) { |
| 3984 | this->flushFramebufferSRGB(true); |
| 3985 | } |
| 3986 | |
| 3987 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 3988 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 3989 | this->didWriteToSurface(dst, origin, clip.scissorEnabled() ? &clip.scissorRect() : nullptr); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 3990 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3991 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 3992 | bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 3993 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3994 | const SkIRect& srcRect, |
| 3995 | const SkIPoint& dstPoint) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3996 | GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture()); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 3997 | int progIdx = TextureToCopyProgramIdx(srcTex); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3998 | |
| 3999 | if (!fCopyPrograms[progIdx].fProgram) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 4000 | if (!this->createCopyProgram(srcTex)) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4001 | SkDebugf("Failed to create copy program.\n"); |
| 4002 | return false; |
| 4003 | } |
| 4004 | } |
| 4005 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4006 | int w = srcRect.width(); |
| 4007 | int h = srcRect.height(); |
| 4008 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 4009 | this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4010 | |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4011 | GrGLIRect dstVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4012 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4013 | this->flushViewport(dstVP); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4014 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4015 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4016 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4017 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 4018 | GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram)); |
| 4019 | fHWProgramID = fCopyPrograms[progIdx].fProgram; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4020 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4021 | fHWVertexArrayState.setVertexArrayID(this, 0); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4022 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4023 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 4024 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 4025 | attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 4026 | 2 * sizeof(GrGLfloat), 0); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4027 | |
| 4028 | // dst rect edges in NDC (-1 to 1) |
| 4029 | int dw = dst->width(); |
| 4030 | int dh = dst->height(); |
| 4031 | GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f; |
| 4032 | GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f; |
| 4033 | GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f; |
| 4034 | GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4035 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4036 | dy0 = -dy0; |
| 4037 | dy1 = -dy1; |
| 4038 | } |
| 4039 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 4040 | GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft; |
| 4041 | GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w); |
| 4042 | GrGLfloat sy0 = (GrGLfloat)srcRect.fTop; |
| 4043 | GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h); |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 4044 | int sw = src->width(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4045 | int sh = src->height(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4046 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 4047 | sy0 = sh - sy0; |
| 4048 | sy1 = sh - sy1; |
| 4049 | } |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 4050 | // src rect edges in normalized texture space (0 to 1) |
| 4051 | sx0 /= sw; |
| 4052 | sx1 /= sw; |
| 4053 | sy0 /= sh; |
| 4054 | sy1 /= sh; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4055 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 4056 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0)); |
| 4057 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 4058 | sx1 - sx0, sy1 - sy0, sx0, sy0)); |
| 4059 | GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4060 | |
| 4061 | GrXferProcessor::BlendInfo blendInfo; |
| 4062 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 4063 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4064 | this->flushColorWrite(true); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4065 | this->flushHWAAState(nullptr, false, false); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4066 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4067 | this->disableWindowRectangles(); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 4068 | this->disableStencil(); |
Brian Osman | f02fa6f | 2017-07-11 11:17:47 -0400 | [diff] [blame] | 4069 | if (this->glCaps().srgbWriteControl()) { |
| 4070 | this->flushFramebufferSRGB(true); |
| 4071 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4072 | |
| 4073 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4074 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4075 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4076 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4077 | return true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4080 | void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 4081 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4082 | const SkIRect& srcRect, |
| 4083 | const SkIPoint& dstPoint) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4084 | SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4085 | GrGLIRect srcVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4086 | this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4087 | GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4088 | SkASSERT(dstTex); |
| 4089 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4090 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4091 | GrGLIRect srcGLRect; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4092 | srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4093 | |
| 4094 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 4095 | GL_CALL(BindTexture(dstTex->target(), dstTex->textureID())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4096 | GrGLint dstY; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4097 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4098 | dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight); |
| 4099 | } else { |
| 4100 | dstY = dstPoint.fY; |
| 4101 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 4102 | GL_CALL(CopyTexSubImage2D(dstTex->target(), 0, |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4103 | dstPoint.fX, dstY, |
| 4104 | srcGLRect.fLeft, srcGLRect.fBottom, |
| 4105 | srcGLRect.fWidth, srcGLRect.fHeight)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4106 | this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 4107 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 4108 | srcRect.width(), srcRect.height()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4109 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4110 | } |
| 4111 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4112 | bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 4113 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4114 | const SkIRect& srcRect, |
| 4115 | const SkIPoint& dstPoint) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4116 | SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin, |
| 4117 | srcRect, dstPoint, this)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4118 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 4119 | srcRect.width(), srcRect.height()); |
| 4120 | if (dst == src) { |
| 4121 | if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) { |
| 4122 | return false; |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 4123 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 4124 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4125 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4126 | GrGLIRect dstVP; |
| 4127 | GrGLIRect srcVP; |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4128 | this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
| 4129 | this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4130 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4131 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4132 | GrGLIRect srcGLRect; |
| 4133 | GrGLIRect dstGLRect; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4134 | srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin); |
| 4135 | dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4136 | |
| 4137 | // BlitFrameBuffer respects the scissor, so disable it. |
| 4138 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4139 | this->disableWindowRectangles(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4140 | |
| 4141 | GrGLint srcY0; |
| 4142 | GrGLint srcY1; |
| 4143 | // Does the blit need to y-mirror or not? |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 4144 | if (srcOrigin == dstOrigin) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4145 | srcY0 = srcGLRect.fBottom; |
| 4146 | srcY1 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 4147 | } else { |
| 4148 | srcY0 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 4149 | srcY1 = srcGLRect.fBottom; |
| 4150 | } |
| 4151 | GL_CALL(BlitFramebuffer(srcGLRect.fLeft, |
| 4152 | srcY0, |
| 4153 | srcGLRect.fLeft + srcGLRect.fWidth, |
| 4154 | srcY1, |
| 4155 | dstGLRect.fLeft, |
| 4156 | dstGLRect.fBottom, |
| 4157 | dstGLRect.fLeft + dstGLRect.fWidth, |
| 4158 | dstGLRect.fBottom + dstGLRect.fHeight, |
| 4159 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 4160 | this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst); |
| 4161 | this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 4162 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4163 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4166 | // Manual implementation of mipmap generation, to work around driver bugs w/sRGB. |
| 4167 | // Uses draw calls to do a series of downsample operations to successive mips. |
| 4168 | // 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] | 4169 | bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin, |
| 4170 | bool gammaCorrect) { |
brianosman | 09563ce | 2016-06-02 08:59:34 -0700 | [diff] [blame] | 4171 | // Our iterative downsample requires the ability to limit which level we're sampling: |
| 4172 | if (!this->glCaps().doManualMipmapping()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4173 | return false; |
| 4174 | } |
| 4175 | |
| 4176 | // Mipmaps are only supported on 2D textures: |
| 4177 | if (GR_GL_TEXTURE_2D != texture->target()) { |
| 4178 | return false; |
| 4179 | } |
| 4180 | |
| 4181 | // 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] | 4182 | if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4183 | return false; |
| 4184 | } |
| 4185 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4186 | // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct: |
| 4187 | if (GrPixelConfigIsSRGB(texture->config())) { |
| 4188 | // If we have write-control, just set the state that we want: |
| 4189 | if (this->glCaps().srgbWriteControl()) { |
| 4190 | this->flushFramebufferSRGB(gammaCorrect); |
| 4191 | } else if (!gammaCorrect) { |
| 4192 | // If we don't have write-control we can't do non-gamma-correct mipmapping: |
| 4193 | return false; |
| 4194 | } |
| 4195 | } |
| 4196 | |
| 4197 | int width = texture->width(); |
| 4198 | int height = texture->height(); |
| 4199 | int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1; |
| 4200 | |
| 4201 | // Define all mips, if we haven't previously done so: |
| 4202 | if (0 == texture->texturePriv().maxMipMapLevel()) { |
| 4203 | GrGLenum internalFormat; |
| 4204 | GrGLenum externalFormat; |
| 4205 | GrGLenum externalType; |
| 4206 | if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(), |
| 4207 | &internalFormat, &externalFormat, &externalType)) { |
| 4208 | return false; |
| 4209 | } |
| 4210 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 4211 | this->unbindCpuToGpuXferBuffer(); |
| 4212 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4213 | for (GrGLint level = 1; level < levelCount; ++level) { |
| 4214 | // Define the next mip: |
| 4215 | width = SkTMax(1, width / 2); |
| 4216 | height = SkTMax(1, height / 2); |
| 4217 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, |
| 4218 | width, height, 0, |
| 4219 | externalFormat, externalType, nullptr)); |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | // Create (if necessary), then bind temporary FBO: |
| 4224 | if (0 == fTempDstFBOID) { |
| 4225 | GL_CALL(GenFramebuffers(1, &fTempDstFBOID)); |
| 4226 | } |
| 4227 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID)); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 4228 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4229 | |
| 4230 | // Bind the texture, to get things configured for filtering. |
| 4231 | // We'll be changing our base level further below: |
| 4232 | this->setTextureUnit(0); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 4233 | this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4234 | |
| 4235 | // Vertex data: |
| 4236 | if (!fMipmapProgramArrayBuffer) { |
| 4237 | static const GrGLfloat vdata[] = { |
| 4238 | 0, 0, |
| 4239 | 0, 1, |
| 4240 | 1, 0, |
| 4241 | 1, 1 |
| 4242 | }; |
| 4243 | fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), |
| 4244 | kVertex_GrBufferType, |
| 4245 | kStatic_GrAccessPattern, vdata)); |
| 4246 | } |
| 4247 | if (!fMipmapProgramArrayBuffer) { |
| 4248 | return false; |
| 4249 | } |
| 4250 | |
| 4251 | fHWVertexArrayState.setVertexArrayID(this, 0); |
| 4252 | |
| 4253 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 4254 | attribs->enableVertexArrays(this, 1); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 4255 | attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType, |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4256 | 2 * sizeof(GrGLfloat), 0); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4257 | |
| 4258 | // Set "simple" state once: |
| 4259 | GrXferProcessor::BlendInfo blendInfo; |
| 4260 | blendInfo.reset(); |
| 4261 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
| 4262 | this->flushColorWrite(true); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4263 | this->flushHWAAState(nullptr, false, false); |
| 4264 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 4265 | this->disableWindowRectangles(); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 4266 | this->disableStencil(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 4267 | |
| 4268 | // Do all the blits: |
| 4269 | width = texture->width(); |
| 4270 | height = texture->height(); |
| 4271 | GrGLIRect viewport; |
| 4272 | viewport.fLeft = 0; |
| 4273 | viewport.fBottom = 0; |
| 4274 | for (GrGLint level = 1; level < levelCount; ++level) { |
| 4275 | // Get and bind the program for this particular downsample (filter shape can vary): |
| 4276 | int progIdx = TextureSizeToMipmapProgramIdx(width, height); |
| 4277 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 4278 | if (!this->createMipmapProgram(progIdx)) { |
| 4279 | SkDebugf("Failed to create mipmap program.\n"); |
| 4280 | return false; |
| 4281 | } |
| 4282 | } |
| 4283 | GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram)); |
| 4284 | fHWProgramID = fMipmapPrograms[progIdx].fProgram; |
| 4285 | |
| 4286 | // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h) |
| 4287 | const float invWidth = 1.0f / width; |
| 4288 | const float invHeight = 1.0f / height; |
| 4289 | GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 4290 | invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight)); |
| 4291 | GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0)); |
| 4292 | |
| 4293 | // Only sample from previous mip |
| 4294 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1)); |
| 4295 | |
| 4296 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 4297 | GR_GL_TEXTURE_2D, texture->textureID(), level)); |
| 4298 | |
| 4299 | width = SkTMax(1, width / 2); |
| 4300 | height = SkTMax(1, height / 2); |
| 4301 | viewport.fWidth = width; |
| 4302 | viewport.fHeight = height; |
| 4303 | this->flushViewport(viewport); |
| 4304 | |
| 4305 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 4306 | } |
| 4307 | |
| 4308 | // Unbind: |
| 4309 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 4310 | GR_GL_TEXTURE_2D, 0, 0)); |
| 4311 | |
| 4312 | return true; |
| 4313 | } |
| 4314 | |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4315 | void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) { |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 4316 | SkASSERT(type); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4317 | switch (type) { |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4318 | case kTexture_GrXferBarrierType: { |
| 4319 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt); |
Brian Osman | cfe83d1 | 2018-01-19 11:13:45 -0500 | [diff] [blame] | 4320 | SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0); |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4321 | if (glrt->textureFBOID() != glrt->renderFBOID()) { |
| 4322 | // The render target uses separate storage so no need for glTextureBarrier. |
| 4323 | // FIXME: The render target will resolve automatically when its texture is bound, |
| 4324 | // but we could resolve only the bounds that will be read if we do it here instead. |
| 4325 | return; |
| 4326 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4327 | SkASSERT(this->caps()->textureBarrierSupport()); |
| 4328 | GL_CALL(TextureBarrier()); |
| 4329 | return; |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 4330 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 4331 | case kBlend_GrXferBarrierType: |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 4332 | SkASSERT(GrCaps::kAdvanced_BlendEquationSupport == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 4333 | this->caps()->blendEquationSupport()); |
| 4334 | GL_CALL(BlendBarrier()); |
| 4335 | return; |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 4336 | default: break; // placate compiler warnings that kNone not handled |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4340 | #if GR_TEST_UTILS |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 4341 | GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h, |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4342 | GrPixelConfig config, bool /*isRT*/, |
| 4343 | GrMipMapped mipMapped) { |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 4344 | this->handleDirtyContext(); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4345 | if (!this->caps()->isConfigTexturable(config)) { |
| 4346 | return GrBackendTexture(); // invalid |
| 4347 | } |
| 4348 | |
| 4349 | // Currently we don't support uploading pixel data when mipped. |
| 4350 | if (pixels && GrMipMapped::kYes == mipMapped) { |
| 4351 | return GrBackendTexture(); // invalid |
| 4352 | } |
| 4353 | |
| 4354 | GrGLTextureInfo info; |
| 4355 | info.fTarget = GR_GL_TEXTURE_2D; |
| 4356 | info.fID = 0; |
| 4357 | GL_CALL(GenTextures(1, &info.fID)); |
| 4358 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0)); |
| 4359 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 4360 | GL_CALL(BindTexture(info.fTarget, info.fID)); |
| 4361 | fHWBoundTextureUniqueIDs[0].makeInvalid(); |
| 4362 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); |
| 4363 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 4364 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); |
| 4365 | GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE)); |
| 4366 | |
| 4367 | GrGLenum internalFormat; |
| 4368 | GrGLenum externalFormat; |
| 4369 | GrGLenum externalType; |
| 4370 | |
| 4371 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 4372 | &externalType)) { |
| 4373 | return GrBackendTexture(); // invalid |
| 4374 | } |
| 4375 | |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 4376 | info.fFormat = this->glCaps().configSizedInternalFormat(config); |
| 4377 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4378 | this->unbindCpuToGpuXferBuffer(); |
| 4379 | |
| 4380 | // Figure out the number of mip levels. |
| 4381 | int mipLevels = 1; |
| 4382 | if (GrMipMapped::kYes == mipMapped) { |
| 4383 | mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1; |
| 4384 | } |
| 4385 | |
| 4386 | size_t bpp = GrBytesPerPixel(config); |
| 4387 | size_t baseLayerSize = bpp * w * h; |
| 4388 | SkAutoMalloc defaultStorage(baseLayerSize); |
| 4389 | if (!pixels) { |
| 4390 | // Fill in the texture with all zeros so we don't have random garbage |
| 4391 | pixels = defaultStorage.get(); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 4392 | memset(defaultStorage.get(), 0, baseLayerSize); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | int width = w; |
| 4396 | int height = h; |
| 4397 | for (int i = 0; i < mipLevels; ++i) { |
| 4398 | GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat, |
| 4399 | externalType, pixels)); |
| 4400 | width = SkTMax(1, width / 2); |
| 4401 | height = SkTMax(1, height / 2); |
| 4402 | } |
| 4403 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 4404 | // unbind the texture from the texture unit to avoid asserts |
| 4405 | GL_CALL(BindTexture(info.fTarget, 0)); |
| 4406 | |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 4407 | return GrBackendTexture(w, h, mipMapped, info); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
| 4411 | SkASSERT(kOpenGL_GrBackend == tex.backend()); |
| 4412 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 4413 | GrGLTextureInfo info; |
| 4414 | if (!tex.getGLTextureInfo(&info)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4415 | return false; |
| 4416 | } |
| 4417 | |
| 4418 | GrGLboolean result; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 4419 | GL_CALL_RET(result, IsTexture(info.fID)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4420 | |
| 4421 | return (GR_GL_TRUE == result); |
| 4422 | } |
| 4423 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 4424 | void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) { |
| 4425 | SkASSERT(kOpenGL_GrBackend == tex.backend()); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4426 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 4427 | GrGLTextureInfo info; |
| 4428 | if (tex.getGLTextureInfo(&info)) { |
| 4429 | GL_CALL(DeleteTextures(1, &info.fID)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4433 | GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
| 4434 | GrColorType colorType, |
| 4435 | GrSRGBEncoded srgbEncoded) { |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 4436 | this->handleDirtyContext(); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4437 | auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded); |
| 4438 | GrGLenum colorBufferFormat; |
| 4439 | if (!this->glCaps().getRenderbufferFormat(config, &colorBufferFormat)) { |
| 4440 | return {}; |
| 4441 | } |
| 4442 | int sFormatIdx = this->getCompatibleStencilIndex(config); |
| 4443 | if (sFormatIdx < 0) { |
| 4444 | return {}; |
| 4445 | } |
| 4446 | GrGLuint rbIDs[] = {0, 0}; |
| 4447 | GL_CALL(GenRenderbuffers(2, rbIDs)); |
| 4448 | if (!rbIDs[0] || !rbIDs[1]) { |
| 4449 | if (!rbIDs[0]) { |
| 4450 | GL_CALL(DeleteRenderbuffers(1, &rbIDs[0])); |
| 4451 | } |
| 4452 | if (!rbIDs[1]) { |
| 4453 | GL_CALL(DeleteRenderbuffers(1, &rbIDs[1])); |
| 4454 | } |
| 4455 | return {}; |
| 4456 | } |
| 4457 | GrGLFramebufferInfo info; |
| 4458 | info.fFBOID = 0; |
| 4459 | GL_CALL(GenFramebuffers(1, &info.fFBOID)); |
| 4460 | if (!info.fFBOID) { |
| 4461 | GL_CALL(DeleteRenderbuffers(2, rbIDs)); |
| 4462 | return GrBackendRenderTarget(); |
| 4463 | } |
| 4464 | |
| 4465 | this->invalidateBoundRenderTarget(); |
| 4466 | |
| 4467 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID)); |
| 4468 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[0])); |
| 4469 | GL_ALLOC_CALL(this->glInterface(), |
| 4470 | RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h)); |
| 4471 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER, |
| 4472 | rbIDs[0])); |
| 4473 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[1])); |
| 4474 | auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat; |
| 4475 | GL_ALLOC_CALL(this->glInterface(), |
| 4476 | RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h)); |
| 4477 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, |
| 4478 | rbIDs[1])); |
| 4479 | if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) { |
| 4480 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, |
| 4481 | GR_GL_RENDERBUFFER, rbIDs[1])); |
| 4482 | } |
| 4483 | |
| 4484 | // We don't want to have to recover the renderbuffer IDs later to delete them. OpenGL has this |
| 4485 | // rule that if a renderbuffer is deleted and a FBO other than the current FBO has the RB |
| 4486 | // attached then deletion is delayed. So we unbind the FBO here and delete the renderbuffers. |
| 4487 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); |
| 4488 | GL_CALL(DeleteRenderbuffers(2, rbIDs)); |
| 4489 | |
| 4490 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID)); |
| 4491 | GrGLenum status; |
| 4492 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 4493 | if (GR_GL_FRAMEBUFFER_COMPLETE != status) { |
| 4494 | GL_CALL(DeleteFramebuffers(1, &info.fFBOID)); |
| 4495 | return {}; |
| 4496 | } |
| 4497 | auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits); |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 4498 | return {w, h, 1, stencilBits, config, info}; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
| 4502 | SkASSERT(kOpenGL_GrBackend == backendRT.backend()); |
| 4503 | if (auto info = backendRT.getGLFramebufferInfo()) { |
| 4504 | if (info->fFBOID) { |
| 4505 | GL_CALL(DeleteFramebuffers(1, &info->fFBOID)); |
| 4506 | } |
| 4507 | } |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 4508 | } |
| 4509 | |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 4510 | void GrGLGpu::testingOnly_flushGpuAndSync() { |
| 4511 | GL_CALL(Finish()); |
| 4512 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 4513 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 4514 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4515 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4516 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4517 | GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 4518 | const GrBuffer* ibuf) { |
robertphillips@google.com | 4f65a27 | 2013-03-26 19:40:46 +0000 | [diff] [blame] | 4519 | GrGLAttribArrayState* attribState; |
| 4520 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4521 | if (gpu->glCaps().isCoreProfile()) { |
| 4522 | if (!fCoreProfileVertexArray) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4523 | GrGLuint arrayID; |
| 4524 | GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID)); |
| 4525 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4526 | fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount); |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4527 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4528 | if (ibuf) { |
| 4529 | attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4530 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4531 | attribState = fCoreProfileVertexArray->bind(gpu); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 4532 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4533 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 4534 | if (ibuf) { |
| 4535 | // bindBuffer implicitly binds VAO 0 when binding an index buffer. |
| 4536 | gpu->bindBuffer(kIndex_GrBufferType, ibuf); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4537 | } else { |
| 4538 | this->setVertexArrayID(gpu, 0); |
| 4539 | } |
| 4540 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
| 4541 | if (fDefaultVertexArrayAttribState.count() != attrCount) { |
| 4542 | fDefaultVertexArrayAttribState.resize(attrCount); |
| 4543 | } |
| 4544 | attribState = &fDefaultVertexArrayAttribState; |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 4545 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 4546 | return attribState; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 4547 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 4548 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 4549 | void GrGLGpu::onFinishFlush(bool insertedSemaphore) { |
| 4550 | // If we inserted semaphores during the flush, we need to call GLFlush. |
| 4551 | if (insertedSemaphore) { |
| 4552 | GL_CALL(Flush()); |
| 4553 | } |
| 4554 | } |
| 4555 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4556 | GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4557 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4558 | GrGLsync sync; |
| 4559 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 4560 | GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync)); |
| 4561 | return (GrFence)sync; |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4564 | bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) { |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4565 | GrGLenum result; |
| 4566 | GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout)); |
| 4567 | return (GR_GL_CONDITION_SATISFIED == result); |
| 4568 | } |
| 4569 | |
| 4570 | void GrGLGpu::deleteFence(GrFence fence) const { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4571 | this->deleteSync((GrGLsync)fence); |
| 4572 | } |
| 4573 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4574 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4575 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4576 | return GrGLSemaphore::Make(this, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4577 | } |
| 4578 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4579 | sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 4580 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 4581 | GrWrapOwnership ownership) { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 4582 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4583 | return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership); |
| 4584 | } |
| 4585 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4586 | void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4587 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get()); |
| 4588 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4589 | GrGLsync sync; |
| 4590 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 4591 | glSem->setSync(sync); |
Brian Osman | dc87c95 | 2017-04-28 13:57:38 -0400 | [diff] [blame] | 4592 | |
| 4593 | if (flush) { |
| 4594 | GL_CALL(Flush()); |
| 4595 | } |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4596 | } |
| 4597 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4598 | void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4599 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get()); |
| 4600 | |
| 4601 | GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED)); |
| 4602 | } |
| 4603 | |
| 4604 | void GrGLGpu::deleteSync(GrGLsync sync) const { |
| 4605 | GL_CALL(DeleteSync(sync)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4606 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4607 | |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 4608 | void GrGLGpu::insertEventMarker(const char* msg) { |
| 4609 | GL_CALL(InsertEventMarker(strlen(msg), msg)); |
| 4610 | } |
| 4611 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4612 | sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) { |
| 4613 | // 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] | 4614 | sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4615 | this->insertSemaphore(semaphore, true); |
| 4616 | |
| 4617 | return semaphore; |
| 4618 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4619 | |
| 4620 | int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) { |
| 4621 | switch (texture->texturePriv().samplerType()) { |
| 4622 | case kTexture2DSampler_GrSLType: |
| 4623 | return 0; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4624 | case kTexture2DRectSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4625 | return 1; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4626 | case kTextureExternalSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4627 | return 2; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4628 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 4629 | SK_ABORT("Unexpected samper type"); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4630 | return 0; |
| 4631 | } |
| 4632 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 4633 | |
| 4634 | void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const { |
| 4635 | // We are called by the base class, which has already called beginObject(). We choose to nest |
| 4636 | // all of our caps information in a named sub-object. |
| 4637 | writer->beginObject("GL GPU"); |
| 4638 | |
| 4639 | const GrGLubyte* str; |
| 4640 | GL_CALL_RET(str, GetString(GR_GL_VERSION)); |
| 4641 | writer->appendString("GL_VERSION", (const char*)(str)); |
| 4642 | GL_CALL_RET(str, GetString(GR_GL_RENDERER)); |
| 4643 | writer->appendString("GL_RENDERER", (const char*)(str)); |
| 4644 | GL_CALL_RET(str, GetString(GR_GL_VENDOR)); |
| 4645 | writer->appendString("GL_VENDOR", (const char*)(str)); |
| 4646 | GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 4647 | writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str)); |
| 4648 | |
| 4649 | writer->appendName("extensions"); |
| 4650 | glInterface()->fExtensions.dumpJSON(writer); |
| 4651 | |
| 4652 | writer->endObject(); |
| 4653 | } |