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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 9 | #include "GrGLGpu.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 10 | #include "GrGLGLSL.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 11 | #include "GrGLStencilAttachment.h" |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 12 | #include "GrGLTextureRenderTarget.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 14 | #include "GrPipeline.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 15 | #include "GrRenderTargetPriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 16 | #include "GrSurfacePriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 17 | #include "GrTexturePriv.h" |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 18 | #include "GrTypes.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 19 | #include "GrVertices.h" |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 20 | #include "builders/GrGLShaderStringBuilder.h" |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 21 | #include "glsl/GrGLSL.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 22 | #include "glsl/GrGLSLCaps.h" |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 23 | #include "SkStrokeRec.h" |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 24 | #include "SkTemplates.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 26 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 27 | #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] | 28 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | #define SKIP_CACHE_CHECK true |
| 30 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 31 | #if GR_GL_CHECK_ALLOC_WITH_GET_ERROR |
| 32 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface) |
| 33 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call) |
| 34 | #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | #else |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 36 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) |
| 37 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call) |
| 38 | #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR |
| 39 | #endif |
| 40 | |
ethannicholas | 5366a09 | 2016-01-22 09:45:47 -0800 | [diff] [blame] | 41 | |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 44 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 45 | static const GrGLenum gXfermodeEquation2Blend[] = { |
| 46 | // Basic OpenGL blend equations. |
| 47 | GR_GL_FUNC_ADD, |
| 48 | GR_GL_FUNC_SUBTRACT, |
| 49 | GR_GL_FUNC_REVERSE_SUBTRACT, |
| 50 | |
| 51 | // GL_KHR_blend_equation_advanced. |
| 52 | GR_GL_SCREEN, |
| 53 | GR_GL_OVERLAY, |
| 54 | GR_GL_DARKEN, |
| 55 | GR_GL_LIGHTEN, |
| 56 | GR_GL_COLORDODGE, |
| 57 | GR_GL_COLORBURN, |
| 58 | GR_GL_HARDLIGHT, |
| 59 | GR_GL_SOFTLIGHT, |
| 60 | GR_GL_DIFFERENCE, |
| 61 | GR_GL_EXCLUSION, |
| 62 | GR_GL_MULTIPLY, |
| 63 | GR_GL_HSL_HUE, |
| 64 | GR_GL_HSL_SATURATION, |
| 65 | GR_GL_HSL_COLOR, |
| 66 | GR_GL_HSL_LUMINOSITY |
| 67 | }; |
| 68 | GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation); |
| 69 | GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation); |
| 70 | GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation); |
| 71 | GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation); |
| 72 | GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation); |
| 73 | GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation); |
| 74 | GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation); |
| 75 | GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation); |
| 76 | GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation); |
| 77 | GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation); |
| 78 | GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation); |
| 79 | GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation); |
| 80 | GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation); |
| 81 | GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation); |
| 82 | GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation); |
| 83 | GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation); |
| 84 | GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation); |
| 85 | GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation); |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 86 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 87 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 88 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 89 | GR_GL_ZERO, |
| 90 | GR_GL_ONE, |
| 91 | GR_GL_SRC_COLOR, |
| 92 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 93 | GR_GL_DST_COLOR, |
| 94 | GR_GL_ONE_MINUS_DST_COLOR, |
| 95 | GR_GL_SRC_ALPHA, |
| 96 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 97 | GR_GL_DST_ALPHA, |
| 98 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 99 | GR_GL_CONSTANT_COLOR, |
| 100 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 101 | GR_GL_CONSTANT_ALPHA, |
| 102 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 103 | |
| 104 | // extended blend coeffs |
| 105 | GR_GL_SRC1_COLOR, |
| 106 | GR_GL_ONE_MINUS_SRC1_COLOR, |
| 107 | GR_GL_SRC1_ALPHA, |
| 108 | GR_GL_ONE_MINUS_SRC1_ALPHA, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 111 | bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 112 | static const bool gCoeffReferencesBlendConst[] = { |
| 113 | false, |
| 114 | false, |
| 115 | false, |
| 116 | false, |
| 117 | false, |
| 118 | false, |
| 119 | false, |
| 120 | false, |
| 121 | false, |
| 122 | false, |
| 123 | true, |
| 124 | true, |
| 125 | true, |
| 126 | true, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 127 | |
| 128 | // extended blend coeffs |
| 129 | false, |
| 130 | false, |
| 131 | false, |
| 132 | false, |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 133 | }; |
| 134 | return gCoeffReferencesBlendConst[coeff]; |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 135 | GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 136 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 137 | GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff); |
| 138 | GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff); |
| 139 | GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff); |
| 140 | GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff); |
| 141 | GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff); |
| 142 | GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff); |
| 143 | GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff); |
| 144 | GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff); |
| 145 | GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff); |
| 146 | GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff); |
| 147 | GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff); |
| 148 | GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff); |
| 149 | GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff); |
| 150 | GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 152 | GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff); |
| 153 | GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff); |
| 154 | GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff); |
| 155 | GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 156 | |
| 157 | // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 158 | GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 163 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 164 | GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options, |
| 165 | GrContext* context) { |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 166 | SkAutoTUnref<const GrGLInterface> glInterface( |
| 167 | reinterpret_cast<const GrGLInterface*>(backendContext)); |
| 168 | if (!glInterface) { |
| 169 | glInterface.reset(GrGLDefaultInterface()); |
| 170 | } else { |
| 171 | glInterface->ref(); |
| 172 | } |
| 173 | if (!glInterface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | return nullptr; |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 175 | } |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 176 | GrGLContext* glContext = GrGLContext::Create(glInterface, options); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 177 | if (glContext) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 178 | return new GrGLGpu(glContext, context); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 179 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | return nullptr; |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 181 | } |
| 182 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 183 | static bool gPrintStartupSpew; |
bsalomon@google.com | 42ab7ea | 2011-01-19 17:19:40 +0000 | [diff] [blame] | 184 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 185 | GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context) |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 186 | : GrGpu(context) |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 187 | , fGLContext(ctx) { |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 188 | SkASSERT(ctx); |
| 189 | fCaps.reset(SkRef(ctx->caps())); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 190 | |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 191 | fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 192 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 193 | GrGLClearErr(this->glInterface()); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 194 | if (gPrintStartupSpew) { |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 195 | const GrGLubyte* vendor; |
| 196 | const GrGLubyte* renderer; |
| 197 | const GrGLubyte* version; |
| 198 | GL_CALL_RET(vendor, GetString(GR_GL_VENDOR)); |
| 199 | GL_CALL_RET(renderer, GetString(GR_GL_RENDERER)); |
| 200 | GL_CALL_RET(version, GetString(GR_GL_VERSION)); |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 201 | SkDebugf("------------------------- create GrGLGpu %p --------------\n", |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 202 | this); |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 203 | SkDebugf("------ VENDOR %s\n", vendor); |
| 204 | SkDebugf("------ RENDERER %s\n", renderer); |
| 205 | SkDebugf("------ VERSION %s\n", version); |
| 206 | SkDebugf("------ EXTENSIONS\n"); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 207 | this->glContext().extensions().print(); |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 208 | SkDebugf("\n"); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 209 | SkDebugf("%s", this->glCaps().dump().c_str()); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 210 | } |
| 211 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 212 | fProgramCache = new ProgramCache(this); |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 213 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 214 | SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 215 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 216 | fHWProgramID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 217 | fTempSrcFBOID = 0; |
| 218 | fTempDstFBOID = 0; |
| 219 | fStencilClearFBOID = 0; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 220 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 221 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 222 | fPathRendering.reset(new GrGLPathRendering(this)); |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 223 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 224 | this->createCopyPrograms(); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 225 | fWireRectProgram.fProgram = 0; |
| 226 | fWireRectArrayBuffer = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 227 | } |
| 228 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 229 | GrGLGpu::~GrGLGpu() { |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 230 | // Delete the path rendering explicitly, since it will need working gpu object to release the |
| 231 | // resources the object itself holds. |
| 232 | fPathRendering.reset(); |
| 233 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 234 | if (0 != fHWProgramID) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 235 | // detach the current program so there is no confusion on OpenGL's part |
| 236 | // that we want it to be deleted |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 237 | GL_CALL(UseProgram(0)); |
| 238 | } |
| 239 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 240 | if (0 != fTempSrcFBOID) { |
| 241 | GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 242 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 243 | if (0 != fTempDstFBOID) { |
| 244 | GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 245 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 246 | if (0 != fStencilClearFBOID) { |
| 247 | GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID)); |
bsalomon | dd3143b | 2015-02-23 09:27:45 -0800 | [diff] [blame] | 248 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 249 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 250 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 251 | if (0 != fCopyPrograms[i].fProgram) { |
| 252 | GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram)); |
| 253 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 254 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 255 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 256 | if (0 != fCopyProgramArrayBuffer) { |
| 257 | GL_CALL(DeleteBuffers(1, &fCopyProgramArrayBuffer)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 258 | } |
| 259 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 260 | if (0 != fWireRectProgram.fProgram) { |
| 261 | GL_CALL(DeleteProgram(fWireRectProgram.fProgram)); |
| 262 | } |
| 263 | |
| 264 | if (0 != fWireRectArrayBuffer) { |
| 265 | GL_CALL(DeleteBuffers(1, &fWireRectArrayBuffer)); |
| 266 | } |
| 267 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 268 | delete fProgramCache; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 269 | } |
| 270 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 271 | void GrGLGpu::contextAbandoned() { |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 272 | INHERITED::contextAbandoned(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 273 | fProgramCache->abandon(); |
| 274 | fHWProgramID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 275 | fTempSrcFBOID = 0; |
| 276 | fTempDstFBOID = 0; |
| 277 | fStencilClearFBOID = 0; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 278 | fCopyProgramArrayBuffer = 0; |
| 279 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 280 | fCopyPrograms[i].fProgram = 0; |
| 281 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 282 | fWireRectProgram.fProgram = 0; |
| 283 | fWireRectArrayBuffer = 0; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 284 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 285 | this->glPathRendering()->abandonGpuResources(); |
| 286 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 287 | } |
| 288 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 289 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 290 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 291 | void GrGLGpu::onResetContext(uint32_t resetBits) { |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 292 | // we don't use the zb at all |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 293 | if (resetBits & kMisc_GrGLBackendState) { |
| 294 | GL_CALL(Disable(GR_GL_DEPTH_TEST)); |
| 295 | GL_CALL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 296 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 297 | fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 298 | |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 299 | if (kGL_GrGLStandard == this->glStandard()) { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 300 | // Desktop-only state that we never change |
| 301 | if (!this->glCaps().isCoreProfile()) { |
| 302 | GL_CALL(Disable(GR_GL_POINT_SMOOTH)); |
| 303 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 304 | GL_CALL(Disable(GR_GL_POLYGON_SMOOTH)); |
| 305 | GL_CALL(Disable(GR_GL_POLYGON_STIPPLE)); |
| 306 | GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP)); |
| 307 | GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP)); |
| 308 | } |
| 309 | // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a |
| 310 | // core profile. This seems like a bug since the core spec removes any mention of |
| 311 | // GL_ARB_imaging. |
| 312 | if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) { |
| 313 | GL_CALL(Disable(GR_GL_COLOR_TABLE)); |
| 314 | } |
| 315 | GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL)); |
| 316 | // Since ES doesn't support glPointSize at all we always use the VS to |
| 317 | // set the point size |
| 318 | GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE)); |
| 319 | |
| 320 | // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't |
| 321 | // currently part of our gl interface. There are probably others as |
| 322 | // well. |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 323 | } |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 324 | |
| 325 | if (kGLES_GrGLStandard == this->glStandard() && |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 326 | this->hasExtension("GL_ARM_shader_framebuffer_fetch")) { |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 327 | // The arm extension requires specifically enabling MSAA fetching per sample. |
| 328 | // On some devices this may have a perf hit. Also multiple render targets are disabled |
| 329 | GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM)); |
| 330 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 331 | fHWWriteToColor = kUnknown_TriState; |
| 332 | // we only ever use lines in hairline mode |
| 333 | GL_CALL(LineWidth(1)); |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 334 | GL_CALL(Disable(GR_GL_DITHER)); |
bsalomon@google.com | cad107b | 2013-06-28 14:32:08 +0000 | [diff] [blame] | 335 | } |
edisonn@google.com | ba66999 | 2013-06-28 16:03:21 +0000 | [diff] [blame] | 336 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 337 | if (resetBits & kMSAAEnable_GrGLBackendState) { |
| 338 | fMSAAEnabled = kUnknown_TriState; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 339 | |
| 340 | // In mixed samples mode coverage modulation allows the coverage to be converted to |
| 341 | // "opacity", which can then be blended into the color buffer to accomplish antialiasing. |
| 342 | // Enable coverage modulation suitable for premultiplied alpha colors. |
| 343 | // This state has no effect when not rendering to a mixed sampled target. |
egdaniel | eed519e | 2016-01-15 11:36:18 -0800 | [diff] [blame] | 344 | if (this->caps()->usesMixedSamples()) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 345 | GL_CALL(CoverageModulation(GR_GL_RGBA)); |
| 346 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 347 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 348 | |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 349 | fHWActiveTextureUnitIdx = -1; // invalid |
| 350 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 351 | if (resetBits & kTextureBinding_GrGLBackendState) { |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 352 | for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) { |
| 353 | fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 354 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 355 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 356 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 357 | if (resetBits & kBlend_GrGLBackendState) { |
| 358 | fHWBlendState.invalidate(); |
| 359 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 360 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 361 | if (resetBits & kView_GrGLBackendState) { |
| 362 | fHWScissorSettings.invalidate(); |
| 363 | fHWViewport.invalidate(); |
| 364 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 366 | if (resetBits & kStencil_GrGLBackendState) { |
| 367 | fHWStencilSettings.invalidate(); |
| 368 | fHWStencilTestEnabled = kUnknown_TriState; |
| 369 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 370 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 371 | // Vertex |
| 372 | if (resetBits & kVertex_GrGLBackendState) { |
| 373 | fHWGeometryState.invalidate(); |
| 374 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 376 | if (resetBits & kRenderTarget_GrGLBackendState) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 377 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 378 | fHWSRGBFramebuffer = kUnknown_TriState; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 379 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 380 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 381 | if (resetBits & kPathRendering_GrGLBackendState) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 382 | if (this->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 383 | this->glPathRendering()->resetContext(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 384 | } |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 385 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 386 | |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 387 | // we assume these values |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 388 | if (resetBits & kPixelStore_GrGLBackendState) { |
| 389 | if (this->glCaps().unpackRowLengthSupport()) { |
| 390 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 391 | } |
| 392 | if (this->glCaps().packRowLengthSupport()) { |
| 393 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 394 | } |
| 395 | if (this->glCaps().unpackFlipYSupport()) { |
| 396 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 397 | } |
| 398 | if (this->glCaps().packFlipYSupport()) { |
| 399 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 400 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 401 | } |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 402 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 403 | if (resetBits & kProgram_GrGLBackendState) { |
| 404 | fHWProgramID = 0; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 405 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 406 | } |
| 407 | |
egdaniel | cf614fd | 2015-04-22 13:58:58 -0700 | [diff] [blame] | 408 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) { |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 409 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 410 | // can be drawn to by the outside world without the client having |
| 411 | // to render upside down. |
| 412 | if (kDefault_GrSurfaceOrigin == origin) { |
| 413 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 414 | } else { |
| 415 | return origin; |
| 416 | } |
| 417 | } |
| 418 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 419 | GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc, |
| 420 | GrWrapOwnership ownership) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 421 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
| 422 | if (!desc.fTextureHandle) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 423 | return nullptr; |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 424 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 425 | #else |
| 426 | const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle); |
| 427 | if (!info || !info->fID) { |
| 428 | return nullptr; |
| 429 | } |
| 430 | #endif |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 431 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 432 | int maxSize = this->caps()->maxTextureSize(); |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 433 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 434 | return nullptr; |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 435 | } |
| 436 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 437 | // next line relies on GrBackendTextureDesc's flags matching GrTexture's |
| 438 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 439 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 440 | GrGLTexture::IDDesc idDesc; |
| 441 | GrSurfaceDesc surfDesc; |
| 442 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 443 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
| 444 | idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 445 | // We only support GL_TEXTURE_2D at the moment. |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 446 | idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D; |
| 447 | #else |
| 448 | idDesc.fInfo = *info; |
| 449 | #endif |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 450 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 451 | if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) { |
| 452 | if (renderTarget) { |
| 453 | // This combination is not supported. |
| 454 | return nullptr; |
| 455 | } |
| 456 | if (!this->glCaps().externalTextureSupport()) { |
| 457 | return nullptr; |
| 458 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 459 | } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) { |
| 460 | if (!this->glCaps().rectangleTextureSupport()) { |
| 461 | return nullptr; |
| 462 | } |
| 463 | } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) { |
| 464 | return nullptr; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 465 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 466 | |
| 467 | // 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] | 468 | // for a render buffer that resolves to the texture. We don't support MSAA textures. |
| 469 | if (desc.fSampleCnt && !renderTarget) { |
| 470 | return nullptr; |
| 471 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 472 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 473 | switch (ownership) { |
| 474 | case kAdopt_GrWrapOwnership: |
| 475 | idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle; |
| 476 | break; |
| 477 | case kBorrow_GrWrapOwnership: |
| 478 | idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle; |
| 479 | break; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 480 | } |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 481 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 482 | surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags; |
| 483 | surfDesc.fWidth = desc.fWidth; |
| 484 | surfDesc.fHeight = desc.fHeight; |
| 485 | surfDesc.fConfig = desc.fConfig; |
senorblanco | 94e5010 | 2015-04-06 09:42:57 -0700 | [diff] [blame] | 486 | surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 487 | // FIXME: this should be calling resolve_origin(), but Chrome code is currently |
| 488 | // assuming the old behaviour, which is that backend textures are always |
| 489 | // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to: |
| 490 | // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget); |
| 491 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 492 | surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 493 | } else { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 494 | surfDesc.fOrigin = desc.fOrigin; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 495 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 496 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 497 | GrGLTexture* texture = nullptr; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 498 | if (renderTarget) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 499 | GrGLRenderTarget::IDDesc rtIDDesc; |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 500 | if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 501 | idDesc.fInfo, &rtIDDesc)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 502 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 503 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 504 | texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 505 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 506 | texture = new GrGLTexture(this, surfDesc, idDesc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 507 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 508 | if (nullptr == texture) { |
| 509 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 510 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 511 | |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 512 | return texture; |
| 513 | } |
| 514 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 515 | GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc, |
| 516 | GrWrapOwnership ownership) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 517 | GrGLRenderTarget::IDDesc idDesc; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 518 | idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 519 | idDesc.fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 520 | idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 521 | switch (ownership) { |
| 522 | case kAdopt_GrWrapOwnership: |
| 523 | idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle; |
| 524 | break; |
| 525 | case kBorrow_GrWrapOwnership: |
| 526 | idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle; |
| 527 | break; |
| 528 | } |
senorblanco | d7395d8 | 2015-06-18 13:26:52 -0700 | [diff] [blame] | 529 | idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 530 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 531 | GrSurfaceDesc desc; |
| 532 | desc.fConfig = wrapDesc.fConfig; |
senorblanco | 4b01329 | 2015-08-19 09:10:28 -0700 | [diff] [blame] | 533 | desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag; |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 534 | desc.fWidth = wrapDesc.fWidth; |
| 535 | desc.fHeight = wrapDesc.fHeight; |
senorblanco | 94e5010 | 2015-04-06 09:42:57 -0700 | [diff] [blame] | 536 | desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount()); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 537 | desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true); |
| 538 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 539 | return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 540 | } |
| 541 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 542 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 543 | bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
| 544 | size_t rowBytes, GrPixelConfig srcConfig, |
| 545 | DrawPreference* drawPreference, |
| 546 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 547 | if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) { |
| 548 | return false; |
| 549 | } |
| 550 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 551 | // This subclass only allows writes to textures. If the dst is not a texture we have to draw |
| 552 | // into it. We could use glDrawPixels on GLs that have it, but we don't today. |
| 553 | if (!dstSurface->asTexture()) { |
| 554 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 555 | } else { |
| 556 | GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 557 | if (GR_GL_TEXTURE_EXTERNAL == texture->target()) { |
| 558 | // We don't currently support writing pixels to EXTERNAL textures. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 559 | return false; |
| 560 | } |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 561 | } |
| 562 | |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 563 | if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) { |
| 564 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 565 | } |
| 566 | |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 567 | // Start off assuming no swizzling |
| 568 | tempDrawInfo->fSwizzle = GrSwizzle::RGBA(); |
| 569 | tempDrawInfo->fWriteConfig = srcConfig; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 570 | |
| 571 | // These settings we will always want if a temp draw is performed. Initially set the config |
| 572 | // to srcConfig, though that may be modified if we decide to do a R/G swap. |
| 573 | tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags; |
| 574 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
| 575 | tempDrawInfo->fTempSurfaceDesc.fWidth = width; |
| 576 | tempDrawInfo->fTempSurfaceDesc.fHeight = height; |
| 577 | tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0; |
| 578 | tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL. |
| 579 | |
| 580 | bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config(); |
| 581 | |
| 582 | if (configsAreRBSwaps) { |
| 583 | if (!this->caps()->isConfigTexturable(srcConfig)) { |
| 584 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 585 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 586 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 587 | tempDrawInfo->fWriteConfig = dstSurface->config(); |
bsalomon | 88c7b98 | 2015-07-31 11:20:16 -0700 | [diff] [blame] | 588 | } else if (this->glCaps().rgba8888PixelsOpsAreSlow() && |
| 589 | kRGBA_8888_GrPixelConfig == srcConfig) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 590 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 591 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 592 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 593 | tempDrawInfo->fWriteConfig = dstSurface->config(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 594 | } else if (kGLES_GrGLStandard == this->glStandard() && |
| 595 | this->glCaps().bgraIsInternalFormat()) { |
| 596 | // The internal format and external formats must match texture uploads so we can't |
| 597 | // swizzle while uploading when BGRA is a distinct internal format. |
| 598 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 599 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 600 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 601 | tempDrawInfo->fWriteConfig = dstSurface->config(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| 605 | if (!this->glCaps().unpackFlipYSupport() && |
| 606 | kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) { |
| 607 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
| 608 | } |
| 609 | |
| 610 | return true; |
| 611 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 612 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 613 | static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface, |
| 614 | GrPixelConfig config) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 615 | if (!glTex) { |
| 616 | return false; |
| 617 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 618 | |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 619 | // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels. |
| 620 | if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) { |
| 621 | return false; |
| 622 | } |
| 623 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 624 | // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures |
| 625 | if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | |
| 632 | bool GrGLGpu::onWritePixels(GrSurface* surface, |
| 633 | int left, int top, int width, int height, |
| 634 | GrPixelConfig config, const void* buffer, |
| 635 | size_t rowBytes) { |
| 636 | GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture()); |
| 637 | |
| 638 | if (!check_write_and_transfer_input(glTex, surface, config)) { |
| 639 | return false; |
| 640 | } |
| 641 | |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 642 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 643 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 644 | |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 645 | bool success = false; |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 646 | if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 647 | // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels() |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 648 | SkASSERT(config == glTex->desc().fConfig); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 649 | success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), buffer, |
| 650 | kWrite_UploadType, left, top, width, height); |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 651 | } else { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 652 | success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType, |
| 653 | left, top, width, height, config, buffer, rowBytes); |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | if (success) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 657 | glTex->texturePriv().dirtyMipMaps(true); |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 658 | return true; |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 659 | } |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 660 | |
| 661 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 662 | } |
| 663 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 664 | bool GrGLGpu::onTransferPixels(GrSurface* surface, |
| 665 | int left, int top, int width, int height, |
| 666 | GrPixelConfig config, GrTransferBuffer* buffer, |
| 667 | size_t offset, size_t rowBytes) { |
| 668 | GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture()); |
| 669 | |
| 670 | if (!check_write_and_transfer_input(glTex, surface, config)) { |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | // For the moment, can't transfer compressed data |
| 675 | if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) { |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | this->setScratchTextureUnit(); |
| 680 | GL_CALL(BindTexture(glTex->target(), glTex->textureID())); |
| 681 | |
| 682 | SkASSERT(!buffer->isMapped()); |
| 683 | GrGLTransferBuffer* glBuffer = reinterpret_cast<GrGLTransferBuffer*>(buffer); |
| 684 | // bind the transfer buffer |
| 685 | SkASSERT(GR_GL_PIXEL_UNPACK_BUFFER == glBuffer->bufferType() || |
| 686 | GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM == glBuffer->bufferType()); |
| 687 | GL_CALL(BindBuffer(glBuffer->bufferType(), glBuffer->bufferID())); |
| 688 | |
| 689 | bool success = false; |
| 690 | success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType, |
| 691 | left, top, width, height, config, buffer, rowBytes); |
| 692 | |
| 693 | if (success) { |
| 694 | glTex->texturePriv().dirtyMipMaps(true); |
| 695 | return true; |
| 696 | } |
| 697 | |
| 698 | return false; |
| 699 | } |
| 700 | |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 701 | // For GL_[UN]PACK_ALIGNMENT. |
| 702 | static inline GrGLint config_alignment(GrPixelConfig config) { |
| 703 | SkASSERT(!GrPixelConfigIsCompressed(config)); |
| 704 | switch (config) { |
| 705 | case kAlpha_8_GrPixelConfig: |
| 706 | return 1; |
| 707 | case kRGB_565_GrPixelConfig: |
| 708 | case kRGBA_4444_GrPixelConfig: |
| 709 | case kAlpha_half_GrPixelConfig: |
| 710 | case kRGBA_half_GrPixelConfig: |
| 711 | return 2; |
| 712 | case kRGBA_8888_GrPixelConfig: |
| 713 | case kBGRA_8888_GrPixelConfig: |
| 714 | case kSRGBA_8888_GrPixelConfig: |
| 715 | case kRGBA_float_GrPixelConfig: |
| 716 | return 4; |
| 717 | default: |
| 718 | return 0; |
| 719 | } |
| 720 | } |
| 721 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 722 | static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc, |
| 723 | const GrGLInterface* interface) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 724 | if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) { |
senorblanco@chromium.org | d092524 | 2013-06-10 15:06:09 +0000 | [diff] [blame] | 725 | return GR_GL_GET_ERROR(interface); |
| 726 | } else { |
| 727 | return CHECK_ALLOC_ERROR(interface); |
| 728 | } |
| 729 | } |
| 730 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 731 | bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 732 | GrGLenum target, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 733 | UploadType uploadType, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 734 | int left, int top, int width, int height, |
| 735 | GrPixelConfig dataConfig, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 736 | const void* dataOrOffset, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 737 | size_t rowBytes) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 738 | SkASSERT(dataOrOffset || kNewTexture_UploadType == uploadType || |
| 739 | kTransfer_UploadType == uploadType); |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 740 | |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 741 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 742 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 743 | |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 744 | SkASSERT(this->caps()->isConfigTexturable(desc.fConfig)); |
| 745 | |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 746 | size_t bpp = GrBytesPerPixel(dataConfig); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 747 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 748 | &width, &height, &dataOrOffset, &rowBytes)) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 749 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 750 | } |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 751 | size_t trimRowBytes = width * bpp; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 752 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 753 | // in case we need a temporary, trimmed copy of the src pixels |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 754 | #if defined(GOOGLE3) |
| 755 | // Stack frame size is limited in GOOGLE3. |
| 756 | SkAutoSMalloc<64 * 128> tempStorage; |
| 757 | #else |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 758 | SkAutoSMalloc<128 * 128> tempStorage; |
benjaminwagner | 7d974f5 | 2015-10-19 13:55:55 -0700 | [diff] [blame] | 759 | #endif |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 760 | |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 761 | // Internal format comes from the texture desc. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 762 | GrGLenum internalFormat; |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 763 | // External format and type come from the upload data. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 764 | GrGLenum externalFormat; |
| 765 | GrGLenum externalType; |
| 766 | if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat, |
| 767 | &externalFormat, &externalType)) { |
| 768 | return false; |
| 769 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 770 | /* |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 771 | * Check whether to allocate a temporary buffer for flipping y or |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 772 | * because our srcData has extra bytes past each row. If so, we need |
| 773 | * to trim those off here, since GL ES may not let us specify |
| 774 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 775 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 776 | bool restoreGLRowLength = false; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 777 | bool swFlipY = false; |
| 778 | bool glFlipY = false; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 779 | if (dataOrOffset) { |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 780 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 781 | if (this->glCaps().unpackFlipYSupport()) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 782 | glFlipY = true; |
| 783 | } else { |
| 784 | swFlipY = true; |
| 785 | } |
| 786 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 787 | if (this->glCaps().unpackRowLengthSupport() && !swFlipY) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 788 | // can't use this for flipping, only non-neg values allowed. :( |
| 789 | if (rowBytes != trimRowBytes) { |
| 790 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
| 791 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
| 792 | restoreGLRowLength = true; |
| 793 | } |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 794 | } else if (kTransfer_UploadType != uploadType) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 795 | if (trimRowBytes != rowBytes || swFlipY) { |
| 796 | // copy data into our new storage, skipping the trailing bytes |
| 797 | size_t trimSize = height * trimRowBytes; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 798 | const char* src = (const char*)dataOrOffset; |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 799 | if (swFlipY) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 800 | src += (height - 1) * rowBytes; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 801 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 802 | char* dst = (char*)tempStorage.reset(trimSize); |
| 803 | for (int y = 0; y < height; y++) { |
| 804 | memcpy(dst, src, trimRowBytes); |
| 805 | if (swFlipY) { |
| 806 | src -= rowBytes; |
| 807 | } else { |
| 808 | src += rowBytes; |
| 809 | } |
| 810 | dst += trimRowBytes; |
| 811 | } |
| 812 | // now point data to our copied version |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 813 | dataOrOffset = tempStorage.get(); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 814 | } |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 815 | } else { |
| 816 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 817 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 818 | if (glFlipY) { |
| 819 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE)); |
| 820 | } |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 821 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(dataConfig))); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 822 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 823 | bool succeeded = true; |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 824 | if (kNewTexture_UploadType == uploadType) { |
| 825 | if (dataOrOffset && |
| 826 | !(0 == left && 0 == top && desc.fWidth == width && desc.fHeight == height)) { |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 827 | succeeded = false; |
| 828 | } else { |
bsalomon | 102b07e | 2015-12-17 15:37:33 -0800 | [diff] [blame] | 829 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 830 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(target, 0, internalFormat, desc.fWidth, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 831 | desc.fHeight, 0, externalFormat, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 832 | externalType, dataOrOffset)); |
bsalomon | 102b07e | 2015-12-17 15:37:33 -0800 | [diff] [blame] | 833 | GrGLenum error = check_alloc_error(desc, this->glInterface()); |
| 834 | if (error != GR_GL_NO_ERROR) { |
| 835 | succeeded = false; |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 836 | } |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 837 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 838 | } else { |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 839 | if (swFlipY || glFlipY) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 840 | top = desc.fHeight - (top + height); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 841 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 842 | GL_CALL(TexSubImage2D(target, |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 843 | 0, // level |
| 844 | left, top, |
| 845 | width, height, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 846 | externalFormat, externalType, dataOrOffset)); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (restoreGLRowLength) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 850 | SkASSERT(this->glCaps().unpackRowLengthSupport()); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 851 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 852 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 853 | if (glFlipY) { |
| 854 | GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE)); |
| 855 | } |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 856 | return succeeded; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 857 | } |
| 858 | |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 859 | // 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] | 860 | // 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] | 861 | // create a CompressedTexData struct that takes a desc/ptr and figures out |
| 862 | // the proper upload semantics. Then users can construct this function how they |
| 863 | // 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] | 864 | bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 865 | GrGLenum target, |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 866 | const void* data, |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 867 | UploadType uploadType, |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 868 | int left, int top, int width, int height) { |
bsalomon | 2555ee2 | 2015-12-17 18:39:46 -0800 | [diff] [blame] | 869 | SkASSERT(this->caps()->isConfigTexturable(desc.fConfig)); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 870 | SkASSERT(kTransfer_UploadType != uploadType && |
| 871 | (data || kNewTexture_UploadType != uploadType)); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 872 | |
| 873 | // No support for software flip y, yet... |
| 874 | SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin); |
| 875 | |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 876 | if (-1 == width) { |
| 877 | width = desc.fWidth; |
| 878 | } |
| 879 | #ifdef SK_DEBUG |
| 880 | else { |
| 881 | SkASSERT(width <= desc.fWidth); |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | if (-1 == height) { |
| 886 | height = desc.fHeight; |
| 887 | } |
| 888 | #ifdef SK_DEBUG |
| 889 | else { |
| 890 | SkASSERT(height <= desc.fHeight); |
| 891 | } |
| 892 | #endif |
| 893 | |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 894 | // Make sure that the width and height that we pass to OpenGL |
| 895 | // is a multiple of the block size. |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 896 | size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 897 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 898 | // We only need the internal format for compressed 2D textures. |
| 899 | GrGLenum internalFormat; |
| 900 | if (!this->glCaps().getCompressedTexImageFormats(desc.fConfig, &internalFormat)) { |
| 901 | return false; |
| 902 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 903 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 904 | if (kNewTexture_UploadType == uploadType) { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 905 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 906 | GL_ALLOC_CALL(this->glInterface(), |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 907 | CompressedTexImage2D(target, |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 908 | 0, // level |
| 909 | internalFormat, |
| 910 | width, height, |
| 911 | 0, // border |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 912 | SkToInt(dataSize), |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 913 | data)); |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 914 | GrGLenum error = check_alloc_error(desc, this->glInterface()); |
| 915 | if (error != GR_GL_NO_ERROR) { |
| 916 | return false; |
| 917 | } |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 918 | } else { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 919 | // Paletted textures can't be updated. |
| 920 | if (GR_GL_PALETTE8_RGBA8 == internalFormat) { |
| 921 | return false; |
| 922 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 923 | GL_CALL(CompressedTexSubImage2D(target, |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 924 | 0, // level |
| 925 | left, top, |
| 926 | width, height, |
| 927 | internalFormat, |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 928 | SkToInt(dataSize), |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 929 | data)); |
krajcevski | 145d48c | 2014-06-11 16:07:50 -0700 | [diff] [blame] | 930 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 931 | |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 932 | return true; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 933 | } |
| 934 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 935 | static bool renderbuffer_storage_msaa(const GrGLContext& ctx, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 936 | int sampleCount, |
| 937 | GrGLenum format, |
| 938 | int width, int height) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 939 | CLEAR_ERROR_BEFORE_ALLOC(ctx.interface()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 940 | SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 941 | switch (ctx.caps()->msFBOType()) { |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 942 | case GrGLCaps::kDesktop_ARB_MSFBOType: |
| 943 | case GrGLCaps::kDesktop_EXT_MSFBOType: |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 944 | case GrGLCaps::kMixedSamples_MSFBOType: |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 945 | case GrGLCaps::kES_3_0_MSFBOType: |
| 946 | GL_ALLOC_CALL(ctx.interface(), |
| 947 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 948 | sampleCount, |
| 949 | format, |
| 950 | width, height)); |
| 951 | break; |
| 952 | case GrGLCaps::kES_Apple_MSFBOType: |
| 953 | GL_ALLOC_CALL(ctx.interface(), |
| 954 | RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER, |
| 955 | sampleCount, |
| 956 | format, |
| 957 | width, height)); |
| 958 | break; |
| 959 | case GrGLCaps::kES_EXT_MsToTexture_MSFBOType: |
| 960 | case GrGLCaps::kES_IMG_MsToTexture_MSFBOType: |
| 961 | GL_ALLOC_CALL(ctx.interface(), |
| 962 | RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER, |
| 963 | sampleCount, |
| 964 | format, |
| 965 | width, height)); |
| 966 | break; |
| 967 | case GrGLCaps::kNone_MSFBOType: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 968 | 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] | 969 | break; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 970 | } |
Brian Salomon | 9251d4e | 2015-03-17 16:03:19 -0400 | [diff] [blame] | 971 | return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface())); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 972 | } |
| 973 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 974 | bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, |
| 975 | GrGpuResource::LifeCycle lifeCycle, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 976 | const GrGLTextureInfo& texInfo, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 977 | GrGLRenderTarget::IDDesc* idDesc) { |
| 978 | idDesc->fMSColorRenderbufferID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 979 | idDesc->fRTFBOID = 0; |
| 980 | idDesc->fTexFBOID = 0; |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 981 | idDesc->fLifeCycle = lifeCycle; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 982 | idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() && |
| 983 | desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig : |
| 984 | GrRenderTarget::kUnified_SampleConfig; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 985 | |
| 986 | GrGLenum status; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 987 | |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 988 | GrGLenum colorRenderbufferFormat = 0; // suppress warning |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 989 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 990 | if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 991 | goto FAILED; |
| 992 | } |
| 993 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 994 | GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID)); |
| 995 | if (!idDesc->fTexFBOID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 996 | goto FAILED; |
| 997 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 998 | |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 999 | // If we are using multisampling we will create two FBOS. We render to one and then resolve to |
| 1000 | // the texture bound to the other. The exception is the IMG multisample extension. With this |
| 1001 | // extension the texture is multisampled when rendered to and then auto-resolves it when it is |
| 1002 | // rendered from. |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1003 | if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1004 | GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID)); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1005 | GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1006 | if (!idDesc->fRTFBOID || |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1007 | !idDesc->fMSColorRenderbufferID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1008 | goto FAILED; |
| 1009 | } |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1010 | if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) { |
| 1011 | return false; |
| 1012 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1013 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1014 | idDesc->fRTFBOID = idDesc->fTexFBOID; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1017 | // below here we may bind the FBO |
| 1018 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
| 1019 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1020 | SkASSERT(desc.fSampleCnt > 0); |
| 1021 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID)); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1022 | if (!renderbuffer_storage_msaa(*fGLContext, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1023 | desc.fSampleCnt, |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1024 | colorRenderbufferFormat, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1025 | desc.fWidth, desc.fHeight)) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1026 | goto FAILED; |
| 1027 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1028 | fStats.incRenderTargetBinds(); |
| 1029 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID)); |
| 1030 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1031 | GR_GL_COLOR_ATTACHMENT0, |
| 1032 | GR_GL_RENDERBUFFER, |
| 1033 | idDesc->fMSColorRenderbufferID)); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1034 | if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) || |
| 1035 | !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1036 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1037 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1038 | goto FAILED; |
| 1039 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1040 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1043 | fStats.incRenderTargetBinds(); |
| 1044 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1045 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1046 | if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1047 | GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1048 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1049 | texInfo.fTarget, |
| 1050 | texInfo.fID, 0, desc.fSampleCnt)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1051 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1052 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1053 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1054 | texInfo.fTarget, |
| 1055 | texInfo.fID, 0)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1056 | } |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1057 | if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) || |
| 1058 | !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1059 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 1060 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1061 | goto FAILED; |
| 1062 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1063 | fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | return true; |
| 1067 | |
| 1068 | FAILED: |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1069 | if (idDesc->fMSColorRenderbufferID) { |
| 1070 | GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1071 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1072 | if (idDesc->fRTFBOID != idDesc->fTexFBOID) { |
| 1073 | GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1074 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1075 | if (idDesc->fTexFBOID) { |
| 1076 | GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1077 | } |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1081 | // good to set a break-point here to know when createTexture fails |
| 1082 | static GrTexture* return_null_texture() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1083 | // SkDEBUGFAIL("null texture"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1084 | return nullptr; |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 1087 | #if 0 && defined(SK_DEBUG) |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1088 | static size_t as_size_t(int x) { |
| 1089 | return x; |
| 1090 | } |
| 1091 | #endif |
| 1092 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1093 | GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc, |
| 1094 | GrGpuResource::LifeCycle lifeCycle, |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1095 | const void* srcData, size_t rowBytes) { |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1096 | // We fail if the MSAA was requested and is not available. |
| 1097 | if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1098 | //SkDebugf("MSAA RT requested but not supported on this platform."); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 1099 | return return_null_texture(); |
| 1100 | } |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1101 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1102 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1103 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1104 | GrGLTexture::IDDesc idDesc; |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1105 | idDesc.fInfo.fID = 0; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1106 | GL_CALL(GenTextures(1, &idDesc.fInfo.fID)); |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1107 | idDesc.fLifeCycle = lifeCycle; |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1108 | // We only support GL_TEXTURE_2D at the moment. |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1109 | idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D; |
junov@chromium.org | 8b6b1c9 | 2013-08-29 16:22:09 +0000 | [diff] [blame] | 1110 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1111 | if (!idDesc.fInfo.fID) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1112 | return return_null_texture(); |
| 1113 | } |
| 1114 | |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 1115 | this->setScratchTextureUnit(); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1116 | GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID)); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 1117 | |
junov@chromium.org | 8b6b1c9 | 2013-08-29 16:22:09 +0000 | [diff] [blame] | 1118 | if (renderTarget && this->glCaps().textureUsageSupport()) { |
| 1119 | // provides a hint about how this texture will be used |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1120 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
junov@chromium.org | 8b6b1c9 | 2013-08-29 16:22:09 +0000 | [diff] [blame] | 1121 | GR_GL_TEXTURE_USAGE, |
| 1122 | GR_GL_FRAMEBUFFER_ATTACHMENT)); |
| 1123 | } |
| 1124 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1125 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1126 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1127 | // texture that is not mipmap complete (considering the filter in use). |
| 1128 | GrGLTexture::TexParams initialTexParams; |
| 1129 | // we only set a subset here so invalidate first |
| 1130 | initialTexParams.invalidate(); |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 1131 | initialTexParams.fMinFilter = GR_GL_NEAREST; |
| 1132 | initialTexParams.fMagFilter = GR_GL_NEAREST; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1133 | initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1134 | initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1135 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1136 | GR_GL_TEXTURE_MAG_FILTER, |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 1137 | initialTexParams.fMagFilter)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1138 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1139 | GR_GL_TEXTURE_MIN_FILTER, |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 1140 | initialTexParams.fMinFilter)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1141 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1142 | GR_GL_TEXTURE_WRAP_S, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1143 | initialTexParams.fWrapS)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1144 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1145 | GR_GL_TEXTURE_WRAP_T, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1146 | initialTexParams.fWrapT)); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1147 | if (!this->uploadTexData(desc, idDesc.fInfo.fTarget, kNewTexture_UploadType, 0, 0, |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1148 | desc.fWidth, desc.fHeight, |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1149 | desc.fConfig, srcData, rowBytes)) { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1150 | GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID)); |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1151 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1152 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1153 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1154 | GrGLTexture* tex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1155 | if (renderTarget) { |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1156 | // 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] | 1157 | GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1158 | GrGLRenderTarget::IDDesc rtIDDesc; |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1159 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1160 | if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) { |
| 1161 | GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1162 | return return_null_texture(); |
| 1163 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1164 | tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1165 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1166 | tex = new GrGLTexture(this, desc, idDesc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1167 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1168 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1169 | #ifdef TRACE_TEXTURE_CREATION |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1170 | SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n", |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 1171 | glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1172 | #endif |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1173 | return tex; |
| 1174 | } |
| 1175 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1176 | GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc, |
| 1177 | GrGpuResource::LifeCycle lifeCycle, |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1178 | const void* srcData) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1179 | // Make sure that we're not flipping Y. |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1180 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1181 | return return_null_texture(); |
| 1182 | } |
| 1183 | |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 1184 | GrGLTexture::IDDesc idDesc; |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1185 | idDesc.fInfo.fID = 0; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1186 | GL_CALL(GenTextures(1, &idDesc.fInfo.fID)); |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 1187 | idDesc.fLifeCycle = lifeCycle; |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1188 | // We only support GL_TEXTURE_2D at the moment. |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1189 | idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D; |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1190 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1191 | if (!idDesc.fInfo.fID) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1192 | return return_null_texture(); |
| 1193 | } |
| 1194 | |
| 1195 | this->setScratchTextureUnit(); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1196 | GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID)); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1197 | |
| 1198 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1199 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1200 | // texture that is not mipmap complete (considering the filter in use). |
| 1201 | GrGLTexture::TexParams initialTexParams; |
| 1202 | // we only set a subset here so invalidate first |
| 1203 | initialTexParams.invalidate(); |
| 1204 | initialTexParams.fMinFilter = GR_GL_NEAREST; |
| 1205 | initialTexParams.fMagFilter = GR_GL_NEAREST; |
| 1206 | initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1207 | initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1208 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1209 | GR_GL_TEXTURE_MAG_FILTER, |
| 1210 | initialTexParams.fMagFilter)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1211 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1212 | GR_GL_TEXTURE_MIN_FILTER, |
| 1213 | initialTexParams.fMinFilter)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1214 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1215 | GR_GL_TEXTURE_WRAP_S, |
| 1216 | initialTexParams.fWrapS)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1217 | GL_CALL(TexParameteri(idDesc.fInfo.fTarget, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1218 | GR_GL_TEXTURE_WRAP_T, |
| 1219 | initialTexParams.fWrapT)); |
| 1220 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 1221 | if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, srcData)) { |
| 1222 | GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID)); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1223 | return return_null_texture(); |
| 1224 | } |
| 1225 | |
| 1226 | GrGLTexture* tex; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1227 | tex = new GrGLTexture(this, desc, idDesc); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1228 | tex->setCachedTexParams(initialTexParams, this->getResetTimestamp()); |
| 1229 | #ifdef TRACE_TEXTURE_CREATION |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1230 | SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n", |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 1231 | glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig); |
| 1232 | #endif |
| 1233 | return tex; |
| 1234 | } |
| 1235 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1236 | namespace { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1237 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1238 | const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1239 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1240 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1241 | GrGLStencilAttachment::Format* format) { |
sugoi@google.com | 6ba0b0f | 2013-05-03 13:52:32 +0000 | [diff] [blame] | 1242 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1243 | // we shouldn't ever know one size and not the other |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1244 | SkASSERT((kUnknownBitCount == format->fStencilBits) == |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1245 | (kUnknownBitCount == format->fTotalBits)); |
| 1246 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1247 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1248 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1249 | (GrGLint*)&format->fStencilBits); |
| 1250 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1251 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1252 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1253 | (GrGLint*)&format->fTotalBits); |
| 1254 | format->fTotalBits += format->fStencilBits; |
| 1255 | } else { |
| 1256 | format->fTotalBits = format->fStencilBits; |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1262 | int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) { |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1263 | static const int kSize = 16; |
bsalomon | 926cb02 | 2015-12-17 18:15:11 -0800 | [diff] [blame] | 1264 | SkASSERT(this->caps()->isConfigRenderable(config, false)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1265 | if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) { |
| 1266 | // Default to unsupported, set this if we find a stencil format that works. |
| 1267 | int firstWorkingStencilFormatIndex = -1; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1268 | // Create color texture |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1269 | GrGLuint colorID = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1270 | GL_CALL(GenTextures(1, &colorID)); |
| 1271 | this->setScratchTextureUnit(); |
| 1272 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID)); |
| 1273 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1274 | GR_GL_TEXTURE_MAG_FILTER, |
| 1275 | GR_GL_NEAREST)); |
| 1276 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1277 | GR_GL_TEXTURE_MIN_FILTER, |
| 1278 | GR_GL_NEAREST)); |
| 1279 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1280 | GR_GL_TEXTURE_WRAP_S, |
| 1281 | GR_GL_CLAMP_TO_EDGE)); |
| 1282 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, |
| 1283 | GR_GL_TEXTURE_WRAP_T, |
| 1284 | GR_GL_CLAMP_TO_EDGE)); |
| 1285 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1286 | GrGLenum internalFormat; |
| 1287 | GrGLenum externalFormat; |
| 1288 | GrGLenum externalType; |
| 1289 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 1290 | &externalType)) { |
| 1291 | return false; |
| 1292 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1293 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1294 | GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, |
bsalomon | 926cb02 | 2015-12-17 18:15:11 -0800 | [diff] [blame] | 1295 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1296 | internalFormat, |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1297 | kSize, |
| 1298 | kSize, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1299 | 0, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1300 | externalFormat, |
| 1301 | externalType, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1302 | NULL)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1303 | if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1304 | GL_CALL(DeleteTextures(1, &colorID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1305 | return -1; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | // unbind the texture from the texture unit before binding it to the frame buffer |
| 1309 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 1310 | |
| 1311 | // Create Framebuffer |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1312 | GrGLuint fb = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1313 | GL_CALL(GenFramebuffers(1, &fb)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1314 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1315 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
| 1316 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 1317 | GR_GL_COLOR_ATTACHMENT0, |
| 1318 | GR_GL_TEXTURE_2D, |
| 1319 | colorID, |
| 1320 | 0)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1321 | GrGLuint sbRBID = 0; |
| 1322 | GL_CALL(GenRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1323 | |
| 1324 | // look over formats till I find a compatible one |
| 1325 | int stencilFmtCnt = this->glCaps().stencilFormats().count(); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1326 | if (sbRBID) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1327 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1328 | for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) { |
| 1329 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i]; |
| 1330 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1331 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1332 | sFmt.fInternalFormat, |
| 1333 | kSize, kSize)); |
| 1334 | if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1335 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1336 | GR_GL_STENCIL_ATTACHMENT, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1337 | GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1338 | if (sFmt.fPacked) { |
| 1339 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1340 | GR_GL_DEPTH_ATTACHMENT, |
| 1341 | GR_GL_RENDERBUFFER, sbRBID)); |
| 1342 | } else { |
| 1343 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1344 | GR_GL_DEPTH_ATTACHMENT, |
| 1345 | GR_GL_RENDERBUFFER, 0)); |
| 1346 | } |
| 1347 | GrGLenum status; |
| 1348 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1349 | if (status == GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1350 | firstWorkingStencilFormatIndex = i; |
| 1351 | break; |
| 1352 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1353 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1354 | GR_GL_STENCIL_ATTACHMENT, |
| 1355 | GR_GL_RENDERBUFFER, 0)); |
| 1356 | if (sFmt.fPacked) { |
| 1357 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1358 | GR_GL_DEPTH_ATTACHMENT, |
| 1359 | GR_GL_RENDERBUFFER, 0)); |
| 1360 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1363 | GL_CALL(DeleteRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1364 | } |
| 1365 | GL_CALL(DeleteTextures(1, &colorID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1366 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); |
| 1367 | GL_CALL(DeleteFramebuffers(1, &fb)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1368 | fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1369 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1370 | return this->glCaps().getStencilFormatIndexForConfig(config); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1373 | GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 1374 | int width, |
| 1375 | int height) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1376 | // All internally created RTs are also textures. We don't create |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 1377 | // SBs for a client's standalone RT (that is a RT that isn't also a texture). |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1378 | SkASSERT(rt->asTexture()); |
| 1379 | SkASSERT(width >= rt->width()); |
| 1380 | SkASSERT(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1381 | |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 1382 | int samples = rt->numStencilSamples(); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1383 | GrGLStencilAttachment::IDDesc sbDesc; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1384 | |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1385 | int sIdx = this->getCompatibleStencilIndex(rt->config()); |
bsalomon | 62a627b | 2015-12-17 09:50:47 -0800 | [diff] [blame] | 1386 | if (sIdx < 0) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1387 | return nullptr; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1388 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1389 | |
| 1390 | if (!sbDesc.fRenderbufferID) { |
| 1391 | GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID)); |
| 1392 | } |
| 1393 | if (!sbDesc.fRenderbufferID) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1394 | return nullptr; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1395 | } |
| 1396 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID)); |
| 1397 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx]; |
| 1398 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1399 | // we do this "if" so that we don't call the multisample |
| 1400 | // version on a GL that doesn't have an MSAA extension. |
| 1401 | if (samples > 0) { |
| 1402 | SkAssertResult(renderbuffer_storage_msaa(*fGLContext, |
| 1403 | samples, |
| 1404 | sFmt.fInternalFormat, |
| 1405 | width, height)); |
| 1406 | } else { |
| 1407 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1408 | sFmt.fInternalFormat, |
| 1409 | width, height)); |
| 1410 | SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface())); |
| 1411 | } |
| 1412 | fStats.incStencilAttachmentCreates(); |
| 1413 | // After sized formats we attempt an unsized format and take |
| 1414 | // whatever sizes GL gives us. In that case we query for the size. |
| 1415 | GrGLStencilAttachment::Format format = sFmt; |
| 1416 | get_stencil_rb_sizes(this->glInterface(), &format); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1417 | GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this, |
| 1418 | sbDesc, |
| 1419 | width, |
| 1420 | height, |
| 1421 | samples, |
| 1422 | format); |
| 1423 | return stencil; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1426 | //////////////////////////////////////////////////////////////////////////////// |
| 1427 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1428 | // GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer |
| 1429 | // objects are implemented as client-side-arrays on tile-deferred architectures. |
| 1430 | #define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW |
| 1431 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1432 | GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) { |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1433 | GrGLVertexBuffer::Desc desc; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1434 | desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1435 | desc.fSizeInBytes = size; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1436 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1437 | if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1438 | desc.fID = 0; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1439 | GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1440 | return vertexBuffer; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1441 | } else { |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1442 | desc.fID = 0; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1443 | GL_CALL(GenBuffers(1, &desc.fID)); |
| 1444 | if (desc.fID) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 1445 | fHWGeometryState.setVertexBufferID(this, desc.fID); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1446 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1447 | // make sure driver can allocate memory for this buffer |
| 1448 | GL_ALLOC_CALL(this->glInterface(), |
| 1449 | BufferData(GR_GL_ARRAY_BUFFER, |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 1450 | (GrGLsizeiptr) desc.fSizeInBytes, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1451 | nullptr, // data ptr |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1452 | dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW)); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1453 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
| 1454 | GL_CALL(DeleteBuffers(1, &desc.fID)); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 1455 | this->notifyVertexBufferDelete(desc.fID); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1456 | return nullptr; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1457 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1458 | GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1459 | return vertexBuffer; |
| 1460 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1461 | return nullptr; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1462 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1465 | GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) { |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1466 | GrGLIndexBuffer::Desc desc; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1467 | desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1468 | desc.fSizeInBytes = size; |
bsalomon@google.com | e49ad45 | 2013-02-20 19:33:20 +0000 | [diff] [blame] | 1469 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1470 | if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1471 | desc.fID = 0; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1472 | GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1473 | return indexBuffer; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1474 | } else { |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1475 | desc.fID = 0; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1476 | GL_CALL(GenBuffers(1, &desc.fID)); |
| 1477 | if (desc.fID) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 1478 | fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1479 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1480 | // make sure driver can allocate memory for this buffer |
| 1481 | GL_ALLOC_CALL(this->glInterface(), |
| 1482 | BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 1483 | (GrGLsizeiptr) desc.fSizeInBytes, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1484 | nullptr, // data ptr |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1485 | dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW)); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1486 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
| 1487 | GL_CALL(DeleteBuffers(1, &desc.fID)); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 1488 | this->notifyIndexBufferDelete(desc.fID); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1489 | return nullptr; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1490 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1491 | GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc); |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 1492 | return indexBuffer; |
| 1493 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1494 | return nullptr; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1495 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1498 | GrTransferBuffer* GrGLGpu::onCreateTransferBuffer(size_t size, TransferType xferType) { |
| 1499 | GrGLCaps::TransferBufferType xferBufferType = this->ctxInfo().caps()->transferBufferType(); |
| 1500 | if (GrGLCaps::kNone_TransferBufferType == xferBufferType) { |
| 1501 | return nullptr; |
| 1502 | } |
| 1503 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1504 | GrGLTransferBuffer::Desc desc; |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1505 | bool toGpu = (kCpuToGpu_TransferType == xferType); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1506 | desc.fUsage = toGpu ? GrGLBufferImpl::kStreamDraw_Usage : GrGLBufferImpl::kStreamRead_Usage; |
| 1507 | |
| 1508 | desc.fSizeInBytes = size; |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1509 | desc.fID = 0; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1510 | GL_CALL(GenBuffers(1, &desc.fID)); |
| 1511 | if (desc.fID) { |
| 1512 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1513 | // make sure driver can allocate memory for this bmapuffer |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1514 | GrGLenum target; |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1515 | if (GrGLCaps::kChromium_TransferBufferType == xferBufferType) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1516 | target = toGpu ? GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1517 | : GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM; |
| 1518 | } else { |
| 1519 | SkASSERT(GrGLCaps::kPBO_TransferBufferType == xferBufferType); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1520 | target = toGpu ? GR_GL_PIXEL_UNPACK_BUFFER : GR_GL_PIXEL_PACK_BUFFER; |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1521 | } |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1522 | GL_CALL(BindBuffer(target, desc.fID)); |
| 1523 | GL_ALLOC_CALL(this->glInterface(), |
| 1524 | BufferData(target, |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1525 | (GrGLsizeiptr) desc.fSizeInBytes, |
| 1526 | nullptr, // data ptr |
| 1527 | (toGpu ? GR_GL_STREAM_DRAW : GR_GL_STREAM_READ))); |
| 1528 | if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) { |
| 1529 | GL_CALL(DeleteBuffers(1, &desc.fID)); |
| 1530 | return nullptr; |
| 1531 | } |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1532 | GrTransferBuffer* transferBuffer = new GrGLTransferBuffer(this, desc, target); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1533 | return transferBuffer; |
| 1534 | } |
| 1535 | |
| 1536 | return nullptr; |
| 1537 | } |
| 1538 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 1539 | void GrGLGpu::flushScissor(const GrScissorState& scissorState, |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1540 | const GrGLIRect& rtViewport, |
| 1541 | GrSurfaceOrigin rtOrigin) { |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 1542 | if (scissorState.enabled()) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1543 | GrGLIRect scissor; |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1544 | scissor.setRelativeTo(rtViewport, |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 1545 | scissorState.rect().fLeft, |
| 1546 | scissorState.rect().fTop, |
| 1547 | scissorState.rect().width(), |
| 1548 | scissorState.rect().height(), |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1549 | rtOrigin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1550 | // if the scissor fully contains the viewport then we fall through and |
| 1551 | // disable the scissor test. |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1552 | if (!scissor.contains(rtViewport)) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1553 | if (fHWScissorSettings.fRect != scissor) { |
| 1554 | scissor.pushToGLScissor(this->glInterface()); |
| 1555 | fHWScissorSettings.fRect = scissor; |
| 1556 | } |
| 1557 | if (kYes_TriState != fHWScissorSettings.fEnabled) { |
| 1558 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1559 | fHWScissorSettings.fEnabled = kYes_TriState; |
| 1560 | } |
| 1561 | return; |
| 1562 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1563 | } |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1564 | |
| 1565 | // See fall through note above |
| 1566 | this->disableScissor(); |
| 1567 | } |
| 1568 | |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 1569 | bool GrGLGpu::flushGLState(const DrawArgs& args) { |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1570 | GrXferProcessor::BlendInfo blendInfo; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1571 | const GrPipeline& pipeline = *args.fPipeline; |
bsalomon | 2047b78 | 2015-12-21 13:12:54 -0800 | [diff] [blame] | 1572 | args.fPipeline->getXferProcessor().getBlendInfo(&blendInfo); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1573 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 1574 | this->flushColorWrite(blendInfo.fWriteColor); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1575 | this->flushDrawFace(pipeline.getDrawFace()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1576 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 1577 | SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args)); |
| 1578 | if (!program) { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 1579 | GrCapsDebugf(this->caps(), "Failed to create program!\n"); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1580 | return false; |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 1583 | GrGLuint programID = program->programID(); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1584 | if (fHWProgramID != programID) { |
| 1585 | GL_CALL(UseProgram(programID)); |
| 1586 | fHWProgramID = programID; |
| 1587 | } |
| 1588 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1589 | if (blendInfo.fWriteColor) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1590 | // Swizzle the blend to match what the shader will output. |
| 1591 | const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle( |
| 1592 | args.fPipeline->getRenderTarget()->config()); |
| 1593 | this->flushBlend(blendInfo, swizzle); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1594 | } |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1595 | |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 1596 | SkSTArray<8, const GrTextureAccess*> textureAccesses; |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1597 | program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses); |
cdalton | 4271765 | 2015-06-18 11:54:30 -0700 | [diff] [blame] | 1598 | |
| 1599 | int numTextureAccesses = textureAccesses.count(); |
| 1600 | for (int i = 0; i < numTextureAccesses; i++) { |
| 1601 | this->bindTexture(i, textureAccesses[i]->getParams(), |
| 1602 | static_cast<GrGLTexture*>(textureAccesses[i]->getTexture())); |
| 1603 | } |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1604 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1605 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget()); |
| 1606 | this->flushStencil(pipeline.getStencil()); |
| 1607 | this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin()); |
cdalton | 7e806f3 | 2015-11-11 15:16:07 -0800 | [diff] [blame] | 1608 | this->flushHWAAState(glRT, pipeline.isHWAntialiasState()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1609 | |
| 1610 | // This must come after textures are flushed because a texture may need |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1611 | // to be msaa-resolved (which will modify bound FBO state). |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 1612 | this->flushRenderTarget(glRT, nullptr); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1613 | |
| 1614 | return true; |
| 1615 | } |
| 1616 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1617 | void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1618 | const GrNonInstancedVertices& vertices, |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1619 | size_t* indexOffsetInBytes) { |
| 1620 | GrGLVertexBuffer* vbuf; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1621 | vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer(); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1622 | |
| 1623 | SkASSERT(vbuf); |
| 1624 | SkASSERT(!vbuf->isMapped()); |
| 1625 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1626 | GrGLIndexBuffer* ibuf = nullptr; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1627 | if (vertices.isIndexed()) { |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1628 | SkASSERT(indexOffsetInBytes); |
| 1629 | |
| 1630 | *indexOffsetInBytes = 0; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1631 | ibuf = (GrGLIndexBuffer*)vertices.indexBuffer(); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1632 | |
| 1633 | SkASSERT(ibuf); |
| 1634 | SkASSERT(!ibuf->isMapped()); |
| 1635 | *indexOffsetInBytes += ibuf->baseOffset(); |
| 1636 | } |
| 1637 | GrGLAttribArrayState* attribState = |
| 1638 | fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf); |
| 1639 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1640 | int vaCount = primProc.numAttribs(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 1641 | if (vaCount > 0) { |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1642 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1643 | GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1644 | |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 1645 | size_t vertexOffsetInBytes = stride * vertices.startVertex(); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1646 | |
| 1647 | vertexOffsetInBytes += vbuf->baseOffset(); |
| 1648 | |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1649 | uint32_t usedAttribArraysMask = 0; |
| 1650 | size_t offset = 0; |
| 1651 | |
| 1652 | for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) { |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1653 | const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1654 | usedAttribArraysMask |= (1 << attribIndex); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 1655 | GrVertexAttribType attribType = attrib.fType; |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1656 | attribState->set(this, |
| 1657 | attribIndex, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 1658 | vbuf->bufferID(), |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1659 | GrGLAttribTypeToLayout(attribType).fCount, |
| 1660 | GrGLAttribTypeToLayout(attribType).fType, |
| 1661 | GrGLAttribTypeToLayout(attribType).fNormalized, |
| 1662 | stride, |
| 1663 | reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset)); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 1664 | offset += attrib.fOffset; |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1665 | } |
| 1666 | attribState->disableUnusedArrays(this, usedAttribArraysMask); |
| 1667 | } |
| 1668 | } |
| 1669 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 1670 | void GrGLGpu::buildProgramDesc(GrProgramDesc* desc, |
| 1671 | const GrPrimitiveProcessor& primProc, |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 1672 | const GrPipeline& pipeline) const { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 1673 | if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, *this->glCaps().glslCaps())) { |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1674 | SkDEBUGFAIL("Failed to generate GL program descriptor"); |
| 1675 | } |
| 1676 | } |
| 1677 | |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1678 | void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) { |
| 1679 | this->handleDirtyContext(); |
| 1680 | if (GR_GL_ARRAY_BUFFER == type) { |
| 1681 | this->bindVertexBuffer(id); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1682 | } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1683 | this->bindIndexBufferAndDefaultVertexArray(id); |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1684 | } else { |
| 1685 | GR_GL_CALL(this->glInterface(), BindBuffer(type, id)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) { |
| 1690 | this->handleDirtyContext(); |
| 1691 | GL_CALL(DeleteBuffers(1, &id)); |
| 1692 | if (GR_GL_ARRAY_BUFFER == type) { |
| 1693 | this->notifyVertexBufferDelete(id); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1694 | } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1695 | this->notifyIndexBufferDelete(id); |
| 1696 | } |
| 1697 | } |
| 1698 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1699 | static GrGLenum get_gl_usage(GrGLBufferImpl::Usage usage) { |
| 1700 | static const GrGLenum grToGL[] = { |
| 1701 | GR_GL_STATIC_DRAW, // GrGLBufferImpl::kStaticDraw_Usage |
| 1702 | DYNAMIC_USAGE_PARAM, // GrGLBufferImpl::kDynamicDraw_Usage |
| 1703 | GR_GL_STREAM_DRAW, // GrGLBufferImpl::kStreamDraw_Usage |
| 1704 | GR_GL_STREAM_READ, // GrGLBufferImpl::kStreamRead_Usage |
| 1705 | }; |
| 1706 | static_assert(SK_ARRAY_COUNT(grToGL) == GrGLBufferImpl::kUsageCount, "array_size_mismatch"); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1707 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1708 | return grToGL[usage]; |
| 1709 | } |
| 1710 | |
| 1711 | void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, |
| 1712 | size_t currentSize, size_t requestedSize) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1713 | void* mapPtr = nullptr; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1714 | GrGLenum glUsage = get_gl_usage(usage); |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1715 | bool readOnly = (GrGLBufferImpl::kStreamRead_Usage == usage); |
| 1716 | |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1717 | // Handling dirty context is done in the bindBuffer call |
| 1718 | switch (this->glCaps().mapBufferType()) { |
| 1719 | case GrGLCaps::kNone_MapBufferType: |
| 1720 | break; |
| 1721 | case GrGLCaps::kMapBuffer_MapBufferType: |
| 1722 | this->bindBuffer(id, type); |
| 1723 | // Let driver know it can discard the old data |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 1724 | if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1725 | GL_CALL(BufferData(type, requestedSize, nullptr, glUsage)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1726 | } |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1727 | GL_CALL_RET(mapPtr, MapBuffer(type, readOnly ? GR_GL_READ_ONLY : GR_GL_WRITE_ONLY)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1728 | break; |
| 1729 | case GrGLCaps::kMapBufferRange_MapBufferType: { |
| 1730 | this->bindBuffer(id, type); |
| 1731 | // Make sure the GL buffer size agrees with fDesc before mapping. |
| 1732 | if (currentSize != requestedSize) { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1733 | GL_CALL(BufferData(type, requestedSize, nullptr, glUsage)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1734 | } |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1735 | GrGLbitfield writeAccess = GR_GL_MAP_WRITE_BIT; |
| 1736 | // TODO: allow the client to specify invalidation in the stream draw case |
| 1737 | if (GrGLBufferImpl::kStreamDraw_Usage != usage) { |
| 1738 | writeAccess |= GR_GL_MAP_INVALIDATE_BUFFER_BIT; |
| 1739 | } |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1740 | GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, readOnly ? |
| 1741 | GR_GL_MAP_READ_BIT : |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 1742 | writeAccess)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1743 | break; |
| 1744 | } |
| 1745 | case GrGLCaps::kChromium_MapBufferType: |
| 1746 | this->bindBuffer(id, type); |
| 1747 | // Make sure the GL buffer size agrees with fDesc before mapping. |
| 1748 | if (currentSize != requestedSize) { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1749 | GL_CALL(BufferData(type, requestedSize, nullptr, glUsage)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1750 | } |
jvanverth | d7a2c1f | 2015-12-07 07:36:44 -0800 | [diff] [blame] | 1751 | GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, readOnly ? |
| 1752 | GR_GL_READ_ONLY : |
| 1753 | GR_GL_WRITE_ONLY)); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1754 | break; |
| 1755 | } |
| 1756 | return mapPtr; |
| 1757 | } |
| 1758 | |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1759 | void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, |
| 1760 | size_t currentSize, const void* src, size_t srcSizeInBytes) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1761 | SkASSERT(srcSizeInBytes <= currentSize); |
| 1762 | // bindbuffer handles dirty context |
| 1763 | this->bindBuffer(id, type); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1764 | GrGLenum glUsage = get_gl_usage(usage); |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1765 | |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 1766 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
| 1767 | if (currentSize == srcSizeInBytes) { |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1768 | GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, glUsage)); |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 1769 | } else { |
| 1770 | // Before we call glBufferSubData we give the driver a hint using |
| 1771 | // glBufferData with nullptr. This makes the old buffer contents |
| 1772 | // inaccessible to future draws. The GPU may still be processing |
| 1773 | // draws that reference the old contents. With this hint it can |
| 1774 | // assign a different allocation for the new contents to avoid |
| 1775 | // flushing the gpu past draws consuming the old contents. |
| 1776 | // TODO I think we actually want to try calling bufferData here |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1777 | GL_CALL(BufferData(type, currentSize, nullptr, glUsage)); |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 1778 | GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src)); |
| 1779 | } |
| 1780 | #else |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1781 | // Note that we're cheating on the size here. Currently no methods |
| 1782 | // allow a partial update that preserves contents of non-updated |
| 1783 | // portions of the buffer (map() does a glBufferData(..size, nullptr..)) |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1784 | GL_CALL(BufferData(type, srcSizeInBytes, src, glUsage)); |
joshualitt | 6df232d | 2015-10-23 13:54:12 -0700 | [diff] [blame] | 1785 | #endif |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) { |
| 1789 | // bind buffer handles the dirty context |
| 1790 | switch (this->glCaps().mapBufferType()) { |
| 1791 | case GrGLCaps::kNone_MapBufferType: |
| 1792 | SkDEBUGFAIL("Shouldn't get here."); |
| 1793 | return; |
| 1794 | case GrGLCaps::kMapBuffer_MapBufferType: // fall through |
| 1795 | case GrGLCaps::kMapBufferRange_MapBufferType: |
| 1796 | this->bindBuffer(id, type); |
| 1797 | GL_CALL(UnmapBuffer(type)); |
| 1798 | break; |
| 1799 | case GrGLCaps::kChromium_MapBufferType: |
| 1800 | this->bindBuffer(id, type); |
| 1801 | GL_CALL(UnmapBufferSubData(mapPtr)); |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1806 | void GrGLGpu::disableScissor() { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1807 | if (kNo_TriState != fHWScissorSettings.fEnabled) { |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1808 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1809 | fHWScissorSettings.fEnabled = kNo_TriState; |
| 1810 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 1814 | void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1815 | // parent class should never let us get here with no RT |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1816 | SkASSERT(target); |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1817 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1818 | |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 1819 | this->flushRenderTarget(glRT, &rect); |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 1820 | GrScissorState scissorState; |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 1821 | scissorState.set(rect); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1822 | this->flushScissor(scissorState, glRT->getViewport(), glRT->origin()); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1823 | |
| 1824 | GrGLfloat r, g, b, a; |
| 1825 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 1826 | a = GrColorUnpackA(color) * scale255; |
| 1827 | GrGLfloat scaleRGB = scale255; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1828 | r = GrColorUnpackR(color) * scaleRGB; |
| 1829 | g = GrColorUnpackG(color) * scaleRGB; |
| 1830 | b = GrColorUnpackB(color) * scaleRGB; |
| 1831 | |
| 1832 | 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] | 1833 | fHWWriteToColor = kYes_TriState; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1834 | GL_CALL(ClearColor(r, g, b, a)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1835 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1838 | void GrGLGpu::discard(GrRenderTarget* renderTarget) { |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 1839 | SkASSERT(renderTarget); |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1840 | if (!this->caps()->discardRenderTargetSupport()) { |
| 1841 | return; |
| 1842 | } |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1843 | |
| 1844 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1845 | if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) { |
| 1846 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
| 1847 | fStats.incRenderTargetBinds(); |
| 1848 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID())); |
| 1849 | } |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1850 | switch (this->glCaps().invalidateFBType()) { |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 1851 | case GrGLCaps::kNone_InvalidateFBType: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1852 | SkFAIL("Should never get here."); |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | case GrGLCaps::kInvalidate_InvalidateFBType: |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1855 | if (0 == glRT->renderFBOID()) { |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1856 | // When rendering to the default framebuffer the legal values for attachments |
| 1857 | // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment |
| 1858 | // types. |
| 1859 | static const GrGLenum attachments[] = { GR_GL_COLOR }; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1860 | GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments), |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1861 | attachments)); |
| 1862 | } else { |
| 1863 | static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 }; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1864 | GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments), |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1865 | attachments)); |
| 1866 | } |
| 1867 | break; |
| 1868 | case GrGLCaps::kDiscard_InvalidateFBType: { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1869 | if (0 == glRT->renderFBOID()) { |
commit-bot@chromium.org | 4453e8b | 2014-04-16 14:33:27 +0000 | [diff] [blame] | 1870 | // When rendering to the default framebuffer the legal values for attachments |
| 1871 | // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment |
| 1872 | // types. See glDiscardFramebuffer() spec. |
| 1873 | static const GrGLenum attachments[] = { GR_GL_COLOR }; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1874 | GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments), |
commit-bot@chromium.org | 4453e8b | 2014-04-16 14:33:27 +0000 | [diff] [blame] | 1875 | attachments)); |
| 1876 | } else { |
| 1877 | static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 }; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1878 | GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments), |
commit-bot@chromium.org | 4453e8b | 2014-04-16 14:33:27 +0000 | [diff] [blame] | 1879 | attachments)); |
| 1880 | } |
commit-bot@chromium.org | 52ffbf6 | 2014-04-02 16:19:33 +0000 | [diff] [blame] | 1881 | break; |
| 1882 | } |
| 1883 | } |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1884 | renderTarget->flagAsResolved(); |
| 1885 | } |
| 1886 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1887 | void GrGLGpu::clearStencil(GrRenderTarget* target) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1888 | if (nullptr == target) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1891 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1892 | this->flushRenderTarget(glRT, &SkIRect::EmptyIRect()); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1893 | |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1894 | this->disableScissor(); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1895 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1896 | GL_CALL(StencilMask(0xffffffff)); |
| 1897 | GL_CALL(ClearStencil(0)); |
| 1898 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1899 | fHWStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 1902 | void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1903 | SkASSERT(target); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1904 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1905 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1906 | // this should only be called internally when we know we have a |
| 1907 | // stencil buffer. |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 1908 | SkASSERT(sb); |
| 1909 | GrGLint stencilBitCount = sb->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1910 | #if 0 |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1911 | SkASSERT(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1912 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1913 | #else |
| 1914 | // 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] | 1915 | // ANGLE a partial stencil mask will cause clears to be |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1916 | // turned into draws. Our contract on GrDrawTarget says that |
| 1917 | // changing the clip between stencil passes may or may not |
| 1918 | // 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] | 1919 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 1920 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1921 | GrGLint value; |
| 1922 | if (insideClip) { |
| 1923 | value = (1 << (stencilBitCount - 1)); |
| 1924 | } else { |
| 1925 | value = 0; |
| 1926 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1927 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1928 | this->flushRenderTarget(glRT, &SkIRect::EmptyIRect()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1929 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 1930 | GrScissorState scissorState; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 1931 | scissorState.set(rect); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1932 | this->flushScissor(scissorState, glRT->getViewport(), glRT->origin()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1933 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 1934 | GL_CALL(StencilMask((uint32_t) clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 1935 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1936 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1937 | fHWStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 1940 | static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps, |
| 1941 | int width, int height, GrPixelConfig config, |
| 1942 | size_t rowBytes) { |
| 1943 | // 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] | 1944 | if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) { |
| 1945 | return false; |
| 1946 | } |
| 1947 | |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 1948 | // 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] | 1949 | static const int kMinSize = 32; |
| 1950 | if (width < kMinSize || height < kMinSize) { |
bsalomon | 494aa59 | 2015-07-23 11:45:02 -0700 | [diff] [blame] | 1951 | return false; |
| 1952 | } |
| 1953 | |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1954 | // if GL can do the flip then we'll never pay for it. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 1955 | if (caps.packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1956 | return false; |
| 1957 | } |
| 1958 | |
| 1959 | // 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] | 1960 | // get the flip for free. Otherwise it costs. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 1961 | // Note that we're assuming that 0 rowBytes has already been handled and that the width has been |
| 1962 | // clipped. |
| 1963 | return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes; |
| 1964 | } |
| 1965 | |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 1966 | bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) { |
| 1967 | auto bindRenderTarget = [this, target]() -> bool { |
| 1968 | this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect()); |
| 1969 | return true; |
| 1970 | }; |
| 1971 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 1972 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 1973 | }; |
| 1974 | GrPixelConfig rtConfig = target->config(); |
| 1975 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget); |
| 1976 | } |
| 1977 | |
| 1978 | bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) { |
| 1979 | auto bindRenderTarget = [this, rtConfig]() -> bool { |
| 1980 | GrTextureDesc desc; |
| 1981 | desc.fConfig = rtConfig; |
| 1982 | desc.fWidth = desc.fHeight = 16; |
| 1983 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1984 | SkAutoTUnref<GrTexture> temp(this->createTexture(desc, false, nullptr, 0)); |
| 1985 | if (!temp) { |
| 1986 | return false; |
| 1987 | } |
| 1988 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget()); |
| 1989 | this->flushRenderTarget(glrt, &SkIRect::EmptyIRect()); |
| 1990 | return true; |
| 1991 | }; |
| 1992 | auto getIntegerv = [this](GrGLenum query, GrGLint* value) { |
| 1993 | GR_GL_GetIntegerv(this->glInterface(), query, value); |
| 1994 | }; |
| 1995 | return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget); |
| 1996 | } |
| 1997 | |
| 1998 | bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) { |
| 1999 | if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) { |
| 2000 | return this->readPixelsSupported(rt, readConfig); |
| 2001 | } else { |
| 2002 | GrPixelConfig config = surfaceForConfig->config(); |
| 2003 | return this->readPixelsSupported(config, readConfig); |
| 2004 | } |
| 2005 | } |
| 2006 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2007 | bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 2008 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 2009 | ReadPixelTempDrawInfo* tempDrawInfo) { |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2010 | GrRenderTarget* srcAsRT = srcSurface->asRenderTarget(); |
| 2011 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2012 | // This subclass can only read pixels from a render target. We could use glTexSubImage2D on |
| 2013 | // GL versions that support it but we don't today. |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2014 | if (!srcAsRT) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2015 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 2016 | } |
| 2017 | |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 2018 | if (GrPixelConfigIsSRGB(srcSurface->config()) != GrPixelConfigIsSRGB(readConfig)) { |
| 2019 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 2020 | } |
| 2021 | |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2022 | tempDrawInfo->fSwizzle = GrSwizzle::RGBA(); |
| 2023 | tempDrawInfo->fReadConfig = readConfig; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2024 | |
| 2025 | // These settings we will always want if a temp draw is performed. The config is set below |
| 2026 | // depending on whether we want to do a R/B swap or not. |
| 2027 | tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 2028 | tempDrawInfo->fTempSurfaceDesc.fWidth = width; |
| 2029 | tempDrawInfo->fTempSurfaceDesc.fHeight = height; |
| 2030 | tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0; |
| 2031 | tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL. |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 2032 | tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2033 | |
| 2034 | // Start off assuming that any temp draw should be to the readConfig, then check if that will |
| 2035 | // be inefficient. |
| 2036 | GrPixelConfig srcConfig = srcSurface->config(); |
| 2037 | tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig; |
| 2038 | |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2039 | if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig && |
| 2040 | this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2041 | tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2042 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 2043 | tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig; |
bsalomon | b411b3b | 2015-07-31 09:34:24 -0700 | [diff] [blame] | 2044 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2045 | } else if (kMesa_GrGLDriver == this->glContext().driver() && |
| 2046 | GrBytesPerPixel(readConfig) == 4 && |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2047 | GrPixelConfigSwapRAndB(readConfig) == srcConfig && |
| 2048 | this->readPixelsSupported(srcSurface, srcConfig)) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2049 | // 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] | 2050 | // Better to do a draw with a R/B swap and then read as the original config. |
| 2051 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2052 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 2053 | tempDrawInfo->fReadConfig = srcConfig; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2054 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2055 | } else if (!this->readPixelsSupported(srcSurface, readConfig)) { |
| 2056 | if (readConfig == kBGRA_8888_GrPixelConfig && |
| 2057 | this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) && |
| 2058 | this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) { |
| 2059 | |
| 2060 | tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 2061 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 2062 | tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig; |
| 2063 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 2064 | } else { |
| 2065 | return false; |
| 2066 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
bsalomon | 1aa2029 | 2016-01-22 08:16:09 -0800 | [diff] [blame] | 2069 | if (srcAsRT && |
| 2070 | read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 2071 | ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2074 | return true; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2077 | bool GrGLGpu::onReadPixels(GrSurface* surface, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2078 | int left, int top, |
| 2079 | int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2080 | GrPixelConfig config, |
| 2081 | void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2082 | size_t rowBytes) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2083 | SkASSERT(surface); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2084 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2085 | GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
| 2086 | if (!tgt) { |
| 2087 | return false; |
| 2088 | } |
| 2089 | |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 2090 | // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels. |
| 2091 | if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) { |
| 2092 | return false; |
| 2093 | } |
| 2094 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2095 | GrGLenum externalFormat; |
| 2096 | GrGLenum externalType; |
| 2097 | if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat, |
| 2098 | &externalType)) { |
| 2099 | return false; |
| 2100 | } |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2101 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2102 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2103 | // resolve the render target if necessary |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2104 | switch (tgt->getResolveType()) { |
| 2105 | case GrGLRenderTarget::kCantResolve_ResolveType: |
| 2106 | return false; |
| 2107 | case GrGLRenderTarget::kAutoResolves_ResolveType: |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2108 | this->flushRenderTarget(tgt, &SkIRect::EmptyIRect()); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2109 | break; |
| 2110 | case GrGLRenderTarget::kCanResolve_ResolveType: |
| 2111 | this->onResolveRenderTarget(tgt); |
| 2112 | // we don't track the state of the READ FBO ID. |
| 2113 | fStats.incRenderTargetBinds(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 2114 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID())); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2115 | break; |
| 2116 | default: |
| 2117 | SkFAIL("Unknown resolve type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2120 | const GrGLIRect& glvp = tgt->getViewport(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2121 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 2122 | // the read rect is viewport-relative |
| 2123 | GrGLIRect readRect; |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2124 | readRect.setRelativeTo(glvp, left, top, width, height, tgt->origin()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2125 | |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 2126 | size_t tightRowBytes = GrBytesPerPixel(config) * width; |
| 2127 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2128 | size_t readDstRowBytes = tightRowBytes; |
| 2129 | void* readDst = buffer; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2130 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2131 | // determine if GL can read using the passed rowBytes or if we need |
| 2132 | // a scratch buffer. |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 2133 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2134 | if (rowBytes != tightRowBytes) { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 2135 | if (this->glCaps().packRowLengthSupport()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2136 | SkASSERT(!(rowBytes % sizeof(GrColor))); |
skia.committer@gmail.com | 4677acc | 2013-10-17 07:02:33 +0000 | [diff] [blame] | 2137 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 2138 | static_cast<GrGLint>(rowBytes / sizeof(GrColor)))); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2139 | readDstRowBytes = rowBytes; |
| 2140 | } else { |
| 2141 | scratch.reset(tightRowBytes * height); |
| 2142 | readDst = scratch.get(); |
| 2143 | } |
| 2144 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2145 | if (flipY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2146 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1)); |
| 2147 | } |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 2148 | GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config))); |
| 2149 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2150 | GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, |
| 2151 | readRect.fWidth, readRect.fHeight, |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2152 | externalFormat, externalType, readDst)); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2153 | if (readDstRowBytes != tightRowBytes) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2154 | SkASSERT(this->glCaps().packRowLengthSupport()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2155 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 2156 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2157 | if (flipY && this->glCaps().packFlipYSupport()) { |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2158 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0)); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2159 | flipY = false; |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 2160 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2161 | |
| 2162 | // 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] | 2163 | // API presents top-to-bottom. We must preserve the padding contents. Note |
| 2164 | // that the above readPixels did not overwrite the padding. |
| 2165 | if (readDst == buffer) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2166 | SkASSERT(rowBytes == readDstRowBytes); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2167 | if (flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2168 | scratch.reset(tightRowBytes); |
| 2169 | void* tmpRow = scratch.get(); |
| 2170 | // flip y in-place by rows |
| 2171 | const int halfY = height >> 1; |
| 2172 | char* top = reinterpret_cast<char*>(buffer); |
| 2173 | char* bottom = top + (height - 1) * rowBytes; |
| 2174 | for (int y = 0; y < halfY; y++) { |
| 2175 | memcpy(tmpRow, top, tightRowBytes); |
| 2176 | memcpy(top, bottom, tightRowBytes); |
| 2177 | memcpy(bottom, tmpRow, tightRowBytes); |
| 2178 | top += rowBytes; |
| 2179 | bottom -= rowBytes; |
| 2180 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2181 | } |
| 2182 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2183 | SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2184 | // copy from readDst to buffer while flipping y |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 2185 | // const int halfY = height >> 1; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2186 | const char* src = reinterpret_cast<const char*>(readDst); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2187 | char* dst = reinterpret_cast<char*>(buffer); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2188 | if (flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2189 | dst += (height-1) * rowBytes; |
| 2190 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2191 | for (int y = 0; y < height; y++) { |
| 2192 | memcpy(dst, src, tightRowBytes); |
| 2193 | src += readDstRowBytes; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 2194 | if (!flipY) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2195 | dst += rowBytes; |
| 2196 | } else { |
| 2197 | dst -= rowBytes; |
| 2198 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | return true; |
| 2202 | } |
| 2203 | |
ethannicholas | 7df3f5e | 2016-01-22 06:48:46 -0800 | [diff] [blame] | 2204 | void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) { |
ethannicholas | 5366a09 | 2016-01-22 09:45:47 -0800 | [diff] [blame] | 2205 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2206 | SkASSERT(target); |
bsalomon | 6ba6fa1 | 2015-03-04 11:57:37 -0800 | [diff] [blame] | 2207 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2208 | uint32_t rtID = target->getUniqueID(); |
| 2209 | if (fHWBoundRenderTargetUniqueID != rtID) { |
bsalomon | 1e0bf7e | 2015-03-14 12:08:51 -0700 | [diff] [blame] | 2210 | fStats.incRenderTargetBinds(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2211 | GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID())); |
| 2212 | #ifdef SK_DEBUG |
| 2213 | // don't do this check in Chromium -- this is causing |
| 2214 | // lots of repeated command buffer flushes when the compositor is |
| 2215 | // rendering with Ganesh, which is really slow; even too slow for |
| 2216 | // Debug mode. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 2217 | if (kChromium_GrGLDriver != this->glContext().driver()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2218 | GrGLenum status; |
| 2219 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 2220 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 2221 | SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
| 2222 | } |
bsalomon | 160f24c | 2015-03-17 15:55:42 -0700 | [diff] [blame] | 2223 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2224 | #endif |
| 2225 | fHWBoundRenderTargetUniqueID = rtID; |
| 2226 | const GrGLIRect& vp = target->getViewport(); |
| 2227 | if (fHWViewport != vp) { |
| 2228 | vp.pushToGLViewport(this->glInterface()); |
| 2229 | fHWViewport = vp; |
bsalomon | 160f24c | 2015-03-17 15:55:42 -0700 | [diff] [blame] | 2230 | } |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 2231 | if (this->glCaps().srgbWriteControl()) { |
| 2232 | bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config()); |
| 2233 | if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) { |
| 2234 | GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2235 | fHWSRGBFramebuffer = kYes_TriState; |
| 2236 | } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) { |
| 2237 | GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2238 | fHWSRGBFramebuffer = kNo_TriState; |
| 2239 | } |
| 2240 | } |
bsalomon | 5cd020f | 2015-03-17 12:46:56 -0700 | [diff] [blame] | 2241 | } |
bsalomon | a990912 | 2016-01-23 10:41:40 -0800 | [diff] [blame^] | 2242 | |
| 2243 | // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2244 | if (nullptr == bound || !bound->isEmpty()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2245 | target->flagAsNeedingResolve(bound); |
bsalomon | a990912 | 2016-01-23 10:41:40 -0800 | [diff] [blame^] | 2246 | if (GrTexture *texture = target->asTexture()) { |
| 2247 | texture->texturePriv().dirtyMipMaps(true); |
| 2248 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2249 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2252 | GrGLenum gPrimitiveType2GLMode[] = { |
| 2253 | GR_GL_TRIANGLES, |
| 2254 | GR_GL_TRIANGLE_STRIP, |
| 2255 | GR_GL_TRIANGLE_FAN, |
| 2256 | GR_GL_POINTS, |
| 2257 | GR_GL_LINES, |
| 2258 | GR_GL_LINE_STRIP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2259 | }; |
| 2260 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2261 | #define SWAP_PER_DRAW 0 |
| 2262 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 2263 | #if SWAP_PER_DRAW |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2264 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2265 | #include <AGL/agl.h> |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2266 | #elif defined(SK_BUILD_FOR_WIN32) |
bsalomon@google.com | ce7357d | 2012-06-25 17:49:25 +0000 | [diff] [blame] | 2267 | #include <gl/GL.h> |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2268 | void SwapBuf() { |
| 2269 | DWORD procID = GetCurrentProcessId(); |
| 2270 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 2271 | while(hwnd) { |
| 2272 | DWORD wndProcID = 0; |
| 2273 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 2274 | if(wndProcID == procID) { |
| 2275 | SwapBuffers(GetDC(hwnd)); |
| 2276 | } |
| 2277 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 2278 | } |
| 2279 | } |
| 2280 | #endif |
| 2281 | #endif |
| 2282 | |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 2283 | void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) { |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 2284 | if (!this->flushGLState(args)) { |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 2285 | return; |
| 2286 | } |
| 2287 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 2288 | size_t indexOffsetInBytes = 0; |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2289 | this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2290 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2291 | SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2292 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2293 | if (vertices.isIndexed()) { |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 2294 | GrGLvoid* indices = |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2295 | reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * |
| 2296 | vertices.startIndex()); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2297 | // info.startVertex() was accounted for by setupGeometry. |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2298 | GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()], |
| 2299 | vertices.indexCount(), |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2300 | GR_GL_UNSIGNED_SHORT, |
| 2301 | indices)); |
| 2302 | } else { |
| 2303 | // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for |
| 2304 | // startVertex in the DrawElements case. So we always rely on setupGeometry to have |
| 2305 | // accounted for startVertex. |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 2306 | GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0, |
| 2307 | vertices.vertexCount())); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2308 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2309 | #if SWAP_PER_DRAW |
| 2310 | glFlush(); |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2311 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2312 | aglSwapBuffers(aglGetCurrentContext()); |
| 2313 | int set_a_break_pt_here = 9; |
| 2314 | aglSwapBuffers(aglGetCurrentContext()); |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2315 | #elif defined(SK_BUILD_FOR_WIN32) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2316 | SwapBuf(); |
| 2317 | int set_a_break_pt_here = 9; |
| 2318 | SwapBuf(); |
| 2319 | #endif |
| 2320 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 2323 | void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) { |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 2324 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2325 | if (rt->needsResolve()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2326 | // Some extensions automatically resolves the texture when it is read. |
| 2327 | if (this->glCaps().usesMSAARenderBuffers()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2328 | SkASSERT(rt->textureFBOID() != rt->renderFBOID()); |
| 2329 | fStats.incRenderTargetBinds(); |
| 2330 | fStats.incRenderTargetBinds(); |
| 2331 | GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID())); |
| 2332 | GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID())); |
| 2333 | // make sure we go through flushRenderTarget() since we've modified |
| 2334 | // the bound DRAW FBO ID. |
| 2335 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2336 | const GrGLIRect& vp = rt->getViewport(); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 2337 | const SkIRect dirtyRect = rt->getResolveRect(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2338 | |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2339 | if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2340 | // Apple's extension uses the scissor as the blit bounds. |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2341 | GrScissorState scissorState; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 2342 | scissorState.set(dirtyRect); |
| 2343 | this->flushScissor(scissorState, vp, rt->origin()); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2344 | GL_CALL(ResolveMultisampleFramebuffer()); |
| 2345 | } else { |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 2346 | GrGLIRect r; |
| 2347 | r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop, |
| 2348 | dirtyRect.width(), dirtyRect.height(), target->origin()); |
| 2349 | |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2350 | int right = r.fLeft + r.fWidth; |
| 2351 | int top = r.fBottom + r.fHeight; |
derekf | 8c8f71a | 2014-09-16 06:24:57 -0700 | [diff] [blame] | 2352 | |
| 2353 | // BlitFrameBuffer respects the scissor, so disable it. |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 2354 | this->disableScissor(); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 2355 | GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top, |
| 2356 | r.fLeft, r.fBottom, right, top, |
| 2357 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 2358 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2359 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 2360 | rt->flagAsResolved(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2364 | namespace { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2365 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2366 | |
| 2367 | GrGLenum gr_to_gl_stencil_op(GrStencilOp op) { |
| 2368 | static const GrGLenum gTable[] = { |
| 2369 | GR_GL_KEEP, // kKeep_StencilOp |
| 2370 | GR_GL_REPLACE, // kReplace_StencilOp |
| 2371 | GR_GL_INCR_WRAP, // kIncWrap_StencilOp |
| 2372 | GR_GL_INCR, // kIncClamp_StencilOp |
| 2373 | GR_GL_DECR_WRAP, // kDecWrap_StencilOp |
| 2374 | GR_GL_DECR, // kDecClamp_StencilOp |
| 2375 | GR_GL_ZERO, // kZero_StencilOp |
| 2376 | GR_GL_INVERT, // kInvert_StencilOp |
| 2377 | }; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 2378 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2379 | GR_STATIC_ASSERT(0 == kKeep_StencilOp); |
| 2380 | GR_STATIC_ASSERT(1 == kReplace_StencilOp); |
| 2381 | GR_STATIC_ASSERT(2 == kIncWrap_StencilOp); |
| 2382 | GR_STATIC_ASSERT(3 == kIncClamp_StencilOp); |
| 2383 | GR_STATIC_ASSERT(4 == kDecWrap_StencilOp); |
| 2384 | GR_STATIC_ASSERT(5 == kDecClamp_StencilOp); |
| 2385 | GR_STATIC_ASSERT(6 == kZero_StencilOp); |
| 2386 | GR_STATIC_ASSERT(7 == kInvert_StencilOp); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2387 | SkASSERT((unsigned) op < kStencilOpCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2388 | return gTable[op]; |
| 2389 | } |
| 2390 | |
| 2391 | void set_gl_stencil(const GrGLInterface* gl, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2392 | const GrStencilSettings& settings, |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2393 | GrGLenum glFace, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2394 | GrStencilSettings::Face grFace) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 2395 | GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace)); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2396 | GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace)); |
| 2397 | GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace)); |
| 2398 | |
| 2399 | GrGLint ref = settings.funcRef(grFace); |
| 2400 | GrGLint mask = settings.funcMask(grFace); |
| 2401 | GrGLint writeMask = settings.writeMask(grFace); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2402 | |
| 2403 | if (GR_GL_FRONT_AND_BACK == glFace) { |
| 2404 | // we call the combined func just in case separate stencil is not |
| 2405 | // supported. |
| 2406 | GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask)); |
| 2407 | GR_GL_CALL(gl, StencilMask(writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2408 | GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2409 | } else { |
| 2410 | GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask)); |
| 2411 | GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2412 | GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2416 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2417 | void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) { |
| 2418 | if (fHWStencilSettings != stencilSettings) { |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2419 | if (stencilSettings.isDisabled()) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 2420 | if (kNo_TriState != fHWStencilTestEnabled) { |
| 2421 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
| 2422 | fHWStencilTestEnabled = kNo_TriState; |
| 2423 | } |
| 2424 | } else { |
| 2425 | if (kYes_TriState != fHWStencilTestEnabled) { |
| 2426 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
| 2427 | fHWStencilTestEnabled = kYes_TriState; |
| 2428 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2429 | } |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2430 | if (!stencilSettings.isDisabled()) { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 2431 | if (this->caps()->twoSidedStencilSupport()) { |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2432 | set_gl_stencil(this->glInterface(), |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2433 | stencilSettings, |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2434 | GR_GL_FRONT, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2435 | GrStencilSettings::kFront_Face); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2436 | set_gl_stencil(this->glInterface(), |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2437 | stencilSettings, |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2438 | GR_GL_BACK, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2439 | GrStencilSettings::kBack_Face); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2440 | } else { |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2441 | set_gl_stencil(this->glInterface(), |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2442 | stencilSettings, |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2443 | GR_GL_FRONT_AND_BACK, |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2444 | GrStencilSettings::kFront_Face); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2445 | } |
| 2446 | } |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2447 | fHWStencilSettings = stencilSettings; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
cdalton | 7e806f3 | 2015-11-11 15:16:07 -0800 | [diff] [blame] | 2451 | void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 2452 | SkASSERT(!useHWAA || rt->isStencilBufferMultisampled()); |
bsalomon@google.com | 202d139 | 2013-03-19 18:58:08 +0000 | [diff] [blame] | 2453 | |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 2454 | if (this->glCaps().multisampleDisableSupport()) { |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 2455 | if (useHWAA) { |
| 2456 | if (kYes_TriState != fMSAAEnabled) { |
| 2457 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
| 2458 | fMSAAEnabled = kYes_TriState; |
| 2459 | } |
| 2460 | } else { |
| 2461 | if (kNo_TriState != fMSAAEnabled) { |
| 2462 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 2463 | fMSAAEnabled = kNo_TriState; |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2469 | void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) { |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2470 | // Any optimization to disable blending should have already been applied and |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2471 | // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0). |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 2472 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2473 | GrBlendEquation equation = blendInfo.fEquation; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 2474 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 2475 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2476 | bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) && |
| 2477 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff; |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2478 | if (blendOff) { |
| 2479 | if (kNo_TriState != fHWBlendState.fEnabled) { |
| 2480 | GL_CALL(Disable(GR_GL_BLEND)); |
joel.liang | 9764c40 | 2015-07-09 19:46:18 -0700 | [diff] [blame] | 2481 | |
| 2482 | // Workaround for the ARM KHR_blend_equation_advanced blacklist issue |
| 2483 | // https://code.google.com/p/skia/issues/detail?id=3943 |
| 2484 | if (kARM_GrGLVendor == this->ctxInfo().vendor() && |
| 2485 | GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) { |
| 2486 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2487 | // Set to any basic blending equation. |
| 2488 | GrBlendEquation blend_equation = kAdd_GrBlendEquation; |
| 2489 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation])); |
| 2490 | fHWBlendState.fEquation = blend_equation; |
| 2491 | } |
| 2492 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2493 | fHWBlendState.fEnabled = kNo_TriState; |
| 2494 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2495 | return; |
| 2496 | } |
| 2497 | |
| 2498 | if (kYes_TriState != fHWBlendState.fEnabled) { |
| 2499 | GL_CALL(Enable(GR_GL_BLEND)); |
| 2500 | fHWBlendState.fEnabled = kYes_TriState; |
| 2501 | } |
| 2502 | |
| 2503 | if (fHWBlendState.fEquation != equation) { |
| 2504 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation])); |
| 2505 | fHWBlendState.fEquation = equation; |
| 2506 | } |
| 2507 | |
| 2508 | if (GrBlendEquationIsAdvanced(equation)) { |
| 2509 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2510 | // Advanced equations have no other blend state. |
| 2511 | return; |
| 2512 | } |
| 2513 | |
| 2514 | if (fHWBlendState.fSrcCoeff != srcCoeff || |
| 2515 | fHWBlendState.fDstCoeff != dstCoeff) { |
| 2516 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 2517 | gXfermodeCoeff2Blend[dstCoeff])); |
| 2518 | fHWBlendState.fSrcCoeff = srcCoeff; |
| 2519 | fHWBlendState.fDstCoeff = dstCoeff; |
| 2520 | } |
| 2521 | |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2522 | if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) { |
| 2523 | GrColor blendConst = blendInfo.fBlendConstant; |
| 2524 | blendConst = swizzle.applyTo(blendConst); |
| 2525 | if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) { |
| 2526 | GrGLfloat c[4]; |
| 2527 | GrColorToRGBAFloat(blendConst, c); |
| 2528 | GL_CALL(BlendColor(c[0], c[1], c[2], c[3])); |
| 2529 | fHWBlendState.fConstColor = blendConst; |
| 2530 | fHWBlendState.fConstColorValid = true; |
| 2531 | } |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2532 | } |
| 2533 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2534 | |
commit-bot@chromium.org | 6364b5e | 2013-08-20 20:22:52 +0000 | [diff] [blame] | 2535 | static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) { |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2536 | static const GrGLenum gWrapModes[] = { |
| 2537 | GR_GL_CLAMP_TO_EDGE, |
| 2538 | GR_GL_REPEAT, |
| 2539 | GR_GL_MIRRORED_REPEAT |
| 2540 | }; |
commit-bot@chromium.org | 5d7ca95 | 2013-04-22 20:26:44 +0000 | [diff] [blame] | 2541 | GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes)); |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2542 | GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode); |
| 2543 | GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode); |
| 2544 | GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode); |
| 2545 | return gWrapModes[tm]; |
| 2546 | } |
| 2547 | |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2548 | static GrGLenum get_component_enum_from_char(char component) { |
| 2549 | switch (component) { |
| 2550 | case 'r': |
| 2551 | return GR_GL_RED; |
| 2552 | case 'g': |
| 2553 | return GR_GL_GREEN; |
| 2554 | case 'b': |
| 2555 | return GR_GL_BLUE; |
| 2556 | case 'a': |
| 2557 | return GR_GL_ALPHA; |
| 2558 | default: |
| 2559 | SkFAIL("Unsupported component"); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 2565 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 2566 | static void get_tex_param_swizzle(GrPixelConfig config, |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2567 | const GrGLCaps& caps, |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2568 | GrGLenum* glSwizzle) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2569 | const GrSwizzle& swizzle = caps.configSwizzle(config); |
egdaniel | b7e7d57 | 2015-11-04 04:23:53 -0800 | [diff] [blame] | 2570 | for (int i = 0; i < 4; ++i) { |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2571 | glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 2575 | void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2576 | SkASSERT(texture); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 2577 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 2578 | #ifdef SK_DEBUG |
| 2579 | if (!this->caps()->npotTextureTileSupport()) { |
| 2580 | const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX(); |
| 2581 | const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY(); |
| 2582 | if (tileX || tileY) { |
| 2583 | const int w = texture->width(); |
| 2584 | const int h = texture->height(); |
| 2585 | SkASSERT(SkIsPow2(w) && SkIsPow2(h)); |
| 2586 | } |
| 2587 | } |
| 2588 | #endif |
| 2589 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2590 | // If we created a rt/tex and rendered to it without using a texture and now we're texturing |
| 2591 | // 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] | 2592 | // out of the "last != next" check. |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 2593 | GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2594 | if (texRT) { |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2595 | this->onResolveRenderTarget(texRT); |
| 2596 | } |
| 2597 | |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 2598 | uint32_t textureID = texture->getUniqueID(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2599 | GrGLenum target = texture->target(); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 2600 | if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2601 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2602 | GL_CALL(BindTexture(target, texture->textureID())); |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 2603 | fHWBoundTextureUniqueIDs[unitIdx] = textureID; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2606 | ResetTimestamp timestamp; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2607 | const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(×tamp); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2608 | bool setAll = timestamp < this->getResetTimestamp(); |
| 2609 | GrGLTexture::TexParams newTexParams; |
| 2610 | |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 2611 | static GrGLenum glMinFilterModes[] = { |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 2612 | GR_GL_NEAREST, |
| 2613 | GR_GL_LINEAR, |
| 2614 | GR_GL_LINEAR_MIPMAP_LINEAR |
| 2615 | }; |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 2616 | static GrGLenum glMagFilterModes[] = { |
| 2617 | GR_GL_NEAREST, |
| 2618 | GR_GL_LINEAR, |
| 2619 | GR_GL_LINEAR |
| 2620 | }; |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 2621 | GrTextureParams::FilterMode filterMode = params.filterMode(); |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 2622 | |
| 2623 | if (GrTextureParams::kMipMap_FilterMode == filterMode) { |
| 2624 | if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) { |
| 2625 | filterMode = GrTextureParams::kBilerp_FilterMode; |
| 2626 | } |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 2627 | } |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 2628 | |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 2629 | newTexParams.fMinFilter = glMinFilterModes[filterMode]; |
| 2630 | newTexParams.fMagFilter = glMagFilterModes[filterMode]; |
skia.committer@gmail.com | aeefb2a | 2013-07-27 07:01:06 +0000 | [diff] [blame] | 2631 | |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 2632 | if (GrTextureParams::kMipMap_FilterMode == filterMode && |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 2633 | texture->texturePriv().mipMapsAreDirty()) { |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2634 | GL_CALL(GenerateMipmap(target)); |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 2635 | texture->texturePriv().dirtyMipMaps(false); |
commit-bot@chromium.org | cffff79 | 2013-07-26 16:36:04 +0000 | [diff] [blame] | 2636 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2637 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 2638 | newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX()); |
| 2639 | newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2640 | get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA); |
commit-bot@chromium.org | 149f4f5 | 2013-07-26 20:40:06 +0000 | [diff] [blame] | 2641 | if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2642 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2643 | 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] | 2644 | } |
| 2645 | if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) { |
| 2646 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2647 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2648 | } |
| 2649 | if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2650 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2651 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2652 | } |
| 2653 | if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2654 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2655 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT)); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2656 | } |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 2657 | if (this->glCaps().textureSwizzleSupport() && |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2658 | (setAll || memcmp(newTexParams.fSwizzleRGBA, |
| 2659 | oldTexParams.fSwizzleRGBA, |
| 2660 | sizeof(newTexParams.fSwizzleRGBA)))) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2661 | this->setTextureUnit(unitIdx); |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 2662 | if (this->glStandard() == kGLES_GrGLStandard) { |
commit-bot@chromium.org | 6364b5e | 2013-08-20 20:22:52 +0000 | [diff] [blame] | 2663 | // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA. |
| 2664 | const GrGLenum* swizzle = newTexParams.fSwizzleRGBA; |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2665 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0])); |
| 2666 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1])); |
| 2667 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2])); |
| 2668 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3])); |
commit-bot@chromium.org | 6364b5e | 2013-08-20 20:22:52 +0000 | [diff] [blame] | 2669 | } else { |
| 2670 | GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint)); |
| 2671 | const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2672 | GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle)); |
commit-bot@chromium.org | 6364b5e | 2013-08-20 20:22:52 +0000 | [diff] [blame] | 2673 | } |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2674 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2675 | texture->setCachedTexParams(newTexParams, this->getResetTimestamp()); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 2678 | void GrGLGpu::flushColorWrite(bool writeColor) { |
| 2679 | if (!writeColor) { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2680 | if (kNo_TriState != fHWWriteToColor) { |
| 2681 | GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE, |
| 2682 | GR_GL_FALSE, GR_GL_FALSE)); |
| 2683 | fHWWriteToColor = kNo_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2684 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2685 | } else { |
| 2686 | if (kYes_TriState != fHWWriteToColor) { |
| 2687 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
| 2688 | fHWWriteToColor = kYes_TriState; |
| 2689 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2690 | } |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2691 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2692 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2693 | void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) { |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2694 | if (fHWDrawFace != face) { |
| 2695 | switch (face) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2696 | case GrPipelineBuilder::kCCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2697 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2698 | GL_CALL(CullFace(GR_GL_BACK)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2699 | break; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2700 | case GrPipelineBuilder::kCW_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2701 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2702 | GL_CALL(CullFace(GR_GL_FRONT)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2703 | break; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 2704 | case GrPipelineBuilder::kBoth_DrawFace: |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2705 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2706 | break; |
| 2707 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 2708 | SkFAIL("Unknown draw face."); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2709 | } |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2710 | fHWDrawFace = face; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2711 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 2714 | void GrGLGpu::setTextureUnit(int unit) { |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 2715 | SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2716 | if (unit != fHWActiveTextureUnitIdx) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2717 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 4920939 | 2012-06-05 15:13:46 +0000 | [diff] [blame] | 2718 | fHWActiveTextureUnitIdx = unit; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2721 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 2722 | void GrGLGpu::setScratchTextureUnit() { |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2723 | // 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] | 2724 | int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1; |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2725 | if (lastUnitIdx != fHWActiveTextureUnitIdx) { |
| 2726 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx)); |
| 2727 | fHWActiveTextureUnitIdx = lastUnitIdx; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2728 | } |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2729 | // clear out the this field so that if a program does use this unit it will rebind the correct |
| 2730 | // texture. |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 2731 | fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 2734 | // Determines whether glBlitFramebuffer could be used between src and dst. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2735 | static inline bool can_blit_framebuffer(const GrSurface* dst, |
| 2736 | const GrSurface* src, |
| 2737 | const GrGLGpu* gpu) { |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 2738 | if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) && |
| 2739 | gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) && |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 2740 | gpu->glCaps().usesMSAARenderBuffers()) { |
commit-bot@chromium.org | a8e5a06 | 2013-09-05 23:44:09 +0000 | [diff] [blame] | 2741 | // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match |
| 2742 | // or the rects are not the same (not just the same size but have the same edges). |
| 2743 | if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() && |
| 2744 | (src->desc().fSampleCnt > 0 || src->config() != dst->config())) { |
| 2745 | return false; |
| 2746 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2747 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
| 2748 | if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) { |
| 2749 | return false; |
| 2750 | } |
| 2751 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
| 2752 | if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) { |
| 2753 | return false; |
| 2754 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2755 | return true; |
| 2756 | } else { |
| 2757 | return false; |
| 2758 | } |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 2759 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2760 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2761 | static inline bool can_copy_texsubimage(const GrSurface* dst, |
| 2762 | const GrSurface* src, |
| 2763 | const GrGLGpu* gpu) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2764 | // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage |
| 2765 | // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps |
| 2766 | // 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] | 2767 | if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() && |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2768 | (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) { |
| 2769 | return false; |
| 2770 | } |
| 2771 | const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget()); |
| 2772 | // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer) |
| 2773 | // 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] | 2774 | if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2775 | return false; |
| 2776 | } |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 2777 | const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget()); |
| 2778 | // If the src is multisampled (and uses an extension where there is a separate MSAA |
| 2779 | // renderbuffer) then it is an invalid operation to call CopyTexSubImage |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2780 | if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) { |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 2781 | return false; |
| 2782 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2783 | |
| 2784 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
| 2785 | // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a |
| 2786 | // texture. |
| 2787 | if (!dstTex) { |
| 2788 | return false; |
| 2789 | } |
| 2790 | |
| 2791 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
| 2792 | |
| 2793 | // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring |
| 2794 | // is required. |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 2795 | if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) && |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2796 | !GrPixelConfigIsCompressed(src->config()) && |
| 2797 | (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && |
| 2798 | dstTex->target() == GR_GL_TEXTURE_2D && |
| 2799 | dst->origin() == src->origin()) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2800 | return true; |
| 2801 | } else { |
| 2802 | return false; |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | // If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is |
| 2807 | // relative to is output. |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2808 | void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport, |
| 2809 | TempFBOTarget tempFBOTarget) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2810 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2811 | if (nullptr == rt) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2812 | SkASSERT(surface->asTexture()); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2813 | GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2814 | GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2815 | GrGLuint* tempFBOID; |
| 2816 | tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID; |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2817 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2818 | if (0 == *tempFBOID) { |
| 2819 | GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2820 | } |
| 2821 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2822 | fStats.incRenderTargetBinds(); |
| 2823 | GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID)); |
| 2824 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 2825 | GR_GL_COLOR_ATTACHMENT0, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2826 | target, |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 2827 | texID, |
| 2828 | 0)); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2829 | viewport->fLeft = 0; |
| 2830 | viewport->fBottom = 0; |
| 2831 | viewport->fWidth = surface->width(); |
| 2832 | viewport->fHeight = surface->height(); |
| 2833 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2834 | fStats.incRenderTargetBinds(); |
| 2835 | GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID())); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2836 | *viewport = rt->getViewport(); |
| 2837 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2838 | } |
| 2839 | |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2840 | void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) { |
| 2841 | // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to |
| 2842 | if (!surface->asRenderTarget()) { |
| 2843 | SkASSERT(surface->asTexture()); |
| 2844 | GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target(); |
| 2845 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
| 2846 | GR_GL_COLOR_ATTACHMENT0, |
| 2847 | textureTarget, |
| 2848 | 0, |
| 2849 | 0)); |
| 2850 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 2853 | bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 2854 | // If the src is a texture, we can implement the blit as a draw assuming the config is |
| 2855 | // renderable. |
| 2856 | if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) { |
| 2857 | desc->fOrigin = kDefault_GrSurfaceOrigin; |
| 2858 | desc->fFlags = kRenderTarget_GrSurfaceFlag; |
| 2859 | desc->fConfig = src->config(); |
| 2860 | return true; |
| 2861 | } |
| 2862 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2863 | const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture()); |
| 2864 | if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) { |
| 2865 | // Not supported for FBO blit or CopyTexSubImage |
| 2866 | return false; |
| 2867 | } |
| 2868 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 2869 | // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2870 | // possible and we return false to fallback to creating a render target dst for render-to- |
| 2871 | // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo |
| 2872 | // creation. It isn't clear that avoiding temporary fbo creation is actually optimal. |
| 2873 | |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2874 | // Check for format issues with glCopyTexSubImage2D |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 2875 | if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() && |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2876 | kBGRA_8888_GrPixelConfig == src->config()) { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2877 | // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit |
| 2878 | // then we set up for that, otherwise fail. |
| 2879 | if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) { |
| 2880 | desc->fOrigin = kDefault_GrSurfaceOrigin; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 2881 | desc->fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2882 | desc->fConfig = kBGRA_8888_GrPixelConfig; |
| 2883 | return true; |
| 2884 | } |
| 2885 | return false; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2886 | } else if (nullptr == src->asRenderTarget()) { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2887 | // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src. |
| 2888 | return false; |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget()); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2892 | if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2893 | // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or |
| 2894 | // fail. |
| 2895 | if (this->caps()->isConfigRenderable(src->config(), false)) { |
| 2896 | desc->fOrigin = kDefault_GrSurfaceOrigin; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 2897 | desc->fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2898 | desc->fConfig = src->config(); |
| 2899 | return true; |
| 2900 | } |
| 2901 | return false; |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2902 | } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 2903 | |
| 2904 | // We'll do a CopyTexSubImage. Make the dst a plain old texture. |
| 2905 | desc->fConfig = src->config(); |
| 2906 | desc->fOrigin = src->origin(); |
| 2907 | desc->fFlags = kNone_GrSurfaceFlags; |
| 2908 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 2911 | bool GrGLGpu::onCopySurface(GrSurface* dst, |
| 2912 | GrSurface* src, |
| 2913 | const SkIRect& srcRect, |
| 2914 | const SkIPoint& dstPoint) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2915 | // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the |
| 2916 | // swizzle. |
| 2917 | if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) != |
| 2918 | this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) { |
| 2919 | return false; |
| 2920 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 2921 | if (src->asTexture() && dst->asRenderTarget()) { |
| 2922 | this->copySurfaceAsDraw(dst, src, srcRect, dstPoint); |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 2923 | return true; |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 2924 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 2925 | |
| 2926 | if (can_copy_texsubimage(dst, src, this)) { |
| 2927 | this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint); |
| 2928 | return true; |
| 2929 | } |
| 2930 | |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 2931 | if (can_blit_framebuffer(dst, src, this)) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 2932 | return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint); |
| 2933 | } |
| 2934 | |
| 2935 | return false; |
| 2936 | } |
| 2937 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2938 | void GrGLGpu::createCopyPrograms() { |
| 2939 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 2940 | fCopyPrograms[i].fProgram = 0; |
bsalomon | 27a0487 | 2015-11-20 19:34:37 -0800 | [diff] [blame] | 2941 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2942 | const char* version = this->glCaps().glslCaps()->versionDeclString(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 2943 | static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType, |
| 2944 | kSampler2DRect_GrSLType }; |
| 2945 | SkASSERT(3 == SK_ARRAY_COUNT(fCopyPrograms)); |
| 2946 | for (int i = 0; i < 3; ++i) { |
| 2947 | if (kSamplerExternal_GrSLType == kSamplerTypes[i] && |
| 2948 | !this->glCaps().externalTextureSupport()) { |
| 2949 | continue; |
| 2950 | } |
| 2951 | if (kSampler2DRect_GrSLType == kSamplerTypes[i] && |
| 2952 | !this->glCaps().rectangleTextureSupport()) { |
| 2953 | continue; |
| 2954 | } |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2955 | GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier); |
| 2956 | GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType, |
| 2957 | GrShaderVar::kUniform_TypeModifier); |
| 2958 | GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, |
| 2959 | GrShaderVar::kUniform_TypeModifier); |
| 2960 | GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[i], |
| 2961 | GrShaderVar::kUniform_TypeModifier); |
| 2962 | GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, |
| 2963 | GrShaderVar::kVaryingOut_TypeModifier); |
| 2964 | GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, |
| 2965 | GrShaderVar::kOut_TypeModifier); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 2966 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2967 | SkString vshaderTxt(version); |
| 2968 | aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 2969 | vshaderTxt.append(";"); |
| 2970 | uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 2971 | vshaderTxt.append(";"); |
| 2972 | uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 2973 | vshaderTxt.append(";"); |
| 2974 | vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 2975 | vshaderTxt.append(";"); |
| 2976 | |
| 2977 | vshaderTxt.append( |
| 2978 | "// Copy Program VS\n" |
| 2979 | "void main() {" |
| 2980 | " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;" |
| 2981 | " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;" |
| 2982 | " gl_Position.zw = vec2(0, 1);" |
| 2983 | "}" |
| 2984 | ); |
bsalomon | 27a0487 | 2015-11-20 19:34:37 -0800 | [diff] [blame] | 2985 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2986 | SkString fshaderTxt(version); |
| 2987 | if (kSamplerTypes[i] == kSamplerExternal_GrSLType) { |
| 2988 | fshaderTxt.appendf("#extension %s : require\n", |
| 2989 | this->glCaps().glslCaps()->externalTextureExtensionString()); |
| 2990 | } |
| 2991 | GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
| 2992 | *this->glCaps().glslCaps(), |
| 2993 | &fshaderTxt); |
| 2994 | vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier); |
| 2995 | vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt); |
| 2996 | fshaderTxt.append(";"); |
| 2997 | uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt); |
| 2998 | fshaderTxt.append(";"); |
| 2999 | const char* fsOutName; |
| 3000 | if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 3001 | oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt); |
| 3002 | fshaderTxt.append(";"); |
| 3003 | fsOutName = oFragColor.c_str(); |
| 3004 | } else { |
| 3005 | fsOutName = "gl_FragColor"; |
| 3006 | } |
| 3007 | fshaderTxt.appendf( |
| 3008 | "// Copy Program FS\n" |
| 3009 | "void main() {" |
| 3010 | " %s = %s(u_texture, v_texCoord);" |
| 3011 | "}", |
| 3012 | fsOutName, |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 3013 | GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[i], this->glslGeneration()) |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3014 | ); |
| 3015 | |
| 3016 | GL_CALL_RET(fCopyPrograms[i].fProgram, CreateProgram()); |
| 3017 | const char* str; |
| 3018 | GrGLint length; |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3019 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3020 | str = vshaderTxt.c_str(); |
| 3021 | length = SkToInt(vshaderTxt.size()); |
| 3022 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram, |
| 3023 | GR_GL_VERTEX_SHADER, &str, &length, 1, |
| 3024 | &fStats); |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3025 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3026 | str = fshaderTxt.c_str(); |
| 3027 | length = SkToInt(fshaderTxt.size()); |
| 3028 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram, |
| 3029 | GR_GL_FRAGMENT_SHADER, &str, &length, 1, |
| 3030 | &fStats); |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3031 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3032 | GL_CALL(LinkProgram(fCopyPrograms[i].fProgram)); |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3033 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3034 | GL_CALL_RET(fCopyPrograms[i].fTextureUniform, |
| 3035 | GetUniformLocation(fCopyPrograms[i].fProgram, "u_texture")); |
| 3036 | GL_CALL_RET(fCopyPrograms[i].fPosXformUniform, |
| 3037 | GetUniformLocation(fCopyPrograms[i].fProgram, "u_posXform")); |
| 3038 | GL_CALL_RET(fCopyPrograms[i].fTexCoordXformUniform, |
| 3039 | GetUniformLocation(fCopyPrograms[i].fProgram, "u_texCoordXform")); |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3040 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3041 | GL_CALL(BindAttribLocation(fCopyPrograms[i].fProgram, 0, "a_vertex")); |
bsalomon | 0315dbc | 2015-11-20 20:24:31 -0800 | [diff] [blame] | 3042 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3043 | GL_CALL(DeleteShader(vshader)); |
| 3044 | GL_CALL(DeleteShader(fshader)); |
| 3045 | } |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 3046 | fCopyProgramArrayBuffer = 0; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3047 | GL_CALL(GenBuffers(1, &fCopyProgramArrayBuffer)); |
| 3048 | fHWGeometryState.setVertexBufferID(this, fCopyProgramArrayBuffer); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3049 | static const GrGLfloat vdata[] = { |
| 3050 | 0, 0, |
| 3051 | 0, 1, |
| 3052 | 1, 0, |
| 3053 | 1, 1 |
| 3054 | }; |
| 3055 | GL_ALLOC_CALL(this->glInterface(), |
| 3056 | BufferData(GR_GL_ARRAY_BUFFER, |
| 3057 | (GrGLsizeiptr) sizeof(vdata), |
| 3058 | vdata, // data ptr |
| 3059 | GR_GL_STATIC_DRAW)); |
| 3060 | } |
| 3061 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3062 | void GrGLGpu::createWireRectProgram() { |
| 3063 | SkASSERT(!fWireRectProgram.fProgram); |
| 3064 | GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier); |
| 3065 | GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier); |
| 3066 | GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier); |
| 3067 | const char* version = this->glCaps().glslCaps()->versionDeclString(); |
| 3068 | |
| 3069 | // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The |
| 3070 | // program is used with a vbo containing the unit square. Vertices are computed from the rect |
| 3071 | // uniform using the 4 vbo vertices. |
| 3072 | SkString vshaderTxt(version); |
| 3073 | aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 3074 | vshaderTxt.append(";"); |
| 3075 | uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt); |
| 3076 | vshaderTxt.append(";"); |
| 3077 | vshaderTxt.append( |
| 3078 | "// Wire Rect Program VS\n" |
| 3079 | "void main() {" |
| 3080 | " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);" |
| 3081 | " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);" |
| 3082 | " gl_Position.zw = vec2(0, 1);" |
| 3083 | "}" |
| 3084 | ); |
| 3085 | |
| 3086 | GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier); |
| 3087 | |
| 3088 | SkString fshaderTxt(version); |
| 3089 | GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
| 3090 | *this->glCaps().glslCaps(), |
| 3091 | &fshaderTxt); |
| 3092 | uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt); |
| 3093 | fshaderTxt.append(";"); |
| 3094 | const char* fsOutName; |
| 3095 | if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 3096 | oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt); |
| 3097 | fshaderTxt.append(";"); |
| 3098 | fsOutName = oFragColor.c_str(); |
| 3099 | } else { |
| 3100 | fsOutName = "gl_FragColor"; |
| 3101 | } |
| 3102 | fshaderTxt.appendf( |
| 3103 | "// Write Rect Program FS\n" |
| 3104 | "void main() {" |
| 3105 | " %s = %s;" |
| 3106 | "}", |
| 3107 | fsOutName, |
| 3108 | uColor.c_str() |
| 3109 | ); |
| 3110 | |
| 3111 | GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram()); |
| 3112 | const char* str; |
| 3113 | GrGLint length; |
| 3114 | |
| 3115 | str = vshaderTxt.c_str(); |
| 3116 | length = SkToInt(vshaderTxt.size()); |
| 3117 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram, |
| 3118 | GR_GL_VERTEX_SHADER, &str, &length, 1, |
| 3119 | &fStats); |
| 3120 | |
| 3121 | str = fshaderTxt.c_str(); |
| 3122 | length = SkToInt(fshaderTxt.size()); |
| 3123 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram, |
| 3124 | GR_GL_FRAGMENT_SHADER, &str, &length, 1, |
| 3125 | &fStats); |
| 3126 | |
| 3127 | GL_CALL(LinkProgram(fWireRectProgram.fProgram)); |
| 3128 | |
| 3129 | GL_CALL_RET(fWireRectProgram.fColorUniform, |
| 3130 | GetUniformLocation(fWireRectProgram.fProgram, "u_color")); |
| 3131 | GL_CALL_RET(fWireRectProgram.fRectUniform, |
| 3132 | GetUniformLocation(fWireRectProgram.fProgram, "u_rect")); |
| 3133 | GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex")); |
| 3134 | |
| 3135 | GL_CALL(DeleteShader(vshader)); |
| 3136 | GL_CALL(DeleteShader(fshader)); |
| 3137 | GL_CALL(GenBuffers(1, &fWireRectArrayBuffer)); |
| 3138 | fHWGeometryState.setVertexBufferID(this, fWireRectArrayBuffer); |
| 3139 | static const GrGLfloat vdata[] = { |
| 3140 | 0, 0, |
| 3141 | 0, 1, |
| 3142 | 1, 1, |
| 3143 | 1, 0, |
| 3144 | }; |
| 3145 | GL_ALLOC_CALL(this->glInterface(), |
| 3146 | BufferData(GR_GL_ARRAY_BUFFER, |
| 3147 | (GrGLsizeiptr) sizeof(vdata), |
| 3148 | vdata, // data ptr |
| 3149 | GR_GL_STATIC_DRAW)); |
| 3150 | } |
| 3151 | |
| 3152 | void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) { |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3153 | // TODO: This should swizzle the output to match dst's config, though it is a debugging |
| 3154 | // visualization. |
| 3155 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3156 | this->handleDirtyContext(); |
| 3157 | if (!fWireRectProgram.fProgram) { |
| 3158 | this->createWireRectProgram(); |
| 3159 | } |
| 3160 | |
| 3161 | int w = rt->width(); |
| 3162 | int h = rt->height(); |
| 3163 | |
| 3164 | // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider |
| 3165 | // whether the render target is flipped or not. |
| 3166 | GrGLfloat edges[4]; |
| 3167 | edges[0] = SkIntToScalar(rect.fLeft) + 0.5f; |
| 3168 | edges[2] = SkIntToScalar(rect.fRight) - 0.5f; |
| 3169 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 3170 | edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f); |
| 3171 | edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f); |
| 3172 | } else { |
| 3173 | edges[1] = SkIntToScalar(rect.fTop) + 0.5f; |
| 3174 | edges[3] = SkIntToScalar(rect.fBottom) - 0.5f; |
| 3175 | } |
| 3176 | edges[0] = 2 * edges[0] / w - 1.0f; |
| 3177 | edges[1] = 2 * edges[1] / h - 1.0f; |
| 3178 | edges[2] = 2 * edges[2] / w - 1.0f; |
| 3179 | edges[3] = 2 * edges[3] / h - 1.0f; |
| 3180 | |
| 3181 | GrGLfloat channels[4]; |
| 3182 | static const GrGLfloat scale255 = 1.f / 255.f; |
| 3183 | channels[0] = GrColorUnpackR(color) * scale255; |
| 3184 | channels[1] = GrColorUnpackG(color) * scale255; |
| 3185 | channels[2] = GrColorUnpackB(color) * scale255; |
| 3186 | channels[3] = GrColorUnpackA(color) * scale255; |
| 3187 | |
| 3188 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget()); |
| 3189 | this->flushRenderTarget(glRT, &rect); |
| 3190 | |
| 3191 | GL_CALL(UseProgram(fWireRectProgram.fProgram)); |
| 3192 | fHWProgramID = fWireRectProgram.fProgram; |
| 3193 | |
| 3194 | fHWGeometryState.setVertexArrayID(this, 0); |
| 3195 | |
| 3196 | GrGLAttribArrayState* attribs = |
| 3197 | fHWGeometryState.bindArrayAndBufferToDraw(this, fWireRectArrayBuffer); |
| 3198 | attribs->set(this, 0, fWireRectArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0); |
| 3199 | attribs->disableUnusedArrays(this, 0x1); |
| 3200 | |
| 3201 | GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges)); |
| 3202 | GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels)); |
| 3203 | |
| 3204 | GrXferProcessor::BlendInfo blendInfo; |
| 3205 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3206 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3207 | this->flushColorWrite(true); |
| 3208 | this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace); |
| 3209 | this->flushHWAAState(glRT, false); |
| 3210 | this->disableScissor(); |
| 3211 | GrStencilSettings stencil; |
| 3212 | stencil.setDisabled(); |
| 3213 | this->flushStencil(stencil); |
| 3214 | |
| 3215 | GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4)); |
| 3216 | } |
| 3217 | |
| 3218 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3219 | void GrGLGpu::copySurfaceAsDraw(GrSurface* dst, |
| 3220 | GrSurface* src, |
| 3221 | const SkIRect& srcRect, |
| 3222 | const SkIPoint& dstPoint) { |
| 3223 | int w = srcRect.width(); |
| 3224 | int h = srcRect.height(); |
| 3225 | |
| 3226 | GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture()); |
| 3227 | GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
| 3228 | this->bindTexture(0, params, srcTex); |
| 3229 | |
| 3230 | GrGLRenderTarget* dstRT = static_cast<GrGLRenderTarget*>(dst->asRenderTarget()); |
| 3231 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h); |
| 3232 | this->flushRenderTarget(dstRT, &dstRect); |
| 3233 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3234 | int progIdx = TextureTargetToCopyProgramIdx(srcTex->target()); |
| 3235 | |
| 3236 | GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram)); |
| 3237 | fHWProgramID = fCopyPrograms[progIdx].fProgram; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3238 | |
| 3239 | fHWGeometryState.setVertexArrayID(this, 0); |
| 3240 | |
| 3241 | GrGLAttribArrayState* attribs = |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3242 | fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgramArrayBuffer); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 3243 | attribs->set(this, 0, fCopyProgramArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0); |
bsalomon | d624634 | 2015-06-04 13:57:00 -0700 | [diff] [blame] | 3244 | attribs->disableUnusedArrays(this, 0x1); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3245 | |
| 3246 | // dst rect edges in NDC (-1 to 1) |
| 3247 | int dw = dst->width(); |
| 3248 | int dh = dst->height(); |
| 3249 | GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f; |
| 3250 | GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f; |
| 3251 | GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f; |
| 3252 | GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f; |
| 3253 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 3254 | dy0 = -dy0; |
| 3255 | dy1 = -dy1; |
| 3256 | } |
| 3257 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 3258 | GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft; |
| 3259 | GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w); |
| 3260 | GrGLfloat sy0 = (GrGLfloat)srcRect.fTop; |
| 3261 | GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3262 | int sh = src->height(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3263 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 3264 | sy0 = sh - sy0; |
| 3265 | sy1 = sh - sy1; |
| 3266 | } |
| 3267 | // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture. |
| 3268 | GrGLenum srcTarget = srcTex->target(); |
| 3269 | if (GR_GL_TEXTURE_RECTANGLE != srcTarget) { |
| 3270 | int sw = src->width(); |
| 3271 | sx0 /= sw; |
| 3272 | sx1 /= sw; |
| 3273 | sy0 /= sh; |
| 3274 | sy1 /= sh; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3275 | } |
| 3276 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 3277 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0)); |
| 3278 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 3279 | sx1 - sx0, sy1 - sy0, sx0, sy0)); |
| 3280 | GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3281 | |
| 3282 | GrXferProcessor::BlendInfo blendInfo; |
| 3283 | blendInfo.reset(); |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 3284 | this->flushBlend(blendInfo, GrSwizzle::RGBA()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3285 | this->flushColorWrite(true); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3286 | this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace); |
cdalton | 7e806f3 | 2015-11-11 15:16:07 -0800 | [diff] [blame] | 3287 | this->flushHWAAState(dstRT, false); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3288 | this->disableScissor(); |
| 3289 | GrStencilSettings stencil; |
| 3290 | stencil.setDisabled(); |
| 3291 | this->flushStencil(stencil); |
| 3292 | |
| 3293 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 3294 | } |
| 3295 | |
| 3296 | void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, |
| 3297 | GrSurface* src, |
| 3298 | const SkIRect& srcRect, |
| 3299 | const SkIPoint& dstPoint) { |
| 3300 | SkASSERT(can_copy_texsubimage(dst, src, this)); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3301 | GrGLIRect srcVP; |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3302 | this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3303 | GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture()); |
| 3304 | SkASSERT(dstTex); |
| 3305 | // We modified the bound FBO |
| 3306 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
| 3307 | GrGLIRect srcGLRect; |
| 3308 | srcGLRect.setRelativeTo(srcVP, |
| 3309 | srcRect.fLeft, |
| 3310 | srcRect.fTop, |
| 3311 | srcRect.width(), |
| 3312 | srcRect.height(), |
| 3313 | src->origin()); |
| 3314 | |
| 3315 | this->setScratchTextureUnit(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3316 | GL_CALL(BindTexture(dstTex->target(), dstTex->textureID())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3317 | GrGLint dstY; |
| 3318 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 3319 | dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight); |
| 3320 | } else { |
| 3321 | dstY = dstPoint.fY; |
| 3322 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3323 | GL_CALL(CopyTexSubImage2D(dstTex->target(), 0, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3324 | dstPoint.fX, dstY, |
| 3325 | srcGLRect.fLeft, srcGLRect.fBottom, |
| 3326 | srcGLRect.fWidth, srcGLRect.fHeight)); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3327 | this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, |
| 3331 | GrSurface* src, |
| 3332 | const SkIRect& srcRect, |
| 3333 | const SkIPoint& dstPoint) { |
| 3334 | SkASSERT(can_blit_framebuffer(dst, src, this)); |
| 3335 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 3336 | srcRect.width(), srcRect.height()); |
| 3337 | if (dst == src) { |
| 3338 | if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) { |
| 3339 | return false; |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 3340 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 3341 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3342 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3343 | GrGLIRect dstVP; |
| 3344 | GrGLIRect srcVP; |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3345 | this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget); |
| 3346 | this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3347 | // We modified the bound FBO |
| 3348 | fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID; |
| 3349 | GrGLIRect srcGLRect; |
| 3350 | GrGLIRect dstGLRect; |
| 3351 | srcGLRect.setRelativeTo(srcVP, |
| 3352 | srcRect.fLeft, |
| 3353 | srcRect.fTop, |
| 3354 | srcRect.width(), |
| 3355 | srcRect.height(), |
| 3356 | src->origin()); |
| 3357 | dstGLRect.setRelativeTo(dstVP, |
| 3358 | dstRect.fLeft, |
| 3359 | dstRect.fTop, |
| 3360 | dstRect.width(), |
| 3361 | dstRect.height(), |
| 3362 | dst->origin()); |
| 3363 | |
| 3364 | // BlitFrameBuffer respects the scissor, so disable it. |
| 3365 | this->disableScissor(); |
| 3366 | |
| 3367 | GrGLint srcY0; |
| 3368 | GrGLint srcY1; |
| 3369 | // Does the blit need to y-mirror or not? |
| 3370 | if (src->origin() == dst->origin()) { |
| 3371 | srcY0 = srcGLRect.fBottom; |
| 3372 | srcY1 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 3373 | } else { |
| 3374 | srcY0 = srcGLRect.fBottom + srcGLRect.fHeight; |
| 3375 | srcY1 = srcGLRect.fBottom; |
| 3376 | } |
| 3377 | GL_CALL(BlitFramebuffer(srcGLRect.fLeft, |
| 3378 | srcY0, |
| 3379 | srcGLRect.fLeft + srcGLRect.fWidth, |
| 3380 | srcY1, |
| 3381 | dstGLRect.fLeft, |
| 3382 | dstGLRect.fBottom, |
| 3383 | dstGLRect.fLeft + dstGLRect.fWidth, |
| 3384 | dstGLRect.fBottom + dstGLRect.fHeight, |
| 3385 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3386 | this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst); |
| 3387 | this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3388 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3391 | void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) { |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 3392 | SkASSERT(type); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3393 | switch (type) { |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3394 | case kTexture_GrXferBarrierType: { |
| 3395 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt); |
| 3396 | if (glrt->textureFBOID() != glrt->renderFBOID()) { |
| 3397 | // The render target uses separate storage so no need for glTextureBarrier. |
| 3398 | // FIXME: The render target will resolve automatically when its texture is bound, |
| 3399 | // but we could resolve only the bounds that will be read if we do it here instead. |
| 3400 | return; |
| 3401 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3402 | SkASSERT(this->caps()->textureBarrierSupport()); |
| 3403 | GL_CALL(TextureBarrier()); |
| 3404 | return; |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3405 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 3406 | case kBlend_GrXferBarrierType: |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 3407 | SkASSERT(GrCaps::kAdvanced_BlendEquationSupport == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 3408 | this->caps()->blendEquationSupport()); |
| 3409 | GL_CALL(BlendBarrier()); |
| 3410 | return; |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 3411 | default: break; // placate compiler warnings that kNone not handled |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
jvanverth | 8895792 | 2015-07-14 11:02:52 -0700 | [diff] [blame] | 3415 | GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h, |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3416 | GrPixelConfig config) const { |
bsalomon | 926cb02 | 2015-12-17 18:15:11 -0800 | [diff] [blame] | 3417 | if (!this->caps()->isConfigTexturable(config)) { |
| 3418 | return false; |
| 3419 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3420 | GrGLTextureInfo* info = new GrGLTextureInfo; |
| 3421 | info->fTarget = GR_GL_TEXTURE_2D; |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 3422 | info->fID = 0; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3423 | GL_CALL(GenTextures(1, &info->fID)); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3424 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0)); |
| 3425 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3426 | GL_CALL(BindTexture(info->fTarget, info->fID)); |
| 3427 | GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); |
| 3428 | GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 3429 | GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); |
| 3430 | 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] | 3431 | |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 3432 | GrGLenum internalFormat; |
| 3433 | GrGLenum externalFormat; |
| 3434 | GrGLenum externalType; |
| 3435 | |
| 3436 | if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat, |
| 3437 | &externalType)) { |
| 3438 | delete info; |
| 3439 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
| 3440 | return 0; |
| 3441 | #else |
| 3442 | return reinterpret_cast<GrBackendObject>(nullptr); |
| 3443 | #endif |
| 3444 | } |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3445 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3446 | GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat, |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3447 | externalType, pixels)); |
| 3448 | |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3449 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
| 3450 | GrGLuint id = info->fID; |
| 3451 | delete info; |
| 3452 | return id; |
| 3453 | #else |
| 3454 | return reinterpret_cast<GrBackendObject>(info); |
| 3455 | #endif |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3456 | } |
| 3457 | |
jvanverth | 8895792 | 2015-07-14 11:02:52 -0700 | [diff] [blame] | 3458 | bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3459 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3460 | GrGLuint texID = (GrGLuint)id; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3461 | #else |
| 3462 | GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID; |
| 3463 | #endif |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3464 | |
| 3465 | GrGLboolean result; |
| 3466 | GL_CALL_RET(result, IsTexture(texID)); |
| 3467 | |
| 3468 | return (GR_GL_TRUE == result); |
| 3469 | } |
| 3470 | |
bsalomon | 67d7620 | 2015-11-11 12:40:42 -0800 | [diff] [blame] | 3471 | void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) const { |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3472 | #ifdef SK_IGNORE_GL_TEXTURE_TARGET |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3473 | GrGLuint texID = (GrGLuint)id; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3474 | #else |
| 3475 | const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id); |
| 3476 | GrGLuint texID = info->fID; |
| 3477 | #endif |
| 3478 | |
bsalomon | 67d7620 | 2015-11-11 12:40:42 -0800 | [diff] [blame] | 3479 | if (!abandonTexture) { |
| 3480 | GL_CALL(DeleteTextures(1, &texID)); |
| 3481 | } |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 3482 | |
| 3483 | #ifndef SK_IGNORE_GL_TEXTURE_TARGET |
| 3484 | delete info; |
| 3485 | #endif |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 3488 | void GrGLGpu::resetShaderCacheForTesting() const { |
| 3489 | fProgramCache->abandon(); |
| 3490 | } |
| 3491 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3492 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 3493 | GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw( |
| 3494 | GrGLGpu* gpu, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3495 | const GrGLVertexBuffer* vbuffer, |
| 3496 | const GrGLIndexBuffer* ibuffer) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3497 | SkASSERT(vbuffer); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3498 | GrGLuint vbufferID = vbuffer->bufferID(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3499 | GrGLuint* ibufferIDPtr = nullptr; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3500 | GrGLuint ibufferID; |
| 3501 | if (ibuffer) { |
| 3502 | ibufferID = ibuffer->bufferID(); |
| 3503 | ibufferIDPtr = &ibufferID; |
| 3504 | } |
| 3505 | return this->internalBind(gpu, vbufferID, ibufferIDPtr); |
| 3506 | } |
| 3507 | |
| 3508 | GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu, |
| 3509 | GrGLuint vbufferID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3510 | return this->internalBind(gpu, vbufferID, nullptr); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu, |
| 3514 | GrGLuint vbufferID, |
| 3515 | GrGLuint ibufferID) { |
| 3516 | return this->internalBind(gpu, vbufferID, &ibufferID); |
| 3517 | } |
| 3518 | |
| 3519 | GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu, |
| 3520 | GrGLuint vbufferID, |
| 3521 | GrGLuint* ibufferID) { |
robertphillips@google.com | 4f65a27 | 2013-03-26 19:40:46 +0000 | [diff] [blame] | 3522 | GrGLAttribArrayState* attribState; |
| 3523 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3524 | if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) { |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 3525 | if (!fVBOVertexArray) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3526 | GrGLuint arrayID; |
| 3527 | GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID)); |
| 3528 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 3529 | fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount); |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3530 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3531 | if (ibufferID) { |
| 3532 | attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID); |
| 3533 | } else { |
| 3534 | attribState = fVBOVertexArray->bind(gpu); |
| 3535 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3536 | } else { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3537 | if (ibufferID) { |
| 3538 | this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3539 | } else { |
| 3540 | this->setVertexArrayID(gpu, 0); |
| 3541 | } |
| 3542 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
| 3543 | if (fDefaultVertexArrayAttribState.count() != attrCount) { |
| 3544 | fDefaultVertexArrayAttribState.resize(attrCount); |
| 3545 | } |
| 3546 | attribState = &fDefaultVertexArrayAttribState; |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3547 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3548 | return attribState; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 3549 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 3550 | |
| 3551 | bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams, |
| 3552 | GrTextureProducer::CopyParams* copyParams) const { |
| 3553 | if (textureParams.isTiled() || |
| 3554 | GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) { |
| 3555 | GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture); |
| 3556 | if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() || |
| 3557 | GR_GL_TEXTURE_RECTANGLE == glTexture->target()) { |
| 3558 | copyParams->fFilter = GrTextureParams::kNone_FilterMode; |
| 3559 | copyParams->fWidth = texture->width(); |
| 3560 | copyParams->fHeight = texture->height(); |
| 3561 | return true; |
| 3562 | } |
| 3563 | } |
| 3564 | return false; |
| 3565 | } |