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