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