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