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