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