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