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