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