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