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