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