senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkLightingImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkColorPriv.h" |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 11 | #include "SkDevice.h" |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 12 | #include "SkPoint3.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
tomhudson@google.com | 300f562 | 2012-07-20 14:15:22 +0000 | [diff] [blame] | 14 | #include "SkTypes.h" |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 16 | |
| 17 | #if SK_SUPPORT_GPU |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 18 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 19 | #include "GrDrawContext.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 20 | #include "GrFragmentProcessor.h" |
| 21 | #include "GrInvariantOutput.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 22 | #include "GrPaint.h" |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 23 | #include "SkGr.h" |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 24 | #include "effects/GrSingleTextureEffect.h" |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 25 | #include "effects/GrTextureDomain.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 26 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 28 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 29 | #include "glsl/GrGLSLUniformHandler.h" |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 30 | |
| 31 | class GrGLDiffuseLightingEffect; |
| 32 | class GrGLSpecularLightingEffect; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 34 | // For brevity |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 35 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 36 | #endif |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 37 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 40 | const SkScalar gOneThird = SkIntToScalar(1) / 3; |
| 41 | const SkScalar gTwoThirds = SkIntToScalar(2) / 3; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 42 | const SkScalar gOneHalf = 0.5f; |
| 43 | const SkScalar gOneQuarter = 0.25f; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 45 | #if SK_SUPPORT_GPU |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 46 | void setUniformPoint3(const GrGLSLProgramDataManager& pdman, UniformHandle uni, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 47 | const SkPoint3& point) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 48 | GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(float)); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 49 | pdman.set3fv(uni, 1, &point.fX); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 52 | void setUniformNormal3(const GrGLSLProgramDataManager& pdman, UniformHandle uni, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | const SkPoint3& point) { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 54 | setUniformPoint3(pdman, uni, point); |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 55 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 56 | #endif |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 57 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 58 | // Shift matrix components to the left, as we advance pixels to the right. |
| 59 | inline void shiftMatrixLeft(int m[9]) { |
| 60 | m[0] = m[1]; |
| 61 | m[3] = m[4]; |
| 62 | m[6] = m[7]; |
| 63 | m[1] = m[2]; |
| 64 | m[4] = m[5]; |
| 65 | m[7] = m[8]; |
| 66 | } |
| 67 | |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 68 | static inline void fast_normalize(SkPoint3* vector) { |
| 69 | // add a tiny bit so we don't have to worry about divide-by-zero |
| 70 | SkScalar magSq = vector->dot(*vector) + SK_ScalarNearlyZero; |
| 71 | SkScalar scale = sk_float_rsqrt(magSq); |
| 72 | vector->fX *= scale; |
| 73 | vector->fY *= scale; |
| 74 | vector->fZ *= scale; |
| 75 | } |
| 76 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 77 | class DiffuseLightingType { |
| 78 | public: |
| 79 | DiffuseLightingType(SkScalar kd) |
| 80 | : fKD(kd) {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 81 | SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, |
| 82 | const SkPoint3& lightColor) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 83 | SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); |
| 84 | colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 85 | SkPoint3 color = lightColor.makeScale(colorScale); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 86 | return SkPackARGB32(255, |
senorblanco@chromium.org | b9c9597 | 2014-03-19 19:44:41 +0000 | [diff] [blame] | 87 | SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 88 | SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 89 | SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 90 | } |
| 91 | private: |
| 92 | SkScalar fKD; |
| 93 | }; |
| 94 | |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 95 | static SkScalar max_component(const SkPoint3& p) { |
| 96 | return p.x() > p.y() ? (p.x() > p.z() ? p.x() : p.z()) : (p.y() > p.z() ? p.y() : p.z()); |
| 97 | } |
| 98 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 99 | class SpecularLightingType { |
| 100 | public: |
| 101 | SpecularLightingType(SkScalar ks, SkScalar shininess) |
| 102 | : fKS(ks), fShininess(shininess) {} |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, |
| 104 | const SkPoint3& lightColor) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 105 | SkPoint3 halfDir(surfaceTolight); |
| 106 | halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 107 | fast_normalize(&halfDir); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 108 | SkScalar colorScale = SkScalarMul(fKS, |
| 109 | SkScalarPow(normal.dot(halfDir), fShininess)); |
| 110 | colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 111 | SkPoint3 color = lightColor.makeScale(colorScale); |
| 112 | return SkPackARGB32(SkClampMax(SkScalarRoundToInt(max_component(color)), 255), |
senorblanco@chromium.org | b9c9597 | 2014-03-19 19:44:41 +0000 | [diff] [blame] | 113 | SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 114 | SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 115 | SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 116 | } |
| 117 | private: |
| 118 | SkScalar fKS; |
| 119 | SkScalar fShininess; |
| 120 | }; |
| 121 | |
| 122 | inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale) { |
| 123 | return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); |
| 124 | } |
| 125 | |
| 126 | inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 127 | SkPoint3 vector = SkPoint3::Make(SkScalarMul(-x, surfaceScale), |
| 128 | SkScalarMul(-y, surfaceScale), |
| 129 | SK_Scalar1); |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 130 | fast_normalize(&vector); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 131 | return vector; |
| 132 | } |
| 133 | |
| 134 | inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) { |
| 135 | return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds), |
| 136 | sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds), |
| 137 | surfaceScale); |
| 138 | } |
| 139 | |
| 140 | inline SkPoint3 topNormal(int m[9], SkScalar surfaceScale) { |
| 141 | return pointToNormal(sobel( 0, 0, m[3], m[5], m[6], m[8], gOneThird), |
| 142 | sobel(m[3], m[6], m[4], m[7], m[5], m[8], gOneHalf), |
| 143 | surfaceScale); |
| 144 | } |
| 145 | |
| 146 | inline SkPoint3 topRightNormal(int m[9], SkScalar surfaceScale) { |
| 147 | return pointToNormal(sobel( 0, 0, m[3], m[4], m[6], m[7], gTwoThirds), |
| 148 | sobel(m[3], m[6], m[4], m[7], 0, 0, gTwoThirds), |
| 149 | surfaceScale); |
| 150 | } |
| 151 | |
| 152 | inline SkPoint3 leftNormal(int m[9], SkScalar surfaceScale) { |
| 153 | return pointToNormal(sobel(m[1], m[2], m[4], m[5], m[7], m[8], gOneHalf), |
| 154 | sobel( 0, 0, m[1], m[7], m[2], m[8], gOneThird), |
| 155 | surfaceScale); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | inline SkPoint3 interiorNormal(int m[9], SkScalar surfaceScale) { |
| 160 | return pointToNormal(sobel(m[0], m[2], m[3], m[5], m[6], m[8], gOneQuarter), |
| 161 | sobel(m[0], m[6], m[1], m[7], m[2], m[8], gOneQuarter), |
| 162 | surfaceScale); |
| 163 | } |
| 164 | |
| 165 | inline SkPoint3 rightNormal(int m[9], SkScalar surfaceScale) { |
| 166 | return pointToNormal(sobel(m[0], m[1], m[3], m[4], m[6], m[7], gOneHalf), |
| 167 | sobel(m[0], m[6], m[1], m[7], 0, 0, gOneThird), |
| 168 | surfaceScale); |
| 169 | } |
| 170 | |
| 171 | inline SkPoint3 bottomLeftNormal(int m[9], SkScalar surfaceScale) { |
| 172 | return pointToNormal(sobel(m[1], m[2], m[4], m[5], 0, 0, gTwoThirds), |
| 173 | sobel( 0, 0, m[1], m[4], m[2], m[5], gTwoThirds), |
| 174 | surfaceScale); |
| 175 | } |
| 176 | |
| 177 | inline SkPoint3 bottomNormal(int m[9], SkScalar surfaceScale) { |
| 178 | return pointToNormal(sobel(m[0], m[2], m[3], m[5], 0, 0, gOneThird), |
| 179 | sobel(m[0], m[3], m[1], m[4], m[2], m[5], gOneHalf), |
| 180 | surfaceScale); |
| 181 | } |
| 182 | |
| 183 | inline SkPoint3 bottomRightNormal(int m[9], SkScalar surfaceScale) { |
| 184 | return pointToNormal(sobel(m[0], m[1], m[3], m[4], 0, 0, gTwoThirds), |
| 185 | sobel(m[0], m[3], m[1], m[4], 0, 0, gTwoThirds), |
| 186 | surfaceScale); |
| 187 | } |
| 188 | |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 189 | |
| 190 | class UncheckedPixelFetcher { |
| 191 | public: |
| 192 | static inline uint32_t Fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 193 | return SkGetPackedA32(*src.getAddr32(x, y)); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | // The DecalPixelFetcher is used when the destination crop rect exceeds the input bitmap bounds. |
| 198 | class DecalPixelFetcher { |
| 199 | public: |
| 200 | static inline uint32_t Fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) { |
| 201 | if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) { |
| 202 | return 0; |
| 203 | } else { |
| 204 | return SkGetPackedA32(*src.getAddr32(x, y)); |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | template <class LightingType, class LightType, class PixelFetcher> |
| 210 | void lightBitmap(const LightingType& lightingType, |
| 211 | const SkImageFilterLight* light, |
| 212 | const SkBitmap& src, |
| 213 | SkBitmap* dst, |
| 214 | SkScalar surfaceScale, |
| 215 | const SkIRect& bounds) { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 216 | SkASSERT(dst->width() == bounds.width() && dst->height() == bounds.height()); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 217 | const LightType* l = static_cast<const LightType*>(light); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 218 | int left = bounds.left(), right = bounds.right(); |
| 219 | int bottom = bounds.bottom(); |
| 220 | int y = bounds.top(); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 221 | SkIRect srcBounds = src.bounds(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 222 | SkPMColor* dptr = dst->getAddr32(0, 0); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 223 | { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 224 | int x = left; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 225 | int m[9]; |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 226 | m[4] = PixelFetcher::Fetch(src, x, y, srcBounds); |
| 227 | m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds); |
| 228 | m[7] = PixelFetcher::Fetch(src, x, y + 1, srcBounds); |
| 229 | m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 230 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 231 | *dptr++ = lightingType.light(topLeftNormal(m, surfaceScale), surfaceToLight, |
| 232 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 233 | for (++x; x < right - 1; ++x) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 234 | { |
| 235 | shiftMatrixLeft(m); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 236 | m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds); |
| 237 | m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 238 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 239 | *dptr++ = lightingType.light(topNormal(m, surfaceScale), surfaceToLight, |
| 240 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 241 | } |
| 242 | shiftMatrixLeft(m); |
| 243 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 244 | *dptr++ = lightingType.light(topRightNormal(m, surfaceScale), surfaceToLight, |
| 245 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 246 | } |
| 247 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 248 | for (++y; y < bottom - 1; ++y) { |
| 249 | int x = left; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 250 | int m[9]; |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 251 | m[1] = PixelFetcher::Fetch(src, x, y - 1, srcBounds); |
| 252 | m[2] = PixelFetcher::Fetch(src, x + 1, y - 1, srcBounds); |
| 253 | m[4] = PixelFetcher::Fetch(src, x, y, srcBounds); |
| 254 | m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds); |
| 255 | m[7] = PixelFetcher::Fetch(src, x, y + 1, srcBounds); |
| 256 | m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 257 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 258 | *dptr++ = lightingType.light(leftNormal(m, surfaceScale), surfaceToLight, |
| 259 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 260 | for (++x; x < right - 1; ++x) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 261 | shiftMatrixLeft(m); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 262 | m[2] = PixelFetcher::Fetch(src, x + 1, y - 1, srcBounds); |
| 263 | m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds); |
| 264 | m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 265 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 266 | *dptr++ = lightingType.light(interiorNormal(m, surfaceScale), surfaceToLight, |
| 267 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 268 | } |
| 269 | shiftMatrixLeft(m); |
| 270 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 271 | *dptr++ = lightingType.light(rightNormal(m, surfaceScale), surfaceToLight, |
| 272 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 276 | int x = left; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 277 | int m[9]; |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 278 | m[1] = PixelFetcher::Fetch(src, x, bottom - 2, srcBounds); |
| 279 | m[2] = PixelFetcher::Fetch(src, x + 1, bottom - 2, srcBounds); |
| 280 | m[4] = PixelFetcher::Fetch(src, x, bottom - 1, srcBounds); |
| 281 | m[5] = PixelFetcher::Fetch(src, x + 1, bottom - 1, srcBounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 282 | SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | *dptr++ = lightingType.light(bottomLeftNormal(m, surfaceScale), surfaceToLight, |
| 284 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 285 | for (++x; x < right - 1; ++x) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 286 | { |
| 287 | shiftMatrixLeft(m); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 288 | m[2] = PixelFetcher::Fetch(src, x + 1, bottom - 2, srcBounds); |
| 289 | m[5] = PixelFetcher::Fetch(src, x + 1, bottom - 1, srcBounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 290 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 291 | *dptr++ = lightingType.light(bottomNormal(m, surfaceScale), surfaceToLight, |
| 292 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 293 | } |
| 294 | shiftMatrixLeft(m); |
| 295 | surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 296 | *dptr++ = lightingType.light(bottomRightNormal(m, surfaceScale), surfaceToLight, |
| 297 | l->lightColor(surfaceToLight)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 301 | template <class LightingType, class LightType> |
| 302 | void lightBitmap(const LightingType& lightingType, |
| 303 | const SkImageFilterLight* light, |
| 304 | const SkBitmap& src, |
| 305 | SkBitmap* dst, |
| 306 | SkScalar surfaceScale, |
| 307 | const SkIRect& bounds) { |
| 308 | if (src.bounds().contains(bounds)) { |
| 309 | lightBitmap<LightingType, LightType, UncheckedPixelFetcher>( |
| 310 | lightingType, light, src, dst, surfaceScale, bounds); |
| 311 | } else { |
| 312 | lightBitmap<LightingType, LightType, DecalPixelFetcher>( |
| 313 | lightingType, light, src, dst, surfaceScale, bounds); |
| 314 | } |
| 315 | } |
| 316 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 317 | SkPoint3 readPoint3(SkReadBuffer& buffer) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 318 | SkPoint3 point; |
| 319 | point.fX = buffer.readScalar(); |
| 320 | point.fY = buffer.readScalar(); |
| 321 | point.fZ = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 322 | buffer.validate(SkScalarIsFinite(point.fX) && |
| 323 | SkScalarIsFinite(point.fY) && |
| 324 | SkScalarIsFinite(point.fZ)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 325 | return point; |
| 326 | }; |
| 327 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 328 | void writePoint3(const SkPoint3& point, SkWriteBuffer& buffer) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 329 | buffer.writeScalar(point.fX); |
| 330 | buffer.writeScalar(point.fY); |
| 331 | buffer.writeScalar(point.fZ); |
| 332 | }; |
| 333 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 334 | enum BoundaryMode { |
| 335 | kTopLeft_BoundaryMode, |
| 336 | kTop_BoundaryMode, |
| 337 | kTopRight_BoundaryMode, |
| 338 | kLeft_BoundaryMode, |
| 339 | kInterior_BoundaryMode, |
| 340 | kRight_BoundaryMode, |
| 341 | kBottomLeft_BoundaryMode, |
| 342 | kBottom_BoundaryMode, |
| 343 | kBottomRight_BoundaryMode, |
| 344 | |
| 345 | kBoundaryModeCount, |
| 346 | }; |
| 347 | |
| 348 | class SkLightingImageFilterInternal : public SkLightingImageFilter { |
| 349 | protected: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 350 | SkLightingImageFilterInternal(SkImageFilterLight* light, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 351 | SkScalar surfaceScale, |
| 352 | SkImageFilter* input, |
| 353 | const CropRect* cropRect) |
| 354 | : INHERITED(light, surfaceScale, input, cropRect) {} |
| 355 | |
| 356 | #if SK_SUPPORT_GPU |
| 357 | bool canFilterImageGPU() const override { return true; } |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 358 | bool filterImageGPUDeprecated(Proxy*, const SkBitmap& src, const Context&, |
| 359 | SkBitmap* result, SkIPoint* offset) const override; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 360 | virtual GrFragmentProcessor* getFragmentProcessor(GrTexture*, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 361 | const SkMatrix&, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 362 | const SkIRect* srcBounds, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 363 | BoundaryMode boundaryMode) const = 0; |
| 364 | #endif |
| 365 | private: |
| 366 | #if SK_SUPPORT_GPU |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 367 | void drawRect(GrDrawContext* drawContext, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 368 | GrTexture* src, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 369 | const SkMatrix& matrix, |
| 370 | const GrClip& clip, |
| 371 | const SkRect& dstRect, |
| 372 | BoundaryMode boundaryMode, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 373 | const SkIRect* srcBounds, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 374 | const SkIRect& bounds) const; |
| 375 | #endif |
| 376 | typedef SkLightingImageFilter INHERITED; |
| 377 | }; |
| 378 | |
| 379 | #if SK_SUPPORT_GPU |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 380 | void SkLightingImageFilterInternal::drawRect(GrDrawContext* drawContext, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 381 | GrTexture* src, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 382 | const SkMatrix& matrix, |
| 383 | const GrClip& clip, |
| 384 | const SkRect& dstRect, |
| 385 | BoundaryMode boundaryMode, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 386 | const SkIRect* srcBounds, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 387 | const SkIRect& bounds) const { |
| 388 | SkRect srcRect = dstRect.makeOffset(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y())); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 389 | GrPaint paint; |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 390 | GrFragmentProcessor* fp = this->getFragmentProcessor(src, matrix, srcBounds, boundaryMode); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 391 | paint.addColorFragmentProcessor(fp)->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 392 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 393 | drawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 394 | } |
| 395 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 396 | bool SkLightingImageFilterInternal::filterImageGPUDeprecated(Proxy* proxy, |
| 397 | const SkBitmap& src, |
| 398 | const Context& ctx, |
| 399 | SkBitmap* result, |
| 400 | SkIPoint* offset) const { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 401 | SkBitmap input = src; |
| 402 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 403 | if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 404 | return false; |
| 405 | } |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 406 | SkIRect srcBounds = input.bounds(); |
| 407 | srcBounds.offset(srcOffset); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 408 | SkIRect bounds; |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 409 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 410 | return false; |
| 411 | } |
| 412 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bounds.width()), |
| 413 | SkIntToScalar(bounds.height())); |
| 414 | GrTexture* srcTexture = input.getTexture(); |
| 415 | GrContext* context = srcTexture->getContext(); |
| 416 | |
| 417 | GrSurfaceDesc desc; |
| 418 | desc.fFlags = kRenderTarget_GrSurfaceFlag, |
| 419 | desc.fWidth = bounds.width(); |
| 420 | desc.fHeight = bounds.height(); |
| 421 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 422 | |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 423 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 424 | if (!dst) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | // setup new clip |
| 429 | GrClip clip(dstRect); |
| 430 | |
| 431 | offset->fX = bounds.left(); |
| 432 | offset->fY = bounds.top(); |
| 433 | SkMatrix matrix(ctx.ctm()); |
| 434 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
| 435 | bounds.offset(-srcOffset); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 436 | srcBounds.offset(-srcOffset); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 437 | SkRect topLeft = SkRect::MakeXYWH(0, 0, 1, 1); |
| 438 | SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1); |
| 439 | SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1); |
| 440 | SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2); |
| 441 | SkRect interior = dstRect.makeInset(1, 1); |
| 442 | SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2); |
| 443 | SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1); |
| 444 | SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1); |
| 445 | SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 446 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 447 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 448 | if (!drawContext) { |
| 449 | return false; |
| 450 | } |
| 451 | |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 452 | const SkIRect* pSrcBounds = srcBounds.contains(bounds) ? nullptr : &srcBounds; |
| 453 | this->drawRect(drawContext, srcTexture, matrix, clip, topLeft, kTopLeft_BoundaryMode, |
| 454 | pSrcBounds, bounds); |
| 455 | this->drawRect(drawContext, srcTexture, matrix, clip, top, kTop_BoundaryMode, |
| 456 | pSrcBounds, bounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 457 | this->drawRect(drawContext, srcTexture, matrix, clip, topRight, kTopRight_BoundaryMode, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 458 | pSrcBounds, bounds); |
| 459 | this->drawRect(drawContext, srcTexture, matrix, clip, left, kLeft_BoundaryMode, |
| 460 | pSrcBounds, bounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 461 | this->drawRect(drawContext, srcTexture, matrix, clip, interior, kInterior_BoundaryMode, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 462 | pSrcBounds, bounds); |
| 463 | this->drawRect(drawContext, srcTexture, matrix, clip, right, kRight_BoundaryMode, |
| 464 | pSrcBounds, bounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 465 | this->drawRect(drawContext, srcTexture, matrix, clip, bottomLeft, kBottomLeft_BoundaryMode, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 466 | pSrcBounds, bounds); |
| 467 | this->drawRect(drawContext, srcTexture, matrix, clip, bottom, kBottom_BoundaryMode, |
| 468 | pSrcBounds, bounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 469 | this->drawRect(drawContext, srcTexture, matrix, clip, bottomRight, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 470 | kBottomRight_BoundaryMode, pSrcBounds, bounds); |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 471 | GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | #endif |
| 475 | |
| 476 | class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 477 | public: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 478 | static SkImageFilter* Create(SkImageFilterLight* light, SkScalar surfaceScale, |
| 479 | SkScalar kd, SkImageFilter*, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 480 | const CropRect*); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 481 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 482 | SK_TO_STRING_OVERRIDE() |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 483 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFilter) |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 484 | SkScalar kd() const { return fKD; } |
| 485 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 486 | protected: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 487 | SkDiffuseLightingImageFilter(SkImageFilterLight* light, SkScalar surfaceScale, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 488 | SkScalar kd, SkImageFilter* input, const CropRect* cropRect); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 489 | void flatten(SkWriteBuffer& buffer) const override; |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 490 | bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, |
| 491 | SkBitmap* result, SkIPoint* offset) const override; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 492 | #if SK_SUPPORT_GPU |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 493 | GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 494 | BoundaryMode) const override; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 495 | #endif |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 496 | |
| 497 | private: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 498 | friend class SkLightingImageFilter; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 499 | typedef SkLightingImageFilterInternal INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 500 | SkScalar fKD; |
| 501 | }; |
| 502 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 503 | class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 504 | public: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 505 | static SkImageFilter* Create(SkImageFilterLight* light, SkScalar surfaceScale, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 506 | SkScalar ks, SkScalar shininess, SkImageFilter*, const CropRect*); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 507 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 508 | SK_TO_STRING_OVERRIDE() |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 509 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageFilter) |
| 510 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 511 | SkScalar ks() const { return fKS; } |
| 512 | SkScalar shininess() const { return fShininess; } |
| 513 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 514 | protected: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 515 | SkSpecularLightingImageFilter(SkImageFilterLight* light, SkScalar surfaceScale, SkScalar ks, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 516 | SkScalar shininess, SkImageFilter* input, const CropRect*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 517 | void flatten(SkWriteBuffer& buffer) const override; |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 518 | bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, |
| 519 | SkBitmap* result, SkIPoint* offset) const override; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 520 | #if SK_SUPPORT_GPU |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 521 | GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 522 | BoundaryMode) const override; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 523 | #endif |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 524 | |
| 525 | private: |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 526 | SkScalar fKS; |
| 527 | SkScalar fShininess; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 528 | friend class SkLightingImageFilter; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 529 | typedef SkLightingImageFilterInternal INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 532 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 533 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 534 | class GrLightingEffect : public GrSingleTextureEffect { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 535 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 536 | GrLightingEffect(GrTexture* texture, const SkImageFilterLight* light, SkScalar surfaceScale, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 537 | const SkMatrix& matrix, BoundaryMode boundaryMode, const SkIRect* srcBounds); |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 538 | ~GrLightingEffect() override; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 539 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 540 | const SkImageFilterLight* light() const { return fLight; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 541 | SkScalar surfaceScale() const { return fSurfaceScale; } |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 542 | const SkMatrix& filterMatrix() const { return fFilterMatrix; } |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 543 | BoundaryMode boundaryMode() const { return fBoundaryMode; } |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 544 | const GrTextureDomain& domain() const { return fDomain; } |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 545 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 546 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 547 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 548 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 549 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 550 | // lighting shaders are complicated. We just throw up our hands. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 551 | inout->mulByUnknownFourComponents(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 552 | } |
| 553 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 554 | private: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 555 | const SkImageFilterLight* fLight; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 556 | SkScalar fSurfaceScale; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 557 | SkMatrix fFilterMatrix; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 558 | BoundaryMode fBoundaryMode; |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 559 | GrTextureDomain fDomain; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 560 | |
| 561 | typedef GrSingleTextureEffect INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 564 | class GrDiffuseLightingEffect : public GrLightingEffect { |
| 565 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 566 | static GrFragmentProcessor* Create(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 567 | const SkImageFilterLight* light, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 568 | SkScalar surfaceScale, |
| 569 | const SkMatrix& matrix, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 570 | SkScalar kd, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 571 | BoundaryMode boundaryMode, |
| 572 | const SkIRect* srcBounds) { |
| 573 | return new GrDiffuseLightingEffect(texture, light, surfaceScale, matrix, kd, boundaryMode, |
| 574 | srcBounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 575 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 576 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 577 | const char* name() const override { return "DiffuseLighting"; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 578 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 579 | SkScalar kd() const { return fKD; } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 580 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 581 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 582 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 583 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 584 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 585 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 586 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 587 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 588 | GrDiffuseLightingEffect(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 589 | const SkImageFilterLight* light, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 590 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 591 | const SkMatrix& matrix, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 592 | SkScalar kd, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 593 | BoundaryMode boundaryMode, |
| 594 | const SkIRect* srcBounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 595 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 596 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 597 | typedef GrLightingEffect INHERITED; |
| 598 | SkScalar fKD; |
| 599 | }; |
| 600 | |
| 601 | class GrSpecularLightingEffect : public GrLightingEffect { |
| 602 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 603 | static GrFragmentProcessor* Create(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 604 | const SkImageFilterLight* light, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 605 | SkScalar surfaceScale, |
| 606 | const SkMatrix& matrix, |
| 607 | SkScalar ks, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 608 | SkScalar shininess, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 609 | BoundaryMode boundaryMode, |
| 610 | const SkIRect* srcBounds) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 611 | return new GrSpecularLightingEffect(texture, light, surfaceScale, matrix, ks, shininess, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 612 | boundaryMode, srcBounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 613 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 614 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 615 | const char* name() const override { return "SpecularLighting"; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 616 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 617 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 618 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 619 | SkScalar ks() const { return fKS; } |
| 620 | SkScalar shininess() const { return fShininess; } |
| 621 | |
| 622 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 623 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 624 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 625 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 626 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 627 | GrSpecularLightingEffect(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 628 | const SkImageFilterLight* light, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 629 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 630 | const SkMatrix& matrix, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 631 | SkScalar ks, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 632 | SkScalar shininess, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 633 | BoundaryMode boundaryMode, |
| 634 | const SkIRect* srcBounds); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 635 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 636 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 637 | typedef GrLightingEffect INHERITED; |
| 638 | SkScalar fKS; |
| 639 | SkScalar fShininess; |
| 640 | }; |
| 641 | |
| 642 | /////////////////////////////////////////////////////////////////////////////// |
| 643 | |
| 644 | class GrGLLight { |
| 645 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 646 | virtual ~GrGLLight() {} |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 647 | |
| 648 | /** |
| 649 | * This is called by GrGLLightingEffect::emitCode() before either of the two virtual functions |
| 650 | * below. It adds a vec3f uniform visible in the FS that represents the constant light color. |
| 651 | */ |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 652 | void emitLightColorUniform(GrGLSLUniformHandler*); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 653 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 654 | /** |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 655 | * These two functions are called from GrGLLightingEffect's emitCode() function. |
| 656 | * emitSurfaceToLight places an expression in param out that is the vector from the surface to |
| 657 | * the light. The expression will be used in the FS. emitLightColor writes an expression into |
| 658 | * the FS that is the color of the light. Either function may add functions and/or uniforms to |
| 659 | * the FS. The default of emitLightColor appends the name of the constant light color uniform |
| 660 | * and so this function only needs to be overridden if the light color varies spatially. |
| 661 | */ |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 662 | virtual void emitSurfaceToLight(GrGLSLUniformHandler*, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 663 | GrGLSLFPFragmentBuilder*, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 664 | const char* z) = 0; |
| 665 | virtual void emitLightColor(GrGLSLUniformHandler*, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 666 | GrGLSLFPFragmentBuilder*, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 667 | const char *surfaceToLight); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 668 | |
| 669 | // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLight must call |
| 670 | // INHERITED::setData(). |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 671 | virtual void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 672 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 673 | protected: |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 674 | /** |
| 675 | * Gets the constant light color uniform. Subclasses can use this in their emitLightColor |
| 676 | * function. |
| 677 | */ |
| 678 | UniformHandle lightColorUni() const { return fColorUni; } |
| 679 | |
| 680 | private: |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 681 | UniformHandle fColorUni; |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 682 | |
| 683 | typedef SkRefCnt INHERITED; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | /////////////////////////////////////////////////////////////////////////////// |
| 687 | |
| 688 | class GrGLDistantLight : public GrGLLight { |
| 689 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 690 | virtual ~GrGLDistantLight() {} |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 691 | void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 692 | void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 693 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 694 | private: |
| 695 | typedef GrGLLight INHERITED; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 696 | UniformHandle fDirectionUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | /////////////////////////////////////////////////////////////////////////////// |
| 700 | |
| 701 | class GrGLPointLight : public GrGLLight { |
| 702 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 703 | virtual ~GrGLPointLight() {} |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 704 | void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 705 | void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 706 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 707 | private: |
| 708 | typedef GrGLLight INHERITED; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 709 | UniformHandle fLocationUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | /////////////////////////////////////////////////////////////////////////////// |
| 713 | |
| 714 | class GrGLSpotLight : public GrGLLight { |
| 715 | public: |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 716 | virtual ~GrGLSpotLight() {} |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 717 | void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 718 | void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 719 | void emitLightColor(GrGLSLUniformHandler*, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 720 | GrGLSLFPFragmentBuilder*, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 721 | const char *surfaceToLight) override; |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 722 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 723 | private: |
| 724 | typedef GrGLLight INHERITED; |
| 725 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 726 | SkString fLightColorFunc; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 727 | UniformHandle fLocationUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 728 | UniformHandle fExponentUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 729 | UniformHandle fCosOuterConeAngleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 730 | UniformHandle fCosInnerConeAngleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 731 | UniformHandle fConeScaleUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 732 | UniformHandle fSUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 733 | }; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 734 | #else |
| 735 | |
| 736 | class GrGLLight; |
| 737 | |
| 738 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 739 | |
| 740 | }; |
| 741 | |
| 742 | /////////////////////////////////////////////////////////////////////////////// |
| 743 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 744 | class SkImageFilterLight : public SkRefCnt { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 745 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 746 | |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 747 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 748 | enum LightType { |
| 749 | kDistant_LightType, |
| 750 | kPoint_LightType, |
| 751 | kSpot_LightType, |
| 752 | }; |
| 753 | virtual LightType type() const = 0; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 754 | const SkPoint3& color() const { return fColor; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 755 | virtual GrGLLight* createGLLight() const = 0; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 756 | virtual bool isEqual(const SkImageFilterLight& other) const { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 757 | return fColor == other.fColor; |
| 758 | } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 759 | // Called to know whether the generated GrGLLight will require access to the fragment position. |
| 760 | virtual bool requiresFragmentPosition() const = 0; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 761 | virtual SkImageFilterLight* transform(const SkMatrix& matrix) const = 0; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 762 | |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 763 | // Defined below SkLight's subclasses. |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 764 | void flattenLight(SkWriteBuffer& buffer) const; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 765 | static SkImageFilterLight* UnflattenLight(SkReadBuffer& buffer); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 766 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 767 | protected: |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 768 | SkImageFilterLight(SkColor color) { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 769 | fColor = SkPoint3::Make(SkIntToScalar(SkColorGetR(color)), |
| 770 | SkIntToScalar(SkColorGetG(color)), |
| 771 | SkIntToScalar(SkColorGetB(color))); |
| 772 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 773 | SkImageFilterLight(const SkPoint3& color) |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 774 | : fColor(color) {} |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 775 | SkImageFilterLight(SkReadBuffer& buffer) { |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 776 | fColor = readPoint3(buffer); |
| 777 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 778 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 779 | virtual void onFlattenLight(SkWriteBuffer& buffer) const = 0; |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 780 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 781 | |
| 782 | private: |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 783 | typedef SkRefCnt INHERITED; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 784 | SkPoint3 fColor; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 787 | /////////////////////////////////////////////////////////////////////////////// |
| 788 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 789 | class SkDistantLight : public SkImageFilterLight { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 790 | public: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 791 | SkDistantLight(const SkPoint3& direction, SkColor color) |
| 792 | : INHERITED(color), fDirection(direction) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 793 | } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 794 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 795 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 796 | return fDirection; |
| 797 | }; |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 798 | const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 799 | LightType type() const override { return kDistant_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 800 | const SkPoint3& direction() const { return fDirection; } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 801 | GrGLLight* createGLLight() const override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 802 | #if SK_SUPPORT_GPU |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 803 | return new GrGLDistantLight; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 804 | #else |
| 805 | SkDEBUGFAIL("Should not call in GPU-less build"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 806 | return nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 807 | #endif |
| 808 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 809 | bool requiresFragmentPosition() const override { return false; } |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 810 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 811 | bool isEqual(const SkImageFilterLight& other) const override { |
senorblanco@chromium.org | 6730cbb | 2012-07-11 16:35:00 +0000 | [diff] [blame] | 812 | if (other.type() != kDistant_LightType) { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | const SkDistantLight& o = static_cast<const SkDistantLight&>(other); |
| 817 | return INHERITED::isEqual(other) && |
| 818 | fDirection == o.fDirection; |
| 819 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 820 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 821 | SkDistantLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 822 | fDirection = readPoint3(buffer); |
| 823 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 824 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 825 | protected: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 826 | SkDistantLight(const SkPoint3& direction, const SkPoint3& color) |
| 827 | : INHERITED(color), fDirection(direction) { |
| 828 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 829 | SkImageFilterLight* transform(const SkMatrix& matrix) const override { |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 830 | return new SkDistantLight(direction(), color()); |
| 831 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 832 | void onFlattenLight(SkWriteBuffer& buffer) const override { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 833 | writePoint3(fDirection, buffer); |
| 834 | } |
| 835 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 836 | private: |
| 837 | SkPoint3 fDirection; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 838 | |
| 839 | typedef SkImageFilterLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 840 | }; |
| 841 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 842 | /////////////////////////////////////////////////////////////////////////////// |
| 843 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 844 | class SkPointLight : public SkImageFilterLight { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 845 | public: |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 846 | SkPointLight(const SkPoint3& location, SkColor color) |
| 847 | : INHERITED(color), fLocation(location) {} |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 848 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 849 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 850 | SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), |
| 851 | fLocation.fY - SkIntToScalar(y), |
| 852 | fLocation.fZ - SkScalarMul(SkIntToScalar(z), |
| 853 | surfaceScale)); |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 854 | fast_normalize(&direction); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 855 | return direction; |
| 856 | }; |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 857 | const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 858 | LightType type() const override { return kPoint_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 859 | const SkPoint3& location() const { return fLocation; } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 860 | GrGLLight* createGLLight() const override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 861 | #if SK_SUPPORT_GPU |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 862 | return new GrGLPointLight; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 863 | #else |
| 864 | SkDEBUGFAIL("Should not call in GPU-less build"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 865 | return nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 866 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 867 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 868 | bool requiresFragmentPosition() const override { return true; } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 869 | bool isEqual(const SkImageFilterLight& other) const override { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 870 | if (other.type() != kPoint_LightType) { |
| 871 | return false; |
| 872 | } |
| 873 | const SkPointLight& o = static_cast<const SkPointLight&>(other); |
| 874 | return INHERITED::isEqual(other) && |
| 875 | fLocation == o.fLocation; |
| 876 | } |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 877 | SkImageFilterLight* transform(const SkMatrix& matrix) const override { |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 878 | SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 879 | matrix.mapPoints(&location2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 880 | // Use X scale and Y scale on Z and average the result |
| 881 | SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 882 | matrix.mapVectors(&locationZ, 1); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 883 | SkPoint3 location = SkPoint3::Make(location2.fX, |
| 884 | location2.fY, |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 885 | SkScalarAve(locationZ.fX, locationZ.fY)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 886 | return new SkPointLight(location, color()); |
| 887 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 888 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 889 | SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 890 | fLocation = readPoint3(buffer); |
| 891 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 892 | |
| 893 | protected: |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 894 | SkPointLight(const SkPoint3& location, const SkPoint3& color) |
| 895 | : INHERITED(color), fLocation(location) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 896 | void onFlattenLight(SkWriteBuffer& buffer) const override { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 897 | writePoint3(fLocation, buffer); |
| 898 | } |
| 899 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 900 | private: |
| 901 | SkPoint3 fLocation; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 902 | |
| 903 | typedef SkImageFilterLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 904 | }; |
| 905 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 906 | /////////////////////////////////////////////////////////////////////////////// |
| 907 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 908 | class SkSpotLight : public SkImageFilterLight { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 909 | public: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 910 | SkSpotLight(const SkPoint3& location, |
| 911 | const SkPoint3& target, |
| 912 | SkScalar specularExponent, |
| 913 | SkScalar cutoffAngle, |
| 914 | SkColor color) |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 915 | : INHERITED(color), |
| 916 | fLocation(location), |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 917 | fTarget(target), |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 918 | fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSpecularExponentMax)) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 919 | { |
| 920 | fS = target - location; |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 921 | fast_normalize(&fS); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 922 | fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 923 | const SkScalar antiAliasThreshold = 0.016f; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 924 | fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; |
| 925 | fConeScale = SkScalarInvert(antiAliasThreshold); |
| 926 | } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 927 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 928 | SkImageFilterLight* transform(const SkMatrix& matrix) const override { |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 929 | SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 930 | matrix.mapPoints(&location2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 931 | // Use X scale and Y scale on Z and average the result |
| 932 | SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 933 | matrix.mapVectors(&locationZ, 1); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 934 | SkPoint3 location = SkPoint3::Make(location2.fX, location2.fY, |
| 935 | SkScalarAve(locationZ.fX, locationZ.fY)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 936 | SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); |
| 937 | matrix.mapPoints(&target2, 1); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 938 | SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ); |
| 939 | matrix.mapVectors(&targetZ, 1); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 940 | SkPoint3 target = SkPoint3::Make(target2.fX, target2.fY, |
| 941 | SkScalarAve(targetZ.fX, targetZ.fY)); |
commit-bot@chromium.org | 1037d92 | 2014-03-13 19:45:41 +0000 | [diff] [blame] | 942 | SkPoint3 s = target - location; |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 943 | fast_normalize(&s); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 944 | return new SkSpotLight(location, |
| 945 | target, |
| 946 | fSpecularExponent, |
| 947 | fCosOuterConeAngle, |
| 948 | fCosInnerConeAngle, |
| 949 | fConeScale, |
| 950 | s, |
| 951 | color()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 952 | } |
| 953 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 954 | SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 955 | SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), |
| 956 | fLocation.fY - SkIntToScalar(y), |
| 957 | fLocation.fZ - SkScalarMul(SkIntToScalar(z), |
| 958 | surfaceScale)); |
jvanverth | 992c761 | 2015-07-17 07:22:30 -0700 | [diff] [blame] | 959 | fast_normalize(&direction); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 960 | return direction; |
| 961 | }; |
senorblanco@chromium.org | bdb1ec4 | 2012-07-09 14:29:51 +0000 | [diff] [blame] | 962 | SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 963 | SkScalar cosAngle = -surfaceToLight.dot(fS); |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 964 | SkScalar scale = 0; |
| 965 | if (cosAngle >= fCosOuterConeAngle) { |
| 966 | scale = SkScalarPow(cosAngle, fSpecularExponent); |
| 967 | if (cosAngle < fCosInnerConeAngle) { |
| 968 | scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle); |
| 969 | scale *= fConeScale; |
| 970 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 971 | } |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 972 | return this->color().makeScale(scale); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 973 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 974 | GrGLLight* createGLLight() const override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 975 | #if SK_SUPPORT_GPU |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 976 | return new GrGLSpotLight; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 977 | #else |
| 978 | SkDEBUGFAIL("Should not call in GPU-less build"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 979 | return nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 980 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 981 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 982 | bool requiresFragmentPosition() const override { return true; } |
| 983 | LightType type() const override { return kSpot_LightType; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 984 | const SkPoint3& location() const { return fLocation; } |
| 985 | const SkPoint3& target() const { return fTarget; } |
| 986 | SkScalar specularExponent() const { return fSpecularExponent; } |
| 987 | SkScalar cosInnerConeAngle() const { return fCosInnerConeAngle; } |
| 988 | SkScalar cosOuterConeAngle() const { return fCosOuterConeAngle; } |
| 989 | SkScalar coneScale() const { return fConeScale; } |
senorblanco@chromium.org | eb31184 | 2012-07-11 20:49:26 +0000 | [diff] [blame] | 990 | const SkPoint3& s() const { return fS; } |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 991 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 992 | SkSpotLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 993 | fLocation = readPoint3(buffer); |
| 994 | fTarget = readPoint3(buffer); |
| 995 | fSpecularExponent = buffer.readScalar(); |
| 996 | fCosOuterConeAngle = buffer.readScalar(); |
| 997 | fCosInnerConeAngle = buffer.readScalar(); |
| 998 | fConeScale = buffer.readScalar(); |
| 999 | fS = readPoint3(buffer); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 1000 | buffer.validate(SkScalarIsFinite(fSpecularExponent) && |
| 1001 | SkScalarIsFinite(fCosOuterConeAngle) && |
| 1002 | SkScalarIsFinite(fCosInnerConeAngle) && |
| 1003 | SkScalarIsFinite(fConeScale)); |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 1004 | } |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1005 | protected: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1006 | SkSpotLight(const SkPoint3& location, |
| 1007 | const SkPoint3& target, |
| 1008 | SkScalar specularExponent, |
| 1009 | SkScalar cosOuterConeAngle, |
| 1010 | SkScalar cosInnerConeAngle, |
| 1011 | SkScalar coneScale, |
| 1012 | const SkPoint3& s, |
| 1013 | const SkPoint3& color) |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1014 | : INHERITED(color), |
| 1015 | fLocation(location), |
| 1016 | fTarget(target), |
| 1017 | fSpecularExponent(specularExponent), |
| 1018 | fCosOuterConeAngle(cosOuterConeAngle), |
| 1019 | fCosInnerConeAngle(cosInnerConeAngle), |
| 1020 | fConeScale(coneScale), |
| 1021 | fS(s) |
| 1022 | { |
| 1023 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1024 | void onFlattenLight(SkWriteBuffer& buffer) const override { |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 1025 | writePoint3(fLocation, buffer); |
| 1026 | writePoint3(fTarget, buffer); |
| 1027 | buffer.writeScalar(fSpecularExponent); |
| 1028 | buffer.writeScalar(fCosOuterConeAngle); |
| 1029 | buffer.writeScalar(fCosInnerConeAngle); |
| 1030 | buffer.writeScalar(fConeScale); |
| 1031 | writePoint3(fS, buffer); |
| 1032 | } |
| 1033 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1034 | bool isEqual(const SkImageFilterLight& other) const override { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1035 | if (other.type() != kSpot_LightType) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
| 1039 | const SkSpotLight& o = static_cast<const SkSpotLight&>(other); |
| 1040 | return INHERITED::isEqual(other) && |
| 1041 | fLocation == o.fLocation && |
| 1042 | fTarget == o.fTarget && |
| 1043 | fSpecularExponent == o.fSpecularExponent && |
| 1044 | fCosOuterConeAngle == o.fCosOuterConeAngle; |
| 1045 | } |
| 1046 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1047 | private: |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 1048 | static const SkScalar kSpecularExponentMin; |
| 1049 | static const SkScalar kSpecularExponentMax; |
| 1050 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1051 | SkPoint3 fLocation; |
| 1052 | SkPoint3 fTarget; |
| 1053 | SkScalar fSpecularExponent; |
| 1054 | SkScalar fCosOuterConeAngle; |
| 1055 | SkScalar fCosInnerConeAngle; |
| 1056 | SkScalar fConeScale; |
| 1057 | SkPoint3 fS; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1058 | |
| 1059 | typedef SkImageFilterLight INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1060 | }; |
| 1061 | |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 1062 | // According to the spec, the specular term should be in the range [1, 128] : |
| 1063 | // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttribute |
| 1064 | const SkScalar SkSpotLight::kSpecularExponentMin = 1.0f; |
| 1065 | const SkScalar SkSpotLight::kSpecularExponentMax = 128.0f; |
| 1066 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1067 | /////////////////////////////////////////////////////////////////////////////// |
| 1068 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1069 | void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1070 | // Write type first, then baseclass, then subclass. |
| 1071 | buffer.writeInt(this->type()); |
| 1072 | writePoint3(fColor, buffer); |
| 1073 | this->onFlattenLight(buffer); |
| 1074 | } |
| 1075 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1076 | /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer& buffer) { |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1077 | // Read type first. |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1078 | const SkImageFilterLight::LightType type = (SkImageFilterLight::LightType)buffer.readInt(); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1079 | switch (type) { |
| 1080 | // Each of these constructors must first call SkLight's, so we'll read the baseclass |
| 1081 | // then subclass, same order as flattenLight. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1082 | case SkImageFilterLight::kDistant_LightType: |
| 1083 | return new SkDistantLight(buffer); |
| 1084 | case SkImageFilterLight::kPoint_LightType: |
| 1085 | return new SkPointLight(buffer); |
| 1086 | case SkImageFilterLight::kSpot_LightType: |
| 1087 | return new SkSpotLight(buffer); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1088 | default: |
| 1089 | SkDEBUGFAIL("Unknown LightType."); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1090 | buffer.validate(false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1091 | return nullptr; |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | /////////////////////////////////////////////////////////////////////////////// |
| 1095 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1096 | SkLightingImageFilter::SkLightingImageFilter(SkImageFilterLight* light, SkScalar surfaceScale, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 1097 | SkImageFilter* input, const CropRect* cropRect) |
| 1098 | : INHERITED(1, &input, cropRect) |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1099 | , fLight(SkRef(light)) |
| 1100 | , fSurfaceScale(surfaceScale / 255) |
| 1101 | {} |
| 1102 | |
| 1103 | SkImageFilter* SkLightingImageFilter::CreateDistantLitDiffuse(const SkPoint3& direction, |
| 1104 | SkColor lightColor, |
| 1105 | SkScalar surfaceScale, |
| 1106 | SkScalar kd, |
| 1107 | SkImageFilter* input, |
| 1108 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1109 | SkAutoTUnref<SkImageFilterLight> light(new SkDistantLight(direction, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1110 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1113 | SkImageFilter* SkLightingImageFilter::CreatePointLitDiffuse(const SkPoint3& location, |
| 1114 | SkColor lightColor, |
| 1115 | SkScalar surfaceScale, |
| 1116 | SkScalar kd, |
| 1117 | SkImageFilter* input, |
| 1118 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1119 | SkAutoTUnref<SkImageFilterLight> light(new SkPointLight(location, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1120 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1123 | SkImageFilter* SkLightingImageFilter::CreateSpotLitDiffuse(const SkPoint3& location, |
| 1124 | const SkPoint3& target, |
| 1125 | SkScalar specularExponent, |
| 1126 | SkScalar cutoffAngle, |
| 1127 | SkColor lightColor, |
| 1128 | SkScalar surfaceScale, |
| 1129 | SkScalar kd, |
| 1130 | SkImageFilter* input, |
| 1131 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1132 | SkAutoTUnref<SkImageFilterLight> light( |
| 1133 | new SkSpotLight(location, target, specularExponent, cutoffAngle, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1134 | return SkDiffuseLightingImageFilter::Create(light, surfaceScale, kd, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1137 | SkImageFilter* SkLightingImageFilter::CreateDistantLitSpecular(const SkPoint3& direction, |
| 1138 | SkColor lightColor, |
| 1139 | SkScalar surfaceScale, |
| 1140 | SkScalar ks, |
| 1141 | SkScalar shine, |
| 1142 | SkImageFilter* input, |
| 1143 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1144 | SkAutoTUnref<SkImageFilterLight> light(new SkDistantLight(direction, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1145 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1148 | SkImageFilter* SkLightingImageFilter::CreatePointLitSpecular(const SkPoint3& location, |
| 1149 | SkColor lightColor, |
| 1150 | SkScalar surfaceScale, |
| 1151 | SkScalar ks, |
| 1152 | SkScalar shine, |
| 1153 | SkImageFilter* input, |
| 1154 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1155 | SkAutoTUnref<SkImageFilterLight> light(new SkPointLight(location, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1156 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1159 | SkImageFilter* SkLightingImageFilter::CreateSpotLitSpecular(const SkPoint3& location, |
| 1160 | const SkPoint3& target, |
| 1161 | SkScalar specularExponent, |
| 1162 | SkScalar cutoffAngle, |
| 1163 | SkColor lightColor, |
| 1164 | SkScalar surfaceScale, |
| 1165 | SkScalar ks, |
| 1166 | SkScalar shine, |
| 1167 | SkImageFilter* input, |
| 1168 | const CropRect* cropRect) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1169 | SkAutoTUnref<SkImageFilterLight> light( |
| 1170 | new SkSpotLight(location, target, specularExponent, cutoffAngle, lightColor)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1171 | return SkSpecularLightingImageFilter::Create(light, surfaceScale, ks, shine, input, cropRect); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1174 | SkLightingImageFilter::~SkLightingImageFilter() {} |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1175 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1176 | void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1177 | this->INHERITED::flatten(buffer); |
commit-bot@chromium.org | 790c3f4 | 2013-10-14 16:28:56 +0000 | [diff] [blame] | 1178 | fLight->flattenLight(buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1179 | buffer.writeScalar(fSurfaceScale * 255); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1182 | /////////////////////////////////////////////////////////////////////////////// |
| 1183 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1184 | SkImageFilter* SkDiffuseLightingImageFilter::Create(SkImageFilterLight* light, |
| 1185 | SkScalar surfaceScale, |
| 1186 | SkScalar kd, |
| 1187 | SkImageFilter* input, |
| 1188 | const CropRect* cropRect) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1189 | if (nullptr == light) { |
| 1190 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1191 | } |
| 1192 | if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(kd)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1193 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1194 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1195 | // According to the spec, kd can be any non-negative number : |
| 1196 | // http://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1197 | if (kd < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1198 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1199 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1200 | return new SkDiffuseLightingImageFilter(light, surfaceScale, kd, input, cropRect); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1203 | SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkImageFilterLight* light, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1204 | SkScalar surfaceScale, |
| 1205 | SkScalar kd, |
| 1206 | SkImageFilter* input, |
| 1207 | const CropRect* cropRect) |
| 1208 | : INHERITED(light, surfaceScale, input, cropRect), |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1209 | fKD(kd) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1210 | { |
| 1211 | } |
| 1212 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1213 | SkFlattenable* SkDiffuseLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 1214 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1215 | SkAutoTUnref<SkImageFilterLight> light(SkImageFilterLight::UnflattenLight(buffer)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1216 | SkScalar surfaceScale = buffer.readScalar(); |
| 1217 | SkScalar kd = buffer.readScalar(); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 1218 | return Create(light, surfaceScale, kd, common.getInput(0).get(), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1219 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1220 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1221 | void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1222 | this->INHERITED::flatten(buffer); |
| 1223 | buffer.writeScalar(fKD); |
| 1224 | } |
| 1225 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 1226 | bool SkDiffuseLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, |
| 1227 | const SkBitmap& source, |
| 1228 | const Context& ctx, |
| 1229 | SkBitmap* dst, |
| 1230 | SkIPoint* offset) const { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1231 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1232 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 1233 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1234 | return false; |
| 1235 | } |
| 1236 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 1237 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 1240 | SkIRect srcBounds = src.bounds(); |
| 1241 | srcBounds.offset(srcOffset); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1242 | SkIRect bounds; |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 1243 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (bounds.width() < 2 || bounds.height() < 2) { |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1251 | SkAutoLockPixels alp(src); |
| 1252 | if (!src.getPixels()) { |
| 1253 | return false; |
| 1254 | } |
| 1255 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 1256 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 1257 | if (!device) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 1260 | *dst = device->accessBitmap(false); |
| 1261 | SkAutoLockPixels alp_dst(*dst); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1262 | |
senorblanco | 7b7ecfc | 2015-08-26 14:26:40 -0700 | [diff] [blame] | 1263 | SkMatrix matrix(ctx.ctm()); |
| 1264 | matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset.y())); |
| 1265 | SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1266 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1267 | DiffuseLightingType lightingType(fKD); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1268 | offset->fX = bounds.left(); |
| 1269 | offset->fY = bounds.top(); |
| 1270 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1271 | switch (transformedLight->type()) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1272 | case SkImageFilterLight::kDistant_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1273 | lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, |
| 1274 | transformedLight, |
| 1275 | src, |
| 1276 | dst, |
| 1277 | surfaceScale(), |
| 1278 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1279 | break; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1280 | case SkImageFilterLight::kPoint_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1281 | lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, |
| 1282 | transformedLight, |
| 1283 | src, |
| 1284 | dst, |
| 1285 | surfaceScale(), |
| 1286 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1287 | break; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1288 | case SkImageFilterLight::kSpot_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1289 | lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, |
| 1290 | transformedLight, |
| 1291 | src, |
| 1292 | dst, |
| 1293 | surfaceScale(), |
| 1294 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1297 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1298 | return true; |
| 1299 | } |
| 1300 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 1301 | #ifndef SK_IGNORE_TO_STRING |
| 1302 | void SkDiffuseLightingImageFilter::toString(SkString* str) const { |
| 1303 | str->appendf("SkDiffuseLightingImageFilter: ("); |
| 1304 | str->appendf("kD: %f\n", fKD); |
| 1305 | str->append(")"); |
| 1306 | } |
| 1307 | #endif |
| 1308 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1309 | #if SK_SUPPORT_GPU |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1310 | GrFragmentProcessor* SkDiffuseLightingImageFilter::getFragmentProcessor( |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1311 | GrTexture* texture, |
| 1312 | const SkMatrix& matrix, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1313 | const SkIRect* srcBounds, |
| 1314 | BoundaryMode boundaryMode) const { |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1315 | SkScalar scale = SkScalarMul(this->surfaceScale(), SkIntToScalar(255)); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1316 | return GrDiffuseLightingEffect::Create(texture, this->light(), scale, matrix, this->kd(), |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1317 | boundaryMode, srcBounds); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1318 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 1319 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1320 | |
| 1321 | /////////////////////////////////////////////////////////////////////////////// |
| 1322 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1323 | SkImageFilter* SkSpecularLightingImageFilter::Create(SkImageFilterLight* light, |
| 1324 | SkScalar surfaceScale, |
| 1325 | SkScalar ks, |
| 1326 | SkScalar shininess, |
| 1327 | SkImageFilter* input, |
| 1328 | const CropRect* cropRect) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1329 | if (nullptr == light) { |
| 1330 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1331 | } |
| 1332 | if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(ks) || !SkScalarIsFinite(shininess)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1333 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1334 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 1335 | // According to the spec, ks can be any non-negative number : |
| 1336 | // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1337 | if (ks < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1338 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1339 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1340 | return new SkSpecularLightingImageFilter(light, surfaceScale, ks, shininess, input, cropRect); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1343 | SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkImageFilterLight* light, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1344 | SkScalar surfaceScale, |
| 1345 | SkScalar ks, |
| 1346 | SkScalar shininess, |
| 1347 | SkImageFilter* input, |
| 1348 | const CropRect* cropRect) |
| 1349 | : INHERITED(light, surfaceScale, input, cropRect), |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1350 | fKS(ks), |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1351 | fShininess(shininess) |
| 1352 | { |
| 1353 | } |
| 1354 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1355 | SkFlattenable* SkSpecularLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 1356 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1357 | SkAutoTUnref<SkImageFilterLight> light(SkImageFilterLight::UnflattenLight(buffer)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1358 | SkScalar surfaceScale = buffer.readScalar(); |
| 1359 | SkScalar ks = buffer.readScalar(); |
| 1360 | SkScalar shine = buffer.readScalar(); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 1361 | return Create(light, surfaceScale, ks, shine, common.getInput(0).get(), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 1362 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1363 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1364 | void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1365 | this->INHERITED::flatten(buffer); |
| 1366 | buffer.writeScalar(fKS); |
| 1367 | buffer.writeScalar(fShininess); |
| 1368 | } |
| 1369 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 1370 | bool SkSpecularLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, |
| 1371 | const SkBitmap& source, |
| 1372 | const Context& ctx, |
| 1373 | SkBitmap* dst, |
| 1374 | SkIPoint* offset) const { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1375 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1376 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 1377 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
commit-bot@chromium.org | a8aef8b | 2013-10-23 15:33:44 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 1381 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1382 | return false; |
| 1383 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1384 | |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 1385 | SkIRect srcBounds = src.bounds(); |
| 1386 | srcBounds.offset(srcOffset); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1387 | SkIRect bounds; |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 1388 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1391 | |
| 1392 | if (bounds.width() < 2 || bounds.height() < 2) { |
| 1393 | return false; |
| 1394 | } |
| 1395 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1396 | SkAutoLockPixels alp(src); |
| 1397 | if (!src.getPixels()) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 1401 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 1402 | if (!device) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 1405 | *dst = device->accessBitmap(false); |
| 1406 | SkAutoLockPixels alp_dst(*dst); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1407 | |
| 1408 | SpecularLightingType lightingType(fKS, fShininess); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1409 | offset->fX = bounds.left(); |
| 1410 | offset->fY = bounds.top(); |
senorblanco | 7b7ecfc | 2015-08-26 14:26:40 -0700 | [diff] [blame] | 1411 | SkMatrix matrix(ctx.ctm()); |
| 1412 | matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset.y())); |
| 1413 | SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1414 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1415 | switch (transformedLight->type()) { |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1416 | case SkImageFilterLight::kDistant_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1417 | lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, |
| 1418 | transformedLight, |
| 1419 | src, |
| 1420 | dst, |
| 1421 | surfaceScale(), |
| 1422 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1423 | break; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1424 | case SkImageFilterLight::kPoint_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1425 | lightBitmap<SpecularLightingType, SkPointLight>(lightingType, |
| 1426 | transformedLight, |
| 1427 | src, |
| 1428 | dst, |
| 1429 | surfaceScale(), |
| 1430 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1431 | break; |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1432 | case SkImageFilterLight::kSpot_LightType: |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1433 | lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, |
| 1434 | transformedLight, |
| 1435 | src, |
| 1436 | dst, |
| 1437 | surfaceScale(), |
| 1438 | bounds); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | return true; |
| 1442 | } |
| 1443 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 1444 | #ifndef SK_IGNORE_TO_STRING |
| 1445 | void SkSpecularLightingImageFilter::toString(SkString* str) const { |
| 1446 | str->appendf("SkSpecularLightingImageFilter: ("); |
| 1447 | str->appendf("kS: %f shininess: %f", fKS, fShininess); |
| 1448 | str->append(")"); |
| 1449 | } |
| 1450 | #endif |
| 1451 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1452 | #if SK_SUPPORT_GPU |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1453 | GrFragmentProcessor* SkSpecularLightingImageFilter::getFragmentProcessor( |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1454 | GrTexture* texture, |
| 1455 | const SkMatrix& matrix, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1456 | const SkIRect* srcBounds, |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 1457 | BoundaryMode boundaryMode) const { |
| 1458 | SkScalar scale = SkScalarMul(this->surfaceScale(), SkIntToScalar(255)); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1459 | return GrSpecularLightingEffect::Create(texture, this->light(), scale, matrix, this->ks(), |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1460 | this->shininess(), boundaryMode, srcBounds); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1461 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 1462 | #endif |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1463 | |
| 1464 | /////////////////////////////////////////////////////////////////////////////// |
| 1465 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1466 | #if SK_SUPPORT_GPU |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1467 | |
| 1468 | namespace { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1469 | SkPoint3 random_point3(SkRandom* random) { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 1470 | return SkPoint3::Make(SkScalarToFloat(random->nextSScalar1()), |
| 1471 | SkScalarToFloat(random->nextSScalar1()), |
| 1472 | SkScalarToFloat(random->nextSScalar1())); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1475 | SkImageFilterLight* create_random_light(SkRandom* random) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1476 | int type = random->nextULessThan(3); |
| 1477 | switch (type) { |
| 1478 | case 0: { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1479 | return new SkDistantLight(random_point3(random), random->nextU()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1480 | } |
| 1481 | case 1: { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1482 | return new SkPointLight(random_point3(random), random->nextU()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1483 | } |
| 1484 | case 2: { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1485 | return new SkSpotLight(random_point3(random), random_point3(random), |
| 1486 | random->nextUScalar1(), random->nextUScalar1(), random->nextU()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1487 | } |
| 1488 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1489 | SkFAIL("Unexpected value."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1490 | return nullptr; |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1494 | SkString emitNormalFunc(BoundaryMode mode, |
| 1495 | const char* pointToNormalName, |
| 1496 | const char* sobelFuncName) { |
| 1497 | SkString result; |
| 1498 | switch (mode) { |
| 1499 | case kTopLeft_BoundaryMode: |
| 1500 | result.printf("\treturn %s(%s(0.0, 0.0, m[4], m[5], m[7], m[8], %g),\n" |
| 1501 | "\t %s(0.0, 0.0, m[4], m[7], m[5], m[8], %g),\n" |
| 1502 | "\t surfaceScale);\n", |
| 1503 | pointToNormalName, sobelFuncName, gTwoThirds, |
| 1504 | sobelFuncName, gTwoThirds); |
| 1505 | break; |
| 1506 | case kTop_BoundaryMode: |
| 1507 | result.printf("\treturn %s(%s(0.0, 0.0, m[3], m[5], m[6], m[8], %g),\n" |
| 1508 | "\t %s(0.0, 0.0, m[4], m[7], m[5], m[8], %g),\n" |
| 1509 | "\t surfaceScale);\n", |
| 1510 | pointToNormalName, sobelFuncName, gOneThird, |
| 1511 | sobelFuncName, gOneHalf); |
| 1512 | break; |
| 1513 | case kTopRight_BoundaryMode: |
| 1514 | result.printf("\treturn %s(%s( 0.0, 0.0, m[3], m[4], m[6], m[7], %g),\n" |
| 1515 | "\t %s(m[3], m[6], m[4], m[7], 0.0, 0.0, %g),\n" |
| 1516 | "\t surfaceScale);\n", |
| 1517 | pointToNormalName, sobelFuncName, gTwoThirds, |
| 1518 | sobelFuncName, gTwoThirds); |
| 1519 | break; |
| 1520 | case kLeft_BoundaryMode: |
| 1521 | result.printf("\treturn %s(%s(m[1], m[2], m[4], m[5], m[7], m[8], %g),\n" |
| 1522 | "\t %s( 0.0, 0.0, m[1], m[7], m[2], m[8], %g),\n" |
| 1523 | "\t surfaceScale);\n", |
| 1524 | pointToNormalName, sobelFuncName, gOneHalf, |
| 1525 | sobelFuncName, gOneThird); |
| 1526 | break; |
| 1527 | case kInterior_BoundaryMode: |
| 1528 | result.printf("\treturn %s(%s(m[0], m[2], m[3], m[5], m[6], m[8], %g),\n" |
| 1529 | "\t %s(m[0], m[6], m[1], m[7], m[2], m[8], %g),\n" |
| 1530 | "\t surfaceScale);\n", |
| 1531 | pointToNormalName, sobelFuncName, gOneQuarter, |
| 1532 | sobelFuncName, gOneQuarter); |
| 1533 | break; |
| 1534 | case kRight_BoundaryMode: |
| 1535 | result.printf("\treturn %s(%s(m[0], m[1], m[3], m[4], m[6], m[7], %g),\n" |
| 1536 | "\t %s(m[0], m[6], m[1], m[7], 0.0, 0.0, %g),\n" |
| 1537 | "\t surfaceScale);\n", |
| 1538 | pointToNormalName, sobelFuncName, gOneHalf, |
| 1539 | sobelFuncName, gOneThird); |
| 1540 | break; |
| 1541 | case kBottomLeft_BoundaryMode: |
| 1542 | result.printf("\treturn %s(%s(m[1], m[2], m[4], m[5], 0.0, 0.0, %g),\n" |
| 1543 | "\t %s( 0.0, 0.0, m[1], m[4], m[2], m[5], %g),\n" |
| 1544 | "\t surfaceScale);\n", |
| 1545 | pointToNormalName, sobelFuncName, gTwoThirds, |
| 1546 | sobelFuncName, gTwoThirds); |
| 1547 | break; |
| 1548 | case kBottom_BoundaryMode: |
| 1549 | result.printf("\treturn %s(%s(m[0], m[2], m[3], m[5], 0.0, 0.0, %g),\n" |
| 1550 | "\t %s(m[0], m[3], m[1], m[4], m[2], m[5], %g),\n" |
| 1551 | "\t surfaceScale);\n", |
| 1552 | pointToNormalName, sobelFuncName, gOneThird, |
| 1553 | sobelFuncName, gOneHalf); |
| 1554 | break; |
| 1555 | case kBottomRight_BoundaryMode: |
| 1556 | result.printf("\treturn %s(%s(m[0], m[1], m[3], m[4], 0.0, 0.0, %g),\n" |
| 1557 | "\t %s(m[0], m[3], m[1], m[4], 0.0, 0.0, %g),\n" |
| 1558 | "\t surfaceScale);\n", |
| 1559 | pointToNormalName, sobelFuncName, gTwoThirds, |
| 1560 | sobelFuncName, gTwoThirds); |
| 1561 | break; |
| 1562 | default: |
| 1563 | SkASSERT(false); |
| 1564 | break; |
| 1565 | } |
| 1566 | return result; |
| 1567 | } |
| 1568 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 1571 | class GrGLLightingEffect : public GrGLSLFragmentProcessor { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1572 | public: |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1573 | GrGLLightingEffect() : fLight(nullptr) { } |
| 1574 | virtual ~GrGLLightingEffect() { delete fLight; } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1575 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1576 | void emitCode(EmitArgs&) override; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1577 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 1578 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder* b); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1579 | |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1580 | protected: |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1581 | /** |
| 1582 | * Subclasses of GrGLLightingEffect must call INHERITED::onSetData(); |
| 1583 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1584 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1585 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1586 | virtual void emitLightFunc(GrGLSLUniformHandler*, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1587 | GrGLSLFPFragmentBuilder*, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1588 | SkString* funcName) = 0; |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1589 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1590 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 1591 | typedef GrGLSLFragmentProcessor INHERITED; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1592 | |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1593 | UniformHandle fImageIncrementUni; |
| 1594 | UniformHandle fSurfaceScaleUni; |
| 1595 | GrTextureDomain::GLDomain fDomain; |
| 1596 | GrGLLight* fLight; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1597 | }; |
| 1598 | |
| 1599 | /////////////////////////////////////////////////////////////////////////////// |
| 1600 | |
| 1601 | class GrGLDiffuseLightingEffect : public GrGLLightingEffect { |
| 1602 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1603 | void emitLightFunc(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, SkString* funcName) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1604 | |
| 1605 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1606 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1607 | |
| 1608 | private: |
| 1609 | typedef GrGLLightingEffect INHERITED; |
| 1610 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1611 | UniformHandle fKDUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1612 | }; |
| 1613 | |
| 1614 | /////////////////////////////////////////////////////////////////////////////// |
| 1615 | |
| 1616 | class GrGLSpecularLightingEffect : public GrGLLightingEffect { |
| 1617 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1618 | void emitLightFunc(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, SkString* funcName) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1619 | |
| 1620 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1621 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1622 | |
| 1623 | private: |
| 1624 | typedef GrGLLightingEffect INHERITED; |
| 1625 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1626 | UniformHandle fKSUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1627 | UniformHandle fShininessUni; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1628 | }; |
| 1629 | |
| 1630 | /////////////////////////////////////////////////////////////////////////////// |
| 1631 | |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1632 | namespace { |
| 1633 | |
| 1634 | GrTextureDomain create_domain(GrTexture* texture, const SkIRect* srcBounds, |
| 1635 | GrTextureDomain::Mode mode) { |
| 1636 | if (srcBounds) { |
| 1637 | SkRect texelDomain = GrTextureDomain::MakeTexelDomainForMode(texture, *srcBounds, mode); |
| 1638 | return GrTextureDomain(texelDomain, mode); |
| 1639 | } else { |
| 1640 | return GrTextureDomain(SkRect::MakeEmpty(), GrTextureDomain::kIgnore_Mode); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | }; |
| 1645 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1646 | GrLightingEffect::GrLightingEffect(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1647 | const SkImageFilterLight* light, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1648 | SkScalar surfaceScale, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1649 | const SkMatrix& matrix, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1650 | BoundaryMode boundaryMode, |
| 1651 | const SkIRect* srcBounds) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1652 | : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 1653 | , fLight(light) |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1654 | , fSurfaceScale(surfaceScale) |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1655 | , fFilterMatrix(matrix) |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1656 | , fBoundaryMode(boundaryMode) |
| 1657 | , fDomain(create_domain(texture, srcBounds, GrTextureDomain::kDecal_Mode)) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1658 | fLight->ref(); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 1659 | if (light->requiresFragmentPosition()) { |
| 1660 | this->setWillReadFragmentPosition(); |
| 1661 | } |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | GrLightingEffect::~GrLightingEffect() { |
| 1665 | fLight->unref(); |
| 1666 | } |
| 1667 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 1668 | bool GrLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1669 | const GrLightingEffect& s = sBase.cast<GrLightingEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 1670 | return fLight->isEqual(*s.fLight) && |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1671 | fSurfaceScale == s.fSurfaceScale && |
| 1672 | fBoundaryMode == s.fBoundaryMode; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /////////////////////////////////////////////////////////////////////////////// |
| 1676 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1677 | GrDiffuseLightingEffect::GrDiffuseLightingEffect(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1678 | const SkImageFilterLight* light, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1679 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1680 | const SkMatrix& matrix, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1681 | SkScalar kd, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1682 | BoundaryMode boundaryMode, |
| 1683 | const SkIRect* srcBounds) |
| 1684 | : INHERITED(texture, light, surfaceScale, matrix, boundaryMode, srcBounds), fKD(kd) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1685 | this->initClassID<GrDiffuseLightingEffect>(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 1688 | bool GrDiffuseLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1689 | const GrDiffuseLightingEffect& s = sBase.cast<GrDiffuseLightingEffect>(); |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1690 | return INHERITED::onIsEqual(sBase) && this->kd() == s.kd(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1693 | void GrDiffuseLightingEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 1694 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1695 | GrGLDiffuseLightingEffect::GenKey(*this, caps, b); |
| 1696 | } |
| 1697 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1698 | GrGLSLFragmentProcessor* GrDiffuseLightingEffect::onCreateGLSLInstance() const { |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1699 | return new GrGLDiffuseLightingEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1700 | } |
| 1701 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1702 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDiffuseLightingEffect); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1703 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1704 | const GrFragmentProcessor* GrDiffuseLightingEffect::TestCreate(GrProcessorTestData* d) { |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1705 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 1706 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 1707 | GrTexture* tex = d->fTextures[texIdx]; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1708 | SkScalar surfaceScale = d->fRandom->nextSScalar1(); |
| 1709 | SkScalar kd = d->fRandom->nextUScalar1(); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1710 | SkAutoTUnref<SkImageFilterLight> light(create_random_light(d->fRandom)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1711 | SkMatrix matrix; |
| 1712 | for (int i = 0; i < 9; i++) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1713 | matrix[i] = d->fRandom->nextUScalar1(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1714 | } |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1715 | SkIRect srcBounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, tex->width()), |
| 1716 | d->fRandom->nextRangeU(0, tex->height()), |
| 1717 | d->fRandom->nextRangeU(0, tex->width()), |
| 1718 | d->fRandom->nextRangeU(0, tex->height())); |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1719 | BoundaryMode mode = static_cast<BoundaryMode>(d->fRandom->nextU() % kBoundaryModeCount); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1720 | return GrDiffuseLightingEffect::Create(tex, light, surfaceScale, matrix, kd, mode, &srcBounds); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1724 | /////////////////////////////////////////////////////////////////////////////// |
| 1725 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1726 | void GrGLLightingEffect::emitCode(EmitArgs& args) { |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1727 | const GrLightingEffect& le = args.fFp.cast<GrLightingEffect>(); |
| 1728 | if (!fLight) { |
| 1729 | fLight = le.light()->createGLLight(); |
| 1730 | } |
| 1731 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1732 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1733 | fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1734 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 1735 | "ImageIncrement"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1736 | fSurfaceScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1737 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 1738 | "SurfaceScale"); |
| 1739 | fLight->emitLightColorUniform(uniformHandler); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1740 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1741 | SkString lightFunc; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1742 | this->emitLightFunc(uniformHandler, fragBuilder, &lightFunc); |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 1743 | static const GrGLSLShaderVar gSobelArgs[] = { |
| 1744 | GrGLSLShaderVar("a", kFloat_GrSLType), |
| 1745 | GrGLSLShaderVar("b", kFloat_GrSLType), |
| 1746 | GrGLSLShaderVar("c", kFloat_GrSLType), |
| 1747 | GrGLSLShaderVar("d", kFloat_GrSLType), |
| 1748 | GrGLSLShaderVar("e", kFloat_GrSLType), |
| 1749 | GrGLSLShaderVar("f", kFloat_GrSLType), |
| 1750 | GrGLSLShaderVar("scale", kFloat_GrSLType), |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1751 | }; |
| 1752 | SkString sobelFuncName; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1753 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1754 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1755 | fragBuilder->emitFunction(kFloat_GrSLType, |
| 1756 | "sobel", |
| 1757 | SK_ARRAY_COUNT(gSobelArgs), |
| 1758 | gSobelArgs, |
| 1759 | "\treturn (-a + b - 2.0 * c + 2.0 * d -e + f) * scale;\n", |
| 1760 | &sobelFuncName); |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 1761 | static const GrGLSLShaderVar gPointToNormalArgs[] = { |
| 1762 | GrGLSLShaderVar("x", kFloat_GrSLType), |
| 1763 | GrGLSLShaderVar("y", kFloat_GrSLType), |
| 1764 | GrGLSLShaderVar("scale", kFloat_GrSLType), |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1765 | }; |
| 1766 | SkString pointToNormalName; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1767 | fragBuilder->emitFunction(kVec3f_GrSLType, |
| 1768 | "pointToNormal", |
| 1769 | SK_ARRAY_COUNT(gPointToNormalArgs), |
| 1770 | gPointToNormalArgs, |
| 1771 | "\treturn normalize(vec3(-x * scale, -y * scale, 1));\n", |
| 1772 | &pointToNormalName); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1773 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 1774 | static const GrGLSLShaderVar gInteriorNormalArgs[] = { |
| 1775 | GrGLSLShaderVar("m", kFloat_GrSLType, 9), |
| 1776 | GrGLSLShaderVar("surfaceScale", kFloat_GrSLType), |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1777 | }; |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1778 | SkString normalBody = emitNormalFunc(le.boundaryMode(), |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1779 | pointToNormalName.c_str(), |
| 1780 | sobelFuncName.c_str()); |
| 1781 | SkString normalName; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1782 | fragBuilder->emitFunction(kVec3f_GrSLType, |
| 1783 | "normal", |
| 1784 | SK_ARRAY_COUNT(gInteriorNormalArgs), |
| 1785 | gInteriorNormalArgs, |
| 1786 | normalBody.c_str(), |
| 1787 | &normalName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1788 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1789 | fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 1790 | fragBuilder->codeAppend("\t\tfloat m[9];\n"); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1791 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1792 | const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni); |
| 1793 | const char* surfScale = uniformHandler->getUniformCStr(fSurfaceScaleUni); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1794 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1795 | int index = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1796 | for (int dy = 1; dy >= -1; dy--) { |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1797 | for (int dx = -1; dx <= 1; dx++) { |
| 1798 | SkString texCoords; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1799 | texCoords.appendf("coord + vec2(%d, %d) * %s", dx, dy, imgInc); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1800 | SkString temp; |
| 1801 | temp.appendf("temp%d", index); |
| 1802 | fragBuilder->codeAppendf("vec4 %s;", temp.c_str()); |
| 1803 | fDomain.sampleTexture(fragBuilder, |
| 1804 | args.fUniformHandler, |
| 1805 | args.fGLSLCaps, |
| 1806 | le.domain(), |
| 1807 | temp.c_str(), |
| 1808 | texCoords, |
| 1809 | args.fSamplers[0]); |
| 1810 | fragBuilder->codeAppendf("m[%d] = %s.a;", index, temp.c_str()); |
| 1811 | index++; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1814 | fragBuilder->codeAppend("\t\tvec3 surfaceToLight = "); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1815 | SkString arg; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1816 | arg.appendf("%s * m[4]", surfScale); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1817 | fLight->emitSurfaceToLight(uniformHandler, fragBuilder, arg.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1818 | fragBuilder->codeAppend(";\n"); |
| 1819 | fragBuilder->codeAppendf("\t\t%s = %s(%s(m, %s), surfaceToLight, ", |
| 1820 | args.fOutputColor, lightFunc.c_str(), normalName.c_str(), surfScale); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1821 | fLight->emitLightColor(uniformHandler, fragBuilder, "surfaceToLight"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1822 | fragBuilder->codeAppend(");\n"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 1823 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1824 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1825 | fragBuilder->codeAppend(modulate.c_str()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1828 | void GrGLLightingEffect::GenKey(const GrProcessor& proc, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 1829 | const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1830 | const GrLightingEffect& lighting = proc.cast<GrLightingEffect>(); |
| 1831 | b->add32(lighting.boundaryMode() << 2 | lighting.light()->type()); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1832 | b->add32(GrTextureDomain::GLDomain::DomainKey(lighting.domain())); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1835 | void GrGLLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 1836 | const GrProcessor& proc) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1837 | const GrLightingEffect& lighting = proc.cast<GrLightingEffect>(); |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1838 | if (!fLight) { |
| 1839 | fLight = lighting.light()->createGLLight(); |
| 1840 | } |
| 1841 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 1842 | GrTexture* texture = lighting.texture(0); |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 1843 | float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1844 | pdman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->height()); |
| 1845 | pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1846 | SkAutoTUnref<SkImageFilterLight> transformedLight( |
| 1847 | lighting.light()->transform(lighting.filterMatrix())); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1848 | fDomain.setData(pdman, lighting.domain(), texture->origin()); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1849 | fLight->setData(pdman, transformedLight); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /////////////////////////////////////////////////////////////////////////////// |
| 1853 | |
| 1854 | /////////////////////////////////////////////////////////////////////////////// |
| 1855 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1856 | void GrGLDiffuseLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1857 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1858 | SkString* funcName) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1859 | const char* kd; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1860 | fKDUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 1861 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 1862 | "KD", &kd); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1863 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 1864 | static const GrGLSLShaderVar gLightArgs[] = { |
| 1865 | GrGLSLShaderVar("normal", kVec3f_GrSLType), |
| 1866 | GrGLSLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1867 | GrGLSLShaderVar("lightColor", kVec3f_GrSLType) |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1868 | }; |
| 1869 | SkString lightBody; |
| 1870 | lightBody.appendf("\tfloat colorScale = %s * dot(normal, surfaceToLight);\n", kd); |
| 1871 | lightBody.appendf("\treturn vec4(lightColor * clamp(colorScale, 0.0, 1.0), 1.0);\n"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1872 | fragBuilder->emitFunction(kVec4f_GrSLType, |
| 1873 | "light", |
| 1874 | SK_ARRAY_COUNT(gLightArgs), |
| 1875 | gLightArgs, |
| 1876 | lightBody.c_str(), |
| 1877 | funcName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1880 | void GrGLDiffuseLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 1881 | const GrProcessor& proc) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1882 | INHERITED::onSetData(pdman, proc); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1883 | const GrDiffuseLightingEffect& diffuse = proc.cast<GrDiffuseLightingEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1884 | pdman.set1f(fKDUni, diffuse.kd()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /////////////////////////////////////////////////////////////////////////////// |
| 1888 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1889 | GrSpecularLightingEffect::GrSpecularLightingEffect(GrTexture* texture, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1890 | const SkImageFilterLight* light, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1891 | SkScalar surfaceScale, |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1892 | const SkMatrix& matrix, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 1893 | SkScalar ks, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 1894 | SkScalar shininess, |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1895 | BoundaryMode boundaryMode, |
| 1896 | const SkIRect* srcBounds) |
| 1897 | : INHERITED(texture, light, surfaceScale, matrix, boundaryMode, srcBounds) |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1898 | , fKS(ks) |
| 1899 | , fShininess(shininess) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1900 | this->initClassID<GrSpecularLightingEffect>(); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 1903 | bool GrSpecularLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1904 | const GrSpecularLightingEffect& s = sBase.cast<GrSpecularLightingEffect>(); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1905 | return INHERITED::onIsEqual(sBase) && |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1906 | this->ks() == s.ks() && |
| 1907 | this->shininess() == s.shininess(); |
| 1908 | } |
| 1909 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1910 | void GrSpecularLightingEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 1911 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1912 | GrGLSpecularLightingEffect::GenKey(*this, caps, b); |
| 1913 | } |
| 1914 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1915 | GrGLSLFragmentProcessor* GrSpecularLightingEffect::onCreateGLSLInstance() const { |
robertphillips | d3b32bf | 2016-02-05 07:15:39 -0800 | [diff] [blame] | 1916 | return new GrGLSpecularLightingEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1919 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSpecularLightingEffect); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1920 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1921 | const GrFragmentProcessor* GrSpecularLightingEffect::TestCreate(GrProcessorTestData* d) { |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1922 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 1923 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 1924 | GrTexture* tex = d->fTextures[texIdx]; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1925 | SkScalar surfaceScale = d->fRandom->nextSScalar1(); |
| 1926 | SkScalar ks = d->fRandom->nextUScalar1(); |
| 1927 | SkScalar shininess = d->fRandom->nextUScalar1(); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1928 | SkAutoTUnref<SkImageFilterLight> light(create_random_light(d->fRandom)); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1929 | SkMatrix matrix; |
| 1930 | for (int i = 0; i < 9; i++) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1931 | matrix[i] = d->fRandom->nextUScalar1(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1932 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1933 | BoundaryMode mode = static_cast<BoundaryMode>(d->fRandom->nextU() % kBoundaryModeCount); |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1934 | SkIRect srcBounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, tex->width()), |
| 1935 | d->fRandom->nextRangeU(0, tex->height()), |
| 1936 | d->fRandom->nextRangeU(0, tex->width()), |
| 1937 | d->fRandom->nextRangeU(0, tex->height())); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 1938 | return GrSpecularLightingEffect::Create(d->fTextures[GrProcessorUnitTest::kAlphaTextureIdx], |
senorblanco | 9bd5f74 | 2016-02-17 10:59:47 -0800 | [diff] [blame] | 1939 | light, surfaceScale, matrix, ks, shininess, mode, |
| 1940 | &srcBounds); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1943 | /////////////////////////////////////////////////////////////////////////////// |
| 1944 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1945 | void GrGLSpecularLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1946 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1947 | SkString* funcName) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1948 | const char* ks; |
| 1949 | const char* shininess; |
| 1950 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1951 | fKSUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1952 | kFloat_GrSLType, kDefault_GrSLPrecision, "KS", &ks); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1953 | fShininessUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1954 | kFloat_GrSLType, |
| 1955 | kDefault_GrSLPrecision, |
| 1956 | "Shininess", |
| 1957 | &shininess); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1958 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 1959 | static const GrGLSLShaderVar gLightArgs[] = { |
| 1960 | GrGLSLShaderVar("normal", kVec3f_GrSLType), |
| 1961 | GrGLSLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1962 | GrGLSLShaderVar("lightColor", kVec3f_GrSLType) |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 1963 | }; |
| 1964 | SkString lightBody; |
| 1965 | lightBody.appendf("\tvec3 halfDir = vec3(normalize(surfaceToLight + vec3(0, 0, 1)));\n"); |
| 1966 | lightBody.appendf("\tfloat colorScale = %s * pow(dot(normal, halfDir), %s);\n", ks, shininess); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 1967 | lightBody.appendf("\tvec3 color = lightColor * clamp(colorScale, 0.0, 1.0);\n"); |
| 1968 | lightBody.appendf("\treturn vec4(color, max(max(color.r, color.g), color.b));\n"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1969 | fragBuilder->emitFunction(kVec4f_GrSLType, |
| 1970 | "light", |
| 1971 | SK_ARRAY_COUNT(gLightArgs), |
| 1972 | gLightArgs, |
| 1973 | lightBody.c_str(), |
| 1974 | funcName); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1977 | void GrGLSpecularLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 1978 | const GrProcessor& effect) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1979 | INHERITED::onSetData(pdman, effect); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1980 | const GrSpecularLightingEffect& spec = effect.cast<GrSpecularLightingEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1981 | pdman.set1f(fKSUni, spec.ks()); |
| 1982 | pdman.set1f(fShininessUni, spec.shininess()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /////////////////////////////////////////////////////////////////////////////// |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1986 | void GrGLLight::emitLightColorUniform(GrGLSLUniformHandler* uniformHandler) { |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1987 | fColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1988 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 1989 | "LightColor"); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1992 | void GrGLLight::emitLightColor(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1993 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1994 | const char *surfaceToLight) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1995 | fragBuilder->codeAppend(uniformHandler->getUniformCStr(this->lightColorUni())); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1998 | void GrGLLight::setData(const GrGLSLProgramDataManager& pdman, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 1999 | const SkImageFilterLight* light) const { |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 2000 | setUniformPoint3(pdman, fColorUni, |
| 2001 | light->color().makeScale(SkScalarInvert(SkIntToScalar(255)))); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2004 | /////////////////////////////////////////////////////////////////////////////// |
| 2005 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 2006 | void GrGLDistantLight::setData(const GrGLSLProgramDataManager& pdman, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2007 | const SkImageFilterLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2008 | INHERITED::setData(pdman, light); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2009 | SkASSERT(light->type() == SkImageFilterLight::kDistant_LightType); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2010 | const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2011 | setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2014 | void GrGLDistantLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 2015 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2016 | const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2017 | const char* dir; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2018 | fDirectionUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2019 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 2020 | "LightDirection", &dir); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2021 | fragBuilder->codeAppend(dir); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /////////////////////////////////////////////////////////////////////////////// |
| 2025 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 2026 | void GrGLPointLight::setData(const GrGLSLProgramDataManager& pdman, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2027 | const SkImageFilterLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2028 | INHERITED::setData(pdman, light); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2029 | SkASSERT(light->type() == SkImageFilterLight::kPoint_LightType); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2030 | const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2031 | setUniformPoint3(pdman, fLocationUni, pointLight->location()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2034 | void GrGLPointLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 2035 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2036 | const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2037 | const char* loc; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2038 | fLocationUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2039 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 2040 | "LightLocation", &loc); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2041 | fragBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 2042 | loc, fragBuilder->fragmentPosition(), z); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | /////////////////////////////////////////////////////////////////////////////// |
| 2046 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 2047 | void GrGLSpotLight::setData(const GrGLSLProgramDataManager& pdman, |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2048 | const SkImageFilterLight* light) const { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2049 | INHERITED::setData(pdman, light); |
robertphillips | 2f0dbc7 | 2015-08-20 05:15:06 -0700 | [diff] [blame] | 2050 | SkASSERT(light->type() == SkImageFilterLight::kSpot_LightType); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2051 | const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 2052 | setUniformPoint3(pdman, fLocationUni, spotLight->location()); |
| 2053 | pdman.set1f(fExponentUni, spotLight->specularExponent()); |
| 2054 | pdman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); |
| 2055 | pdman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); |
| 2056 | pdman.set1f(fConeScaleUni, spotLight->coneScale()); |
| 2057 | setUniformNormal3(pdman, fSUni, spotLight->s()); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2060 | void GrGLSpotLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 2061 | GrGLSLFPFragmentBuilder* fragBuilder, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2062 | const char* z) { |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2063 | const char* location; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2064 | fLocationUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2065 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 2066 | "LightLocation", &location); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 2067 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2068 | fragBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 2069 | location, fragBuilder->fragmentPosition(), z); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2072 | void GrGLSpotLight::emitLightColor(GrGLSLUniformHandler* uniformHandler, |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 2073 | GrGLSLFPFragmentBuilder* fragBuilder, |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2074 | const char *surfaceToLight) { |
| 2075 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2076 | const char* color = uniformHandler->getUniformCStr(this->lightColorUni()); // created by parent class. |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2077 | |
| 2078 | const char* exponent; |
| 2079 | const char* cosInner; |
| 2080 | const char* cosOuter; |
| 2081 | const char* coneScale; |
| 2082 | const char* s; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2083 | fExponentUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2084 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 2085 | "Exponent", &exponent); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2086 | fCosInnerConeAngleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2087 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 2088 | "CosInnerConeAngle", &cosInner); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2089 | fCosOuterConeAngleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2090 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 2091 | "CosOuterConeAngle", &cosOuter); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2092 | fConeScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2093 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 2094 | "ConeScale", &coneScale); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 2095 | fSUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 2096 | kVec3f_GrSLType, kDefault_GrSLPrecision, "S", &s); |
bsalomon@google.com | ae5ef11 | 2012-10-29 14:53:53 +0000 | [diff] [blame] | 2097 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 2098 | static const GrGLSLShaderVar gLightColorArgs[] = { |
| 2099 | GrGLSLShaderVar("surfaceToLight", kVec3f_GrSLType) |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 2100 | }; |
| 2101 | SkString lightColorBody; |
| 2102 | lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); |
| 2103 | lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); |
| 2104 | lightColorBody.appendf("\t\treturn vec3(0);\n"); |
| 2105 | lightColorBody.appendf("\t}\n"); |
| 2106 | lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); |
| 2107 | lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); |
| 2108 | lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", |
| 2109 | color, cosOuter, coneScale); |
| 2110 | lightColorBody.appendf("\t}\n"); |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 2111 | lightColorBody.appendf("\treturn %s;\n", color); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2112 | fragBuilder->emitFunction(kVec3f_GrSLType, |
| 2113 | "lightColor", |
| 2114 | SK_ARRAY_COUNT(gLightColorArgs), |
| 2115 | gLightColorArgs, |
| 2116 | lightColorBody.c_str(), |
| 2117 | &fLightColorFunc); |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 2118 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 2119 | fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 2122 | #endif |
| 2123 | |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 2124 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
| 2125 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
| 2126 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
djsollen@google.com | 0833777 | 2012-06-26 14:33:13 +0000 | [diff] [blame] | 2127 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |