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