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