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