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