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