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