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