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