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