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