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