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