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