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