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