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