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