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