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