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