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