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