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