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