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