SkLightingImageFilter.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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. #include "include/core/SkBitmap.h"
  8. #include "include/core/SkPoint3.h"
  9. #include "include/core/SkTypes.h"
  10. #include "include/effects/SkLightingImageFilter.h"
  11. #include "include/private/SkColorData.h"
  12. #include "src/core/SkImageFilterPriv.h"
  13. #include "src/core/SkReadBuffer.h"
  14. #include "src/core/SkSpecialImage.h"
  15. #include "src/core/SkWriteBuffer.h"
  16. #if SK_SUPPORT_GPU
  17. #include "include/gpu/GrTexture.h"
  18. #include "include/private/GrRecordingContext.h"
  19. #include "src/gpu/GrCaps.h"
  20. #include "src/gpu/GrFixedClip.h"
  21. #include "src/gpu/GrFragmentProcessor.h"
  22. #include "src/gpu/GrPaint.h"
  23. #include "src/gpu/GrRecordingContextPriv.h"
  24. #include "src/gpu/GrRenderTargetContext.h"
  25. #include "src/gpu/GrTextureProxy.h"
  26. #include "src/gpu/SkGr.h"
  27. #include "src/gpu/effects/GrTextureDomain.h"
  28. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  29. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  30. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  31. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  32. class GrGLDiffuseLightingEffect;
  33. class GrGLSpecularLightingEffect;
  34. // For brevity
  35. typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
  36. #endif
  37. const SkScalar gOneThird = SkIntToScalar(1) / 3;
  38. const SkScalar gTwoThirds = SkIntToScalar(2) / 3;
  39. const SkScalar gOneHalf = 0.5f;
  40. const SkScalar gOneQuarter = 0.25f;
  41. #if SK_SUPPORT_GPU
  42. static void setUniformPoint3(const GrGLSLProgramDataManager& pdman, UniformHandle uni,
  43. const SkPoint3& point) {
  44. GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(float));
  45. pdman.set3fv(uni, 1, &point.fX);
  46. }
  47. static void setUniformNormal3(const GrGLSLProgramDataManager& pdman, UniformHandle uni,
  48. const SkPoint3& point) {
  49. setUniformPoint3(pdman, uni, point);
  50. }
  51. #endif
  52. // Shift matrix components to the left, as we advance pixels to the right.
  53. static inline void shiftMatrixLeft(int m[9]) {
  54. m[0] = m[1];
  55. m[3] = m[4];
  56. m[6] = m[7];
  57. m[1] = m[2];
  58. m[4] = m[5];
  59. m[7] = m[8];
  60. }
  61. static inline void fast_normalize(SkPoint3* vector) {
  62. // add a tiny bit so we don't have to worry about divide-by-zero
  63. SkScalar magSq = vector->dot(*vector) + SK_ScalarNearlyZero;
  64. SkScalar scale = sk_float_rsqrt(magSq);
  65. vector->fX *= scale;
  66. vector->fY *= scale;
  67. vector->fZ *= scale;
  68. }
  69. static SkPoint3 read_point3(SkReadBuffer& buffer) {
  70. SkPoint3 point;
  71. point.fX = buffer.readScalar();
  72. point.fY = buffer.readScalar();
  73. point.fZ = buffer.readScalar();
  74. buffer.validate(SkScalarIsFinite(point.fX) &&
  75. SkScalarIsFinite(point.fY) &&
  76. SkScalarIsFinite(point.fZ));
  77. return point;
  78. };
  79. static void write_point3(const SkPoint3& point, SkWriteBuffer& buffer) {
  80. buffer.writeScalar(point.fX);
  81. buffer.writeScalar(point.fY);
  82. buffer.writeScalar(point.fZ);
  83. };
  84. class GrGLLight;
  85. class SkImageFilterLight : public SkRefCnt {
  86. public:
  87. enum LightType {
  88. kDistant_LightType,
  89. kPoint_LightType,
  90. kSpot_LightType,
  91. kLast_LightType = kSpot_LightType
  92. };
  93. virtual LightType type() const = 0;
  94. const SkPoint3& color() const { return fColor; }
  95. virtual GrGLLight* createGLLight() const = 0;
  96. virtual bool isEqual(const SkImageFilterLight& other) const {
  97. return fColor == other.fColor;
  98. }
  99. virtual SkImageFilterLight* transform(const SkMatrix& matrix) const = 0;
  100. // Defined below SkLight's subclasses.
  101. void flattenLight(SkWriteBuffer& buffer) const;
  102. static SkImageFilterLight* UnflattenLight(SkReadBuffer& buffer);
  103. virtual SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const = 0;
  104. virtual SkPoint3 lightColor(const SkPoint3& surfaceToLight) const = 0;
  105. protected:
  106. SkImageFilterLight(SkColor color) {
  107. fColor = SkPoint3::Make(SkIntToScalar(SkColorGetR(color)),
  108. SkIntToScalar(SkColorGetG(color)),
  109. SkIntToScalar(SkColorGetB(color)));
  110. }
  111. SkImageFilterLight(const SkPoint3& color) : fColor(color) {}
  112. SkImageFilterLight(SkReadBuffer& buffer) {
  113. fColor = read_point3(buffer);
  114. }
  115. virtual void onFlattenLight(SkWriteBuffer& buffer) const = 0;
  116. private:
  117. typedef SkRefCnt INHERITED;
  118. SkPoint3 fColor;
  119. };
  120. class BaseLightingType {
  121. public:
  122. BaseLightingType() {}
  123. virtual ~BaseLightingType() {}
  124. virtual SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight,
  125. const SkPoint3& lightColor) const= 0;
  126. };
  127. class DiffuseLightingType : public BaseLightingType {
  128. public:
  129. DiffuseLightingType(SkScalar kd)
  130. : fKD(kd) {}
  131. SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight,
  132. const SkPoint3& lightColor) const override {
  133. SkScalar colorScale = fKD * normal.dot(surfaceTolight);
  134. colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
  135. SkPoint3 color = lightColor.makeScale(colorScale);
  136. return SkPackARGB32(255,
  137. SkClampMax(SkScalarRoundToInt(color.fX), 255),
  138. SkClampMax(SkScalarRoundToInt(color.fY), 255),
  139. SkClampMax(SkScalarRoundToInt(color.fZ), 255));
  140. }
  141. private:
  142. SkScalar fKD;
  143. };
  144. static SkScalar max_component(const SkPoint3& p) {
  145. return p.x() > p.y() ? (p.x() > p.z() ? p.x() : p.z()) : (p.y() > p.z() ? p.y() : p.z());
  146. }
  147. class SpecularLightingType : public BaseLightingType {
  148. public:
  149. SpecularLightingType(SkScalar ks, SkScalar shininess)
  150. : fKS(ks), fShininess(shininess) {}
  151. SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight,
  152. const SkPoint3& lightColor) const override {
  153. SkPoint3 halfDir(surfaceTolight);
  154. halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1)
  155. fast_normalize(&halfDir);
  156. SkScalar colorScale = fKS * SkScalarPow(normal.dot(halfDir), fShininess);
  157. colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
  158. SkPoint3 color = lightColor.makeScale(colorScale);
  159. return SkPackARGB32(SkClampMax(SkScalarRoundToInt(max_component(color)), 255),
  160. SkClampMax(SkScalarRoundToInt(color.fX), 255),
  161. SkClampMax(SkScalarRoundToInt(color.fY), 255),
  162. SkClampMax(SkScalarRoundToInt(color.fZ), 255));
  163. }
  164. private:
  165. SkScalar fKS;
  166. SkScalar fShininess;
  167. };
  168. static inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale) {
  169. return (-a + b - 2 * c + 2 * d -e + f) * scale;
  170. }
  171. static inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) {
  172. SkPoint3 vector = SkPoint3::Make(-x * surfaceScale, -y * surfaceScale, 1);
  173. fast_normalize(&vector);
  174. return vector;
  175. }
  176. static inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) {
  177. return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds),
  178. sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds),
  179. surfaceScale);
  180. }
  181. static inline SkPoint3 topNormal(int m[9], SkScalar surfaceScale) {
  182. return pointToNormal(sobel( 0, 0, m[3], m[5], m[6], m[8], gOneThird),
  183. sobel(m[3], m[6], m[4], m[7], m[5], m[8], gOneHalf),
  184. surfaceScale);
  185. }
  186. static inline SkPoint3 topRightNormal(int m[9], SkScalar surfaceScale) {
  187. return pointToNormal(sobel( 0, 0, m[3], m[4], m[6], m[7], gTwoThirds),
  188. sobel(m[3], m[6], m[4], m[7], 0, 0, gTwoThirds),
  189. surfaceScale);
  190. }
  191. static inline SkPoint3 leftNormal(int m[9], SkScalar surfaceScale) {
  192. return pointToNormal(sobel(m[1], m[2], m[4], m[5], m[7], m[8], gOneHalf),
  193. sobel( 0, 0, m[1], m[7], m[2], m[8], gOneThird),
  194. surfaceScale);
  195. }
  196. static inline SkPoint3 interiorNormal(int m[9], SkScalar surfaceScale) {
  197. return pointToNormal(sobel(m[0], m[2], m[3], m[5], m[6], m[8], gOneQuarter),
  198. sobel(m[0], m[6], m[1], m[7], m[2], m[8], gOneQuarter),
  199. surfaceScale);
  200. }
  201. static inline SkPoint3 rightNormal(int m[9], SkScalar surfaceScale) {
  202. return pointToNormal(sobel(m[0], m[1], m[3], m[4], m[6], m[7], gOneHalf),
  203. sobel(m[0], m[6], m[1], m[7], 0, 0, gOneThird),
  204. surfaceScale);
  205. }
  206. static inline SkPoint3 bottomLeftNormal(int m[9], SkScalar surfaceScale) {
  207. return pointToNormal(sobel(m[1], m[2], m[4], m[5], 0, 0, gTwoThirds),
  208. sobel( 0, 0, m[1], m[4], m[2], m[5], gTwoThirds),
  209. surfaceScale);
  210. }
  211. static inline SkPoint3 bottomNormal(int m[9], SkScalar surfaceScale) {
  212. return pointToNormal(sobel(m[0], m[2], m[3], m[5], 0, 0, gOneThird),
  213. sobel(m[0], m[3], m[1], m[4], m[2], m[5], gOneHalf),
  214. surfaceScale);
  215. }
  216. static inline SkPoint3 bottomRightNormal(int m[9], SkScalar surfaceScale) {
  217. return pointToNormal(sobel(m[0], m[1], m[3], m[4], 0, 0, gTwoThirds),
  218. sobel(m[0], m[3], m[1], m[4], 0, 0, gTwoThirds),
  219. surfaceScale);
  220. }
  221. class UncheckedPixelFetcher {
  222. public:
  223. static inline uint32_t Fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
  224. return SkGetPackedA32(*src.getAddr32(x, y));
  225. }
  226. };
  227. // The DecalPixelFetcher is used when the destination crop rect exceeds the input bitmap bounds.
  228. class DecalPixelFetcher {
  229. public:
  230. static inline uint32_t Fetch(const SkBitmap& src, int x, int y, const SkIRect& bounds) {
  231. if (x < bounds.fLeft || x >= bounds.fRight || y < bounds.fTop || y >= bounds.fBottom) {
  232. return 0;
  233. } else {
  234. return SkGetPackedA32(*src.getAddr32(x, y));
  235. }
  236. }
  237. };
  238. template <class PixelFetcher>
  239. static void lightBitmap(const BaseLightingType& lightingType,
  240. const SkImageFilterLight* l,
  241. const SkBitmap& src,
  242. SkBitmap* dst,
  243. SkScalar surfaceScale,
  244. const SkIRect& bounds) {
  245. SkASSERT(dst->width() == bounds.width() && dst->height() == bounds.height());
  246. int left = bounds.left(), right = bounds.right();
  247. int bottom = bounds.bottom();
  248. int y = bounds.top();
  249. SkIRect srcBounds = src.bounds();
  250. SkPMColor* dptr = dst->getAddr32(0, 0);
  251. {
  252. int x = left;
  253. int m[9];
  254. m[4] = PixelFetcher::Fetch(src, x, y, srcBounds);
  255. m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds);
  256. m[7] = PixelFetcher::Fetch(src, x, y + 1, srcBounds);
  257. m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds);
  258. SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  259. *dptr++ = lightingType.light(topLeftNormal(m, surfaceScale), surfaceToLight,
  260. l->lightColor(surfaceToLight));
  261. for (++x; x < right - 1; ++x)
  262. {
  263. shiftMatrixLeft(m);
  264. m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds);
  265. m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds);
  266. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  267. *dptr++ = lightingType.light(topNormal(m, surfaceScale), surfaceToLight,
  268. l->lightColor(surfaceToLight));
  269. }
  270. shiftMatrixLeft(m);
  271. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  272. *dptr++ = lightingType.light(topRightNormal(m, surfaceScale), surfaceToLight,
  273. l->lightColor(surfaceToLight));
  274. }
  275. for (++y; y < bottom - 1; ++y) {
  276. int x = left;
  277. int m[9];
  278. m[1] = PixelFetcher::Fetch(src, x, y - 1, srcBounds);
  279. m[2] = PixelFetcher::Fetch(src, x + 1, y - 1, srcBounds);
  280. m[4] = PixelFetcher::Fetch(src, x, y, srcBounds);
  281. m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds);
  282. m[7] = PixelFetcher::Fetch(src, x, y + 1, srcBounds);
  283. m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds);
  284. SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  285. *dptr++ = lightingType.light(leftNormal(m, surfaceScale), surfaceToLight,
  286. l->lightColor(surfaceToLight));
  287. for (++x; x < right - 1; ++x) {
  288. shiftMatrixLeft(m);
  289. m[2] = PixelFetcher::Fetch(src, x + 1, y - 1, srcBounds);
  290. m[5] = PixelFetcher::Fetch(src, x + 1, y, srcBounds);
  291. m[8] = PixelFetcher::Fetch(src, x + 1, y + 1, srcBounds);
  292. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  293. *dptr++ = lightingType.light(interiorNormal(m, surfaceScale), surfaceToLight,
  294. l->lightColor(surfaceToLight));
  295. }
  296. shiftMatrixLeft(m);
  297. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  298. *dptr++ = lightingType.light(rightNormal(m, surfaceScale), surfaceToLight,
  299. l->lightColor(surfaceToLight));
  300. }
  301. {
  302. int x = left;
  303. int m[9];
  304. m[1] = PixelFetcher::Fetch(src, x, bottom - 2, srcBounds);
  305. m[2] = PixelFetcher::Fetch(src, x + 1, bottom - 2, srcBounds);
  306. m[4] = PixelFetcher::Fetch(src, x, bottom - 1, srcBounds);
  307. m[5] = PixelFetcher::Fetch(src, x + 1, bottom - 1, srcBounds);
  308. SkPoint3 surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  309. *dptr++ = lightingType.light(bottomLeftNormal(m, surfaceScale), surfaceToLight,
  310. l->lightColor(surfaceToLight));
  311. for (++x; x < right - 1; ++x)
  312. {
  313. shiftMatrixLeft(m);
  314. m[2] = PixelFetcher::Fetch(src, x + 1, bottom - 2, srcBounds);
  315. m[5] = PixelFetcher::Fetch(src, x + 1, bottom - 1, srcBounds);
  316. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  317. *dptr++ = lightingType.light(bottomNormal(m, surfaceScale), surfaceToLight,
  318. l->lightColor(surfaceToLight));
  319. }
  320. shiftMatrixLeft(m);
  321. surfaceToLight = l->surfaceToLight(x, y, m[4], surfaceScale);
  322. *dptr++ = lightingType.light(bottomRightNormal(m, surfaceScale), surfaceToLight,
  323. l->lightColor(surfaceToLight));
  324. }
  325. }
  326. static void lightBitmap(const BaseLightingType& lightingType,
  327. const SkImageFilterLight* light,
  328. const SkBitmap& src,
  329. SkBitmap* dst,
  330. SkScalar surfaceScale,
  331. const SkIRect& bounds) {
  332. if (src.bounds().contains(bounds)) {
  333. lightBitmap<UncheckedPixelFetcher>(
  334. lightingType, light, src, dst, surfaceScale, bounds);
  335. } else {
  336. lightBitmap<DecalPixelFetcher>(
  337. lightingType, light, src, dst, surfaceScale, bounds);
  338. }
  339. }
  340. enum BoundaryMode {
  341. kTopLeft_BoundaryMode,
  342. kTop_BoundaryMode,
  343. kTopRight_BoundaryMode,
  344. kLeft_BoundaryMode,
  345. kInterior_BoundaryMode,
  346. kRight_BoundaryMode,
  347. kBottomLeft_BoundaryMode,
  348. kBottom_BoundaryMode,
  349. kBottomRight_BoundaryMode,
  350. kBoundaryModeCount,
  351. };
  352. class SkLightingImageFilterInternal : public SkLightingImageFilter {
  353. protected:
  354. SkLightingImageFilterInternal(sk_sp<SkImageFilterLight> light,
  355. SkScalar surfaceScale,
  356. sk_sp<SkImageFilter> input,
  357. const CropRect* cropRect)
  358. : INHERITED(std::move(light), surfaceScale, std::move(input), cropRect) {
  359. }
  360. #if SK_SUPPORT_GPU
  361. sk_sp<SkSpecialImage> filterImageGPU(SkSpecialImage* source,
  362. SkSpecialImage* input,
  363. const SkIRect& bounds,
  364. const SkMatrix& matrix,
  365. const OutputProperties& outputProperties) const;
  366. virtual std::unique_ptr<GrFragmentProcessor> makeFragmentProcessor(
  367. sk_sp<GrTextureProxy>,
  368. const SkMatrix&,
  369. const SkIRect* srcBounds,
  370. BoundaryMode boundaryMode) const = 0;
  371. #endif
  372. private:
  373. #if SK_SUPPORT_GPU
  374. void drawRect(GrRenderTargetContext*,
  375. sk_sp<GrTextureProxy> srcProxy,
  376. const SkMatrix& matrix,
  377. const GrClip& clip,
  378. const SkRect& dstRect,
  379. BoundaryMode boundaryMode,
  380. const SkIRect* srcBounds,
  381. const SkIRect& bounds) const;
  382. #endif
  383. typedef SkLightingImageFilter INHERITED;
  384. };
  385. #if SK_SUPPORT_GPU
  386. void SkLightingImageFilterInternal::drawRect(GrRenderTargetContext* renderTargetContext,
  387. sk_sp<GrTextureProxy> srcProxy,
  388. const SkMatrix& matrix,
  389. const GrClip& clip,
  390. const SkRect& dstRect,
  391. BoundaryMode boundaryMode,
  392. const SkIRect* srcBounds,
  393. const SkIRect& bounds) const {
  394. SkRect srcRect = dstRect.makeOffset(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y()));
  395. GrPaint paint;
  396. auto fp = this->makeFragmentProcessor(std::move(srcProxy), matrix, srcBounds, boundaryMode);
  397. paint.addColorFragmentProcessor(std::move(fp));
  398. paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
  399. renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
  400. srcRect);
  401. }
  402. sk_sp<SkSpecialImage> SkLightingImageFilterInternal::filterImageGPU(
  403. SkSpecialImage* source,
  404. SkSpecialImage* input,
  405. const SkIRect& offsetBounds,
  406. const SkMatrix& matrix,
  407. const OutputProperties& outputProperties) const {
  408. SkASSERT(source->isTextureBacked());
  409. auto context = source->getContext();
  410. sk_sp<GrTextureProxy> inputProxy(input->asTextureProxyRef(context));
  411. SkASSERT(inputProxy);
  412. GrColorType colorType = SkColorTypeToGrColorType(outputProperties.colorType());
  413. sk_sp<GrRenderTargetContext> renderTargetContext(
  414. context->priv().makeDeferredRenderTargetContext(
  415. SkBackingFit::kApprox,
  416. offsetBounds.width(),
  417. offsetBounds.height(),
  418. colorType,
  419. sk_ref_sp(outputProperties.colorSpace()),
  420. 1,
  421. GrMipMapped::kNo,
  422. kBottomLeft_GrSurfaceOrigin,
  423. nullptr,
  424. SkBudgeted::kYes,
  425. inputProxy->isProtected() ? GrProtected::kYes : GrProtected::kNo));
  426. if (!renderTargetContext) {
  427. return nullptr;
  428. }
  429. SkIRect dstIRect = SkIRect::MakeWH(offsetBounds.width(), offsetBounds.height());
  430. SkRect dstRect = SkRect::Make(dstIRect);
  431. // setup new clip
  432. GrFixedClip clip(dstIRect);
  433. const SkIRect inputBounds = SkIRect::MakeWH(input->width(), input->height());
  434. SkRect topLeft = SkRect::MakeXYWH(0, 0, 1, 1);
  435. SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1);
  436. SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1);
  437. SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2);
  438. SkRect interior = dstRect.makeInset(1, 1);
  439. SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2);
  440. SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1);
  441. SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1);
  442. SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1);
  443. const SkIRect* pSrcBounds = inputBounds.contains(offsetBounds) ? nullptr : &inputBounds;
  444. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, topLeft,
  445. kTopLeft_BoundaryMode, pSrcBounds, offsetBounds);
  446. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, top,
  447. kTop_BoundaryMode, pSrcBounds, offsetBounds);
  448. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, topRight,
  449. kTopRight_BoundaryMode, pSrcBounds, offsetBounds);
  450. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, left,
  451. kLeft_BoundaryMode, pSrcBounds, offsetBounds);
  452. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, interior,
  453. kInterior_BoundaryMode, pSrcBounds, offsetBounds);
  454. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, right,
  455. kRight_BoundaryMode, pSrcBounds, offsetBounds);
  456. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, bottomLeft,
  457. kBottomLeft_BoundaryMode, pSrcBounds, offsetBounds);
  458. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, bottom,
  459. kBottom_BoundaryMode, pSrcBounds, offsetBounds);
  460. this->drawRect(renderTargetContext.get(), inputProxy, matrix, clip, bottomRight,
  461. kBottomRight_BoundaryMode, pSrcBounds, offsetBounds);
  462. return SkSpecialImage::MakeDeferredFromGpu(
  463. context,
  464. SkIRect::MakeWH(offsetBounds.width(), offsetBounds.height()),
  465. kNeedNewImageUniqueID_SpecialImage,
  466. renderTargetContext->asTextureProxyRef(),
  467. renderTargetContext->colorSpaceInfo().refColorSpace());
  468. }
  469. #endif
  470. class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal {
  471. public:
  472. static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light,
  473. SkScalar surfaceScale,
  474. SkScalar kd,
  475. sk_sp<SkImageFilter>,
  476. const CropRect*);
  477. SkScalar kd() const { return fKD; }
  478. protected:
  479. SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light, SkScalar surfaceScale,
  480. SkScalar kd,
  481. sk_sp<SkImageFilter> input, const CropRect* cropRect);
  482. void flatten(SkWriteBuffer& buffer) const override;
  483. sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
  484. SkIPoint* offset) const override;
  485. #if SK_SUPPORT_GPU
  486. std::unique_ptr<GrFragmentProcessor> makeFragmentProcessor(sk_sp<GrTextureProxy>,
  487. const SkMatrix&,
  488. const SkIRect* bounds,
  489. BoundaryMode) const override;
  490. #endif
  491. private:
  492. SK_FLATTENABLE_HOOKS(SkDiffuseLightingImageFilter)
  493. friend class SkLightingImageFilter;
  494. SkScalar fKD;
  495. typedef SkLightingImageFilterInternal INHERITED;
  496. };
  497. class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal {
  498. public:
  499. static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light,
  500. SkScalar surfaceScale,
  501. SkScalar ks, SkScalar shininess,
  502. sk_sp<SkImageFilter>, const CropRect*);
  503. SkScalar ks() const { return fKS; }
  504. SkScalar shininess() const { return fShininess; }
  505. protected:
  506. SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light,
  507. SkScalar surfaceScale, SkScalar ks,
  508. SkScalar shininess,
  509. sk_sp<SkImageFilter> input, const CropRect*);
  510. void flatten(SkWriteBuffer& buffer) const override;
  511. sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
  512. SkIPoint* offset) const override;
  513. #if SK_SUPPORT_GPU
  514. std::unique_ptr<GrFragmentProcessor> makeFragmentProcessor(sk_sp<GrTextureProxy>,
  515. const SkMatrix&,
  516. const SkIRect* bounds,
  517. BoundaryMode) const override;
  518. #endif
  519. private:
  520. SK_FLATTENABLE_HOOKS(SkSpecularLightingImageFilter)
  521. SkScalar fKS;
  522. SkScalar fShininess;
  523. friend class SkLightingImageFilter;
  524. typedef SkLightingImageFilterInternal INHERITED;
  525. };
  526. #if SK_SUPPORT_GPU
  527. class GrLightingEffect : public GrFragmentProcessor {
  528. public:
  529. const SkImageFilterLight* light() const { return fLight.get(); }
  530. SkScalar surfaceScale() const { return fSurfaceScale; }
  531. const SkMatrix& filterMatrix() const { return fFilterMatrix; }
  532. BoundaryMode boundaryMode() const { return fBoundaryMode; }
  533. const GrTextureDomain& domain() const { return fDomain; }
  534. protected:
  535. GrLightingEffect(ClassID classID, sk_sp<GrTextureProxy>, sk_sp<const SkImageFilterLight> light,
  536. SkScalar surfaceScale, const SkMatrix& matrix, BoundaryMode boundaryMode,
  537. const SkIRect* srcBounds);
  538. GrLightingEffect(const GrLightingEffect& that);
  539. bool onIsEqual(const GrFragmentProcessor&) const override;
  540. private:
  541. const TextureSampler& onTextureSampler(int) const override { return fTextureSampler; }
  542. GrCoordTransform fCoordTransform;
  543. GrTextureDomain fDomain;
  544. TextureSampler fTextureSampler;
  545. sk_sp<const SkImageFilterLight> fLight;
  546. SkScalar fSurfaceScale;
  547. SkMatrix fFilterMatrix;
  548. BoundaryMode fBoundaryMode;
  549. typedef GrFragmentProcessor INHERITED;
  550. };
  551. class GrDiffuseLightingEffect : public GrLightingEffect {
  552. public:
  553. static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
  554. sk_sp<const SkImageFilterLight> light,
  555. SkScalar surfaceScale,
  556. const SkMatrix& matrix,
  557. SkScalar kd,
  558. BoundaryMode boundaryMode,
  559. const SkIRect* srcBounds) {
  560. return std::unique_ptr<GrFragmentProcessor>(
  561. new GrDiffuseLightingEffect(std::move(proxy), std::move(light), surfaceScale,
  562. matrix, kd, boundaryMode, srcBounds));
  563. }
  564. const char* name() const override { return "DiffuseLighting"; }
  565. std::unique_ptr<GrFragmentProcessor> clone() const override {
  566. return std::unique_ptr<GrFragmentProcessor>(new GrDiffuseLightingEffect(*this));
  567. }
  568. SkScalar kd() const { return fKD; }
  569. private:
  570. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
  571. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
  572. bool onIsEqual(const GrFragmentProcessor&) const override;
  573. GrDiffuseLightingEffect(sk_sp<GrTextureProxy>,
  574. sk_sp<const SkImageFilterLight> light,
  575. SkScalar surfaceScale,
  576. const SkMatrix& matrix,
  577. SkScalar kd,
  578. BoundaryMode boundaryMode,
  579. const SkIRect* srcBounds);
  580. explicit GrDiffuseLightingEffect(const GrDiffuseLightingEffect& that);
  581. GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  582. SkScalar fKD;
  583. typedef GrLightingEffect INHERITED;
  584. };
  585. class GrSpecularLightingEffect : public GrLightingEffect {
  586. public:
  587. static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
  588. sk_sp<const SkImageFilterLight> light,
  589. SkScalar surfaceScale,
  590. const SkMatrix& matrix,
  591. SkScalar ks,
  592. SkScalar shininess,
  593. BoundaryMode boundaryMode,
  594. const SkIRect* srcBounds) {
  595. return std::unique_ptr<GrFragmentProcessor>(
  596. new GrSpecularLightingEffect(std::move(proxy), std::move(light), surfaceScale,
  597. matrix, ks, shininess, boundaryMode, srcBounds));
  598. }
  599. const char* name() const override { return "SpecularLighting"; }
  600. std::unique_ptr<GrFragmentProcessor> clone() const override {
  601. return std::unique_ptr<GrFragmentProcessor>(new GrSpecularLightingEffect(*this));
  602. }
  603. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
  604. SkScalar ks() const { return fKS; }
  605. SkScalar shininess() const { return fShininess; }
  606. private:
  607. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
  608. bool onIsEqual(const GrFragmentProcessor&) const override;
  609. GrSpecularLightingEffect(sk_sp<GrTextureProxy>,
  610. sk_sp<const SkImageFilterLight> light,
  611. SkScalar surfaceScale,
  612. const SkMatrix& matrix,
  613. SkScalar ks,
  614. SkScalar shininess,
  615. BoundaryMode boundaryMode,
  616. const SkIRect* srcBounds);
  617. explicit GrSpecularLightingEffect(const GrSpecularLightingEffect&);
  618. GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  619. SkScalar fKS;
  620. SkScalar fShininess;
  621. typedef GrLightingEffect INHERITED;
  622. };
  623. ///////////////////////////////////////////////////////////////////////////////
  624. class GrGLLight {
  625. public:
  626. virtual ~GrGLLight() {}
  627. /**
  628. * This is called by GrGLLightingEffect::emitCode() before either of the two virtual functions
  629. * below. It adds a half3 uniform visible in the FS that represents the constant light color.
  630. */
  631. void emitLightColorUniform(GrGLSLUniformHandler*);
  632. /**
  633. * These two functions are called from GrGLLightingEffect's emitCode() function.
  634. * emitSurfaceToLight places an expression in param out that is the vector from the surface to
  635. * the light. The expression will be used in the FS. emitLightColor writes an expression into
  636. * the FS that is the color of the light. Either function may add functions and/or uniforms to
  637. * the FS. The default of emitLightColor appends the name of the constant light color uniform
  638. * and so this function only needs to be overridden if the light color varies spatially.
  639. */
  640. virtual void emitSurfaceToLight(GrGLSLUniformHandler*,
  641. GrGLSLFPFragmentBuilder*,
  642. const char* z) = 0;
  643. virtual void emitLightColor(GrGLSLUniformHandler*,
  644. GrGLSLFPFragmentBuilder*,
  645. const char *surfaceToLight);
  646. // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLight must call
  647. // INHERITED::setData().
  648. virtual void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const;
  649. protected:
  650. /**
  651. * Gets the constant light color uniform. Subclasses can use this in their emitLightColor
  652. * function.
  653. */
  654. UniformHandle lightColorUni() const { return fColorUni; }
  655. private:
  656. UniformHandle fColorUni;
  657. typedef SkRefCnt INHERITED;
  658. };
  659. ///////////////////////////////////////////////////////////////////////////////
  660. class GrGLDistantLight : public GrGLLight {
  661. public:
  662. ~GrGLDistantLight() override {}
  663. void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override;
  664. void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override;
  665. private:
  666. typedef GrGLLight INHERITED;
  667. UniformHandle fDirectionUni;
  668. };
  669. ///////////////////////////////////////////////////////////////////////////////
  670. class GrGLPointLight : public GrGLLight {
  671. public:
  672. ~GrGLPointLight() override {}
  673. void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override;
  674. void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override;
  675. private:
  676. typedef GrGLLight INHERITED;
  677. UniformHandle fLocationUni;
  678. };
  679. ///////////////////////////////////////////////////////////////////////////////
  680. class GrGLSpotLight : public GrGLLight {
  681. public:
  682. ~GrGLSpotLight() override {}
  683. void setData(const GrGLSLProgramDataManager&, const SkImageFilterLight* light) const override;
  684. void emitSurfaceToLight(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, const char* z) override;
  685. void emitLightColor(GrGLSLUniformHandler*,
  686. GrGLSLFPFragmentBuilder*,
  687. const char *surfaceToLight) override;
  688. private:
  689. typedef GrGLLight INHERITED;
  690. SkString fLightColorFunc;
  691. UniformHandle fLocationUni;
  692. UniformHandle fExponentUni;
  693. UniformHandle fCosOuterConeAngleUni;
  694. UniformHandle fCosInnerConeAngleUni;
  695. UniformHandle fConeScaleUni;
  696. UniformHandle fSUni;
  697. };
  698. #else
  699. class GrGLLight;
  700. #endif
  701. ///////////////////////////////////////////////////////////////////////////////
  702. ///////////////////////////////////////////////////////////////////////////////
  703. class SkDistantLight : public SkImageFilterLight {
  704. public:
  705. SkDistantLight(const SkPoint3& direction, SkColor color)
  706. : INHERITED(color), fDirection(direction) {
  707. }
  708. SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const override {
  709. return fDirection;
  710. }
  711. SkPoint3 lightColor(const SkPoint3&) const override { return this->color(); }
  712. LightType type() const override { return kDistant_LightType; }
  713. const SkPoint3& direction() const { return fDirection; }
  714. GrGLLight* createGLLight() const override {
  715. #if SK_SUPPORT_GPU
  716. return new GrGLDistantLight;
  717. #else
  718. SkDEBUGFAIL("Should not call in GPU-less build");
  719. return nullptr;
  720. #endif
  721. }
  722. bool isEqual(const SkImageFilterLight& other) const override {
  723. if (other.type() != kDistant_LightType) {
  724. return false;
  725. }
  726. const SkDistantLight& o = static_cast<const SkDistantLight&>(other);
  727. return INHERITED::isEqual(other) &&
  728. fDirection == o.fDirection;
  729. }
  730. SkDistantLight(SkReadBuffer& buffer) : INHERITED(buffer) {
  731. fDirection = read_point3(buffer);
  732. }
  733. protected:
  734. SkDistantLight(const SkPoint3& direction, const SkPoint3& color)
  735. : INHERITED(color), fDirection(direction) {
  736. }
  737. SkImageFilterLight* transform(const SkMatrix& matrix) const override {
  738. return new SkDistantLight(direction(), color());
  739. }
  740. void onFlattenLight(SkWriteBuffer& buffer) const override {
  741. write_point3(fDirection, buffer);
  742. }
  743. private:
  744. SkPoint3 fDirection;
  745. typedef SkImageFilterLight INHERITED;
  746. };
  747. ///////////////////////////////////////////////////////////////////////////////
  748. class SkPointLight : public SkImageFilterLight {
  749. public:
  750. SkPointLight(const SkPoint3& location, SkColor color)
  751. : INHERITED(color), fLocation(location) {}
  752. SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const override {
  753. SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x),
  754. fLocation.fY - SkIntToScalar(y),
  755. fLocation.fZ - SkIntToScalar(z) * surfaceScale);
  756. fast_normalize(&direction);
  757. return direction;
  758. }
  759. SkPoint3 lightColor(const SkPoint3&) const override { return this->color(); }
  760. LightType type() const override { return kPoint_LightType; }
  761. const SkPoint3& location() const { return fLocation; }
  762. GrGLLight* createGLLight() const override {
  763. #if SK_SUPPORT_GPU
  764. return new GrGLPointLight;
  765. #else
  766. SkDEBUGFAIL("Should not call in GPU-less build");
  767. return nullptr;
  768. #endif
  769. }
  770. bool isEqual(const SkImageFilterLight& other) const override {
  771. if (other.type() != kPoint_LightType) {
  772. return false;
  773. }
  774. const SkPointLight& o = static_cast<const SkPointLight&>(other);
  775. return INHERITED::isEqual(other) &&
  776. fLocation == o.fLocation;
  777. }
  778. SkImageFilterLight* transform(const SkMatrix& matrix) const override {
  779. SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY);
  780. matrix.mapPoints(&location2, 1);
  781. // Use X scale and Y scale on Z and average the result
  782. SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ);
  783. matrix.mapVectors(&locationZ, 1);
  784. SkPoint3 location = SkPoint3::Make(location2.fX,
  785. location2.fY,
  786. SkScalarAve(locationZ.fX, locationZ.fY));
  787. return new SkPointLight(location, color());
  788. }
  789. SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) {
  790. fLocation = read_point3(buffer);
  791. }
  792. protected:
  793. SkPointLight(const SkPoint3& location, const SkPoint3& color)
  794. : INHERITED(color), fLocation(location) {}
  795. void onFlattenLight(SkWriteBuffer& buffer) const override {
  796. write_point3(fLocation, buffer);
  797. }
  798. private:
  799. SkPoint3 fLocation;
  800. typedef SkImageFilterLight INHERITED;
  801. };
  802. ///////////////////////////////////////////////////////////////////////////////
  803. class SkSpotLight : public SkImageFilterLight {
  804. public:
  805. SkSpotLight(const SkPoint3& location,
  806. const SkPoint3& target,
  807. SkScalar specularExponent,
  808. SkScalar cutoffAngle,
  809. SkColor color)
  810. : INHERITED(color),
  811. fLocation(location),
  812. fTarget(target),
  813. fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSpecularExponentMax))
  814. {
  815. fS = target - location;
  816. fast_normalize(&fS);
  817. fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle));
  818. const SkScalar antiAliasThreshold = 0.016f;
  819. fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold;
  820. fConeScale = SkScalarInvert(antiAliasThreshold);
  821. }
  822. SkImageFilterLight* transform(const SkMatrix& matrix) const override {
  823. SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY);
  824. matrix.mapPoints(&location2, 1);
  825. // Use X scale and Y scale on Z and average the result
  826. SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ);
  827. matrix.mapVectors(&locationZ, 1);
  828. SkPoint3 location = SkPoint3::Make(location2.fX, location2.fY,
  829. SkScalarAve(locationZ.fX, locationZ.fY));
  830. SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY);
  831. matrix.mapPoints(&target2, 1);
  832. SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ);
  833. matrix.mapVectors(&targetZ, 1);
  834. SkPoint3 target = SkPoint3::Make(target2.fX, target2.fY,
  835. SkScalarAve(targetZ.fX, targetZ.fY));
  836. SkPoint3 s = target - location;
  837. fast_normalize(&s);
  838. return new SkSpotLight(location,
  839. target,
  840. fSpecularExponent,
  841. fCosOuterConeAngle,
  842. fCosInnerConeAngle,
  843. fConeScale,
  844. s,
  845. color());
  846. }
  847. SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const override {
  848. SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x),
  849. fLocation.fY - SkIntToScalar(y),
  850. fLocation.fZ - SkIntToScalar(z) * surfaceScale);
  851. fast_normalize(&direction);
  852. return direction;
  853. }
  854. SkPoint3 lightColor(const SkPoint3& surfaceToLight) const override {
  855. SkScalar cosAngle = -surfaceToLight.dot(fS);
  856. SkScalar scale = 0;
  857. if (cosAngle >= fCosOuterConeAngle) {
  858. scale = SkScalarPow(cosAngle, fSpecularExponent);
  859. if (cosAngle < fCosInnerConeAngle) {
  860. scale *= (cosAngle - fCosOuterConeAngle) * fConeScale;
  861. }
  862. }
  863. return this->color().makeScale(scale);
  864. }
  865. GrGLLight* createGLLight() const override {
  866. #if SK_SUPPORT_GPU
  867. return new GrGLSpotLight;
  868. #else
  869. SkDEBUGFAIL("Should not call in GPU-less build");
  870. return nullptr;
  871. #endif
  872. }
  873. LightType type() const override { return kSpot_LightType; }
  874. const SkPoint3& location() const { return fLocation; }
  875. const SkPoint3& target() const { return fTarget; }
  876. SkScalar specularExponent() const { return fSpecularExponent; }
  877. SkScalar cosInnerConeAngle() const { return fCosInnerConeAngle; }
  878. SkScalar cosOuterConeAngle() const { return fCosOuterConeAngle; }
  879. SkScalar coneScale() const { return fConeScale; }
  880. const SkPoint3& s() const { return fS; }
  881. SkSpotLight(SkReadBuffer& buffer) : INHERITED(buffer) {
  882. fLocation = read_point3(buffer);
  883. fTarget = read_point3(buffer);
  884. fSpecularExponent = buffer.readScalar();
  885. fCosOuterConeAngle = buffer.readScalar();
  886. fCosInnerConeAngle = buffer.readScalar();
  887. fConeScale = buffer.readScalar();
  888. fS = read_point3(buffer);
  889. buffer.validate(SkScalarIsFinite(fSpecularExponent) &&
  890. SkScalarIsFinite(fCosOuterConeAngle) &&
  891. SkScalarIsFinite(fCosInnerConeAngle) &&
  892. SkScalarIsFinite(fConeScale));
  893. }
  894. protected:
  895. SkSpotLight(const SkPoint3& location,
  896. const SkPoint3& target,
  897. SkScalar specularExponent,
  898. SkScalar cosOuterConeAngle,
  899. SkScalar cosInnerConeAngle,
  900. SkScalar coneScale,
  901. const SkPoint3& s,
  902. const SkPoint3& color)
  903. : INHERITED(color),
  904. fLocation(location),
  905. fTarget(target),
  906. fSpecularExponent(specularExponent),
  907. fCosOuterConeAngle(cosOuterConeAngle),
  908. fCosInnerConeAngle(cosInnerConeAngle),
  909. fConeScale(coneScale),
  910. fS(s)
  911. {
  912. }
  913. void onFlattenLight(SkWriteBuffer& buffer) const override {
  914. write_point3(fLocation, buffer);
  915. write_point3(fTarget, buffer);
  916. buffer.writeScalar(fSpecularExponent);
  917. buffer.writeScalar(fCosOuterConeAngle);
  918. buffer.writeScalar(fCosInnerConeAngle);
  919. buffer.writeScalar(fConeScale);
  920. write_point3(fS, buffer);
  921. }
  922. bool isEqual(const SkImageFilterLight& other) const override {
  923. if (other.type() != kSpot_LightType) {
  924. return false;
  925. }
  926. const SkSpotLight& o = static_cast<const SkSpotLight&>(other);
  927. return INHERITED::isEqual(other) &&
  928. fLocation == o.fLocation &&
  929. fTarget == o.fTarget &&
  930. fSpecularExponent == o.fSpecularExponent &&
  931. fCosOuterConeAngle == o.fCosOuterConeAngle;
  932. }
  933. private:
  934. static const SkScalar kSpecularExponentMin;
  935. static const SkScalar kSpecularExponentMax;
  936. SkPoint3 fLocation;
  937. SkPoint3 fTarget;
  938. SkScalar fSpecularExponent;
  939. SkScalar fCosOuterConeAngle;
  940. SkScalar fCosInnerConeAngle;
  941. SkScalar fConeScale;
  942. SkPoint3 fS;
  943. typedef SkImageFilterLight INHERITED;
  944. };
  945. // According to the spec, the specular term should be in the range [1, 128] :
  946. // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttribute
  947. const SkScalar SkSpotLight::kSpecularExponentMin = 1.0f;
  948. const SkScalar SkSpotLight::kSpecularExponentMax = 128.0f;
  949. ///////////////////////////////////////////////////////////////////////////////
  950. void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const {
  951. // Write type first, then baseclass, then subclass.
  952. buffer.writeInt(this->type());
  953. write_point3(fColor, buffer);
  954. this->onFlattenLight(buffer);
  955. }
  956. /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer& buffer) {
  957. SkImageFilterLight::LightType type = buffer.read32LE(SkImageFilterLight::kLast_LightType);
  958. switch (type) {
  959. // Each of these constructors must first call SkLight's, so we'll read the baseclass
  960. // then subclass, same order as flattenLight.
  961. case SkImageFilterLight::kDistant_LightType:
  962. return new SkDistantLight(buffer);
  963. case SkImageFilterLight::kPoint_LightType:
  964. return new SkPointLight(buffer);
  965. case SkImageFilterLight::kSpot_LightType:
  966. return new SkSpotLight(buffer);
  967. default:
  968. // Should never get here due to prior check of SkSafeRange
  969. SkDEBUGFAIL("Unknown LightType.");
  970. return nullptr;
  971. }
  972. }
  973. ///////////////////////////////////////////////////////////////////////////////
  974. SkLightingImageFilter::SkLightingImageFilter(sk_sp<SkImageFilterLight> light,
  975. SkScalar surfaceScale,
  976. sk_sp<SkImageFilter> input, const CropRect* cropRect)
  977. : INHERITED(&input, 1, cropRect)
  978. , fLight(std::move(light))
  979. , fSurfaceScale(surfaceScale / 255) {
  980. }
  981. SkLightingImageFilter::~SkLightingImageFilter() {}
  982. sk_sp<SkImageFilter> SkLightingImageFilter::MakeDistantLitDiffuse(const SkPoint3& direction,
  983. SkColor lightColor,
  984. SkScalar surfaceScale,
  985. SkScalar kd,
  986. sk_sp<SkImageFilter> input,
  987. const CropRect* cropRect) {
  988. sk_sp<SkImageFilterLight> light(new SkDistantLight(direction, lightColor));
  989. return SkDiffuseLightingImageFilter::Make(std::move(light), surfaceScale, kd,
  990. std::move(input), cropRect);
  991. }
  992. sk_sp<SkImageFilter> SkLightingImageFilter::MakePointLitDiffuse(const SkPoint3& location,
  993. SkColor lightColor,
  994. SkScalar surfaceScale,
  995. SkScalar kd,
  996. sk_sp<SkImageFilter> input,
  997. const CropRect* cropRect) {
  998. sk_sp<SkImageFilterLight> light(new SkPointLight(location, lightColor));
  999. return SkDiffuseLightingImageFilter::Make(std::move(light), surfaceScale, kd,
  1000. std::move(input), cropRect);
  1001. }
  1002. sk_sp<SkImageFilter> SkLightingImageFilter::MakeSpotLitDiffuse(const SkPoint3& location,
  1003. const SkPoint3& target,
  1004. SkScalar specularExponent,
  1005. SkScalar cutoffAngle,
  1006. SkColor lightColor,
  1007. SkScalar surfaceScale,
  1008. SkScalar kd,
  1009. sk_sp<SkImageFilter> input,
  1010. const CropRect* cropRect) {
  1011. sk_sp<SkImageFilterLight> light(
  1012. new SkSpotLight(location, target, specularExponent, cutoffAngle, lightColor));
  1013. return SkDiffuseLightingImageFilter::Make(std::move(light), surfaceScale, kd,
  1014. std::move(input), cropRect);
  1015. }
  1016. sk_sp<SkImageFilter> SkLightingImageFilter::MakeDistantLitSpecular(const SkPoint3& direction,
  1017. SkColor lightColor,
  1018. SkScalar surfaceScale,
  1019. SkScalar ks,
  1020. SkScalar shine,
  1021. sk_sp<SkImageFilter> input,
  1022. const CropRect* cropRect) {
  1023. sk_sp<SkImageFilterLight> light(new SkDistantLight(direction, lightColor));
  1024. return SkSpecularLightingImageFilter::Make(std::move(light), surfaceScale, ks, shine,
  1025. std::move(input), cropRect);
  1026. }
  1027. sk_sp<SkImageFilter> SkLightingImageFilter::MakePointLitSpecular(const SkPoint3& location,
  1028. SkColor lightColor,
  1029. SkScalar surfaceScale,
  1030. SkScalar ks,
  1031. SkScalar shine,
  1032. sk_sp<SkImageFilter> input,
  1033. const CropRect* cropRect) {
  1034. sk_sp<SkImageFilterLight> light(new SkPointLight(location, lightColor));
  1035. return SkSpecularLightingImageFilter::Make(std::move(light), surfaceScale, ks, shine,
  1036. std::move(input), cropRect);
  1037. }
  1038. sk_sp<SkImageFilter> SkLightingImageFilter::MakeSpotLitSpecular(const SkPoint3& location,
  1039. const SkPoint3& target,
  1040. SkScalar specularExponent,
  1041. SkScalar cutoffAngle,
  1042. SkColor lightColor,
  1043. SkScalar surfaceScale,
  1044. SkScalar ks,
  1045. SkScalar shine,
  1046. sk_sp<SkImageFilter> input,
  1047. const CropRect* cropRect) {
  1048. sk_sp<SkImageFilterLight> light(
  1049. new SkSpotLight(location, target, specularExponent, cutoffAngle, lightColor));
  1050. return SkSpecularLightingImageFilter::Make(std::move(light), surfaceScale, ks, shine,
  1051. std::move(input), cropRect);
  1052. }
  1053. void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const {
  1054. this->INHERITED::flatten(buffer);
  1055. fLight->flattenLight(buffer);
  1056. buffer.writeScalar(fSurfaceScale * 255);
  1057. }
  1058. sk_sp<const SkImageFilterLight> SkLightingImageFilter::refLight() const { return fLight; }
  1059. ///////////////////////////////////////////////////////////////////////////////
  1060. sk_sp<SkImageFilter> SkDiffuseLightingImageFilter::Make(sk_sp<SkImageFilterLight> light,
  1061. SkScalar surfaceScale,
  1062. SkScalar kd,
  1063. sk_sp<SkImageFilter> input,
  1064. const CropRect* cropRect) {
  1065. if (!light) {
  1066. return nullptr;
  1067. }
  1068. if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(kd)) {
  1069. return nullptr;
  1070. }
  1071. // According to the spec, kd can be any non-negative number :
  1072. // http://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement
  1073. if (kd < 0) {
  1074. return nullptr;
  1075. }
  1076. return sk_sp<SkImageFilter>(new SkDiffuseLightingImageFilter(std::move(light), surfaceScale,
  1077. kd, std::move(input), cropRect));
  1078. }
  1079. SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light,
  1080. SkScalar surfaceScale,
  1081. SkScalar kd,
  1082. sk_sp<SkImageFilter> input,
  1083. const CropRect* cropRect)
  1084. : INHERITED(std::move(light), surfaceScale, std::move(input), cropRect)
  1085. , fKD(kd) {
  1086. }
  1087. sk_sp<SkFlattenable> SkDiffuseLightingImageFilter::CreateProc(SkReadBuffer& buffer) {
  1088. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  1089. sk_sp<SkImageFilterLight> light(SkImageFilterLight::UnflattenLight(buffer));
  1090. SkScalar surfaceScale = buffer.readScalar();
  1091. SkScalar kd = buffer.readScalar();
  1092. return Make(std::move(light), surfaceScale, kd, common.getInput(0), &common.cropRect());
  1093. }
  1094. void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const {
  1095. this->INHERITED::flatten(buffer);
  1096. buffer.writeScalar(fKD);
  1097. }
  1098. sk_sp<SkSpecialImage> SkDiffuseLightingImageFilter::onFilterImage(SkSpecialImage* source,
  1099. const Context& ctx,
  1100. SkIPoint* offset) const {
  1101. SkIPoint inputOffset = SkIPoint::Make(0, 0);
  1102. sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
  1103. if (!input) {
  1104. return nullptr;
  1105. }
  1106. const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
  1107. input->width(), input->height());
  1108. SkIRect bounds;
  1109. if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
  1110. return nullptr;
  1111. }
  1112. offset->fX = bounds.left();
  1113. offset->fY = bounds.top();
  1114. bounds.offset(-inputOffset);
  1115. #if SK_SUPPORT_GPU
  1116. if (source->isTextureBacked()) {
  1117. SkMatrix matrix(ctx.ctm());
  1118. matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset->fY));
  1119. return this->filterImageGPU(source, input.get(), bounds, matrix, ctx.outputProperties());
  1120. }
  1121. #endif
  1122. if (bounds.width() < 2 || bounds.height() < 2) {
  1123. return nullptr;
  1124. }
  1125. SkBitmap inputBM;
  1126. if (!input->getROPixels(&inputBM)) {
  1127. return nullptr;
  1128. }
  1129. if (inputBM.colorType() != kN32_SkColorType) {
  1130. return nullptr;
  1131. }
  1132. if (!inputBM.getPixels()) {
  1133. return nullptr;
  1134. }
  1135. const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
  1136. SkBitmap dst;
  1137. if (!dst.tryAllocPixels(info)) {
  1138. return nullptr;
  1139. }
  1140. SkMatrix matrix(ctx.ctm());
  1141. matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOffset.y()));
  1142. sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix));
  1143. DiffuseLightingType lightingType(fKD);
  1144. lightBitmap(lightingType,
  1145. transformedLight.get(),
  1146. inputBM,
  1147. &dst,
  1148. surfaceScale(),
  1149. bounds);
  1150. return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
  1151. dst);
  1152. }
  1153. #if SK_SUPPORT_GPU
  1154. std::unique_ptr<GrFragmentProcessor> SkDiffuseLightingImageFilter::makeFragmentProcessor(
  1155. sk_sp<GrTextureProxy> proxy,
  1156. const SkMatrix& matrix,
  1157. const SkIRect* srcBounds,
  1158. BoundaryMode boundaryMode) const {
  1159. SkScalar scale = this->surfaceScale() * 255;
  1160. return GrDiffuseLightingEffect::Make(std::move(proxy), this->refLight(), scale, matrix,
  1161. this->kd(), boundaryMode, srcBounds);
  1162. }
  1163. #endif
  1164. ///////////////////////////////////////////////////////////////////////////////
  1165. sk_sp<SkImageFilter> SkSpecularLightingImageFilter::Make(sk_sp<SkImageFilterLight> light,
  1166. SkScalar surfaceScale,
  1167. SkScalar ks,
  1168. SkScalar shininess,
  1169. sk_sp<SkImageFilter> input,
  1170. const CropRect* cropRect) {
  1171. if (!light) {
  1172. return nullptr;
  1173. }
  1174. if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(ks) || !SkScalarIsFinite(shininess)) {
  1175. return nullptr;
  1176. }
  1177. // According to the spec, ks can be any non-negative number :
  1178. // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement
  1179. if (ks < 0) {
  1180. return nullptr;
  1181. }
  1182. return sk_sp<SkImageFilter>(new SkSpecularLightingImageFilter(std::move(light), surfaceScale,
  1183. ks, shininess,
  1184. std::move(input), cropRect));
  1185. }
  1186. SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light,
  1187. SkScalar surfaceScale,
  1188. SkScalar ks,
  1189. SkScalar shininess,
  1190. sk_sp<SkImageFilter> input,
  1191. const CropRect* cropRect)
  1192. : INHERITED(std::move(light), surfaceScale, std::move(input), cropRect)
  1193. , fKS(ks)
  1194. , fShininess(shininess) {
  1195. }
  1196. sk_sp<SkFlattenable> SkSpecularLightingImageFilter::CreateProc(SkReadBuffer& buffer) {
  1197. SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
  1198. sk_sp<SkImageFilterLight> light(SkImageFilterLight::UnflattenLight(buffer));
  1199. SkScalar surfaceScale = buffer.readScalar();
  1200. SkScalar ks = buffer.readScalar();
  1201. SkScalar shine = buffer.readScalar();
  1202. return Make(std::move(light), surfaceScale, ks, shine, common.getInput(0),
  1203. &common.cropRect());
  1204. }
  1205. void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const {
  1206. this->INHERITED::flatten(buffer);
  1207. buffer.writeScalar(fKS);
  1208. buffer.writeScalar(fShininess);
  1209. }
  1210. sk_sp<SkSpecialImage> SkSpecularLightingImageFilter::onFilterImage(SkSpecialImage* source,
  1211. const Context& ctx,
  1212. SkIPoint* offset) const {
  1213. SkIPoint inputOffset = SkIPoint::Make(0, 0);
  1214. sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
  1215. if (!input) {
  1216. return nullptr;
  1217. }
  1218. const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
  1219. input->width(), input->height());
  1220. SkIRect bounds;
  1221. if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
  1222. return nullptr;
  1223. }
  1224. offset->fX = bounds.left();
  1225. offset->fY = bounds.top();
  1226. bounds.offset(-inputOffset);
  1227. #if SK_SUPPORT_GPU
  1228. if (source->isTextureBacked()) {
  1229. SkMatrix matrix(ctx.ctm());
  1230. matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset->fY));
  1231. return this->filterImageGPU(source, input.get(), bounds, matrix, ctx.outputProperties());
  1232. }
  1233. #endif
  1234. if (bounds.width() < 2 || bounds.height() < 2) {
  1235. return nullptr;
  1236. }
  1237. SkBitmap inputBM;
  1238. if (!input->getROPixels(&inputBM)) {
  1239. return nullptr;
  1240. }
  1241. if (inputBM.colorType() != kN32_SkColorType) {
  1242. return nullptr;
  1243. }
  1244. if (!inputBM.getPixels()) {
  1245. return nullptr;
  1246. }
  1247. const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
  1248. SkBitmap dst;
  1249. if (!dst.tryAllocPixels(info)) {
  1250. return nullptr;
  1251. }
  1252. SpecularLightingType lightingType(fKS, fShininess);
  1253. SkMatrix matrix(ctx.ctm());
  1254. matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOffset.y()));
  1255. sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix));
  1256. lightBitmap(lightingType,
  1257. transformedLight.get(),
  1258. inputBM,
  1259. &dst,
  1260. surfaceScale(),
  1261. bounds);
  1262. return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), dst);
  1263. }
  1264. #if SK_SUPPORT_GPU
  1265. std::unique_ptr<GrFragmentProcessor> SkSpecularLightingImageFilter::makeFragmentProcessor(
  1266. sk_sp<GrTextureProxy> proxy,
  1267. const SkMatrix& matrix,
  1268. const SkIRect* srcBounds,
  1269. BoundaryMode boundaryMode) const {
  1270. SkScalar scale = this->surfaceScale() * 255;
  1271. return GrSpecularLightingEffect::Make(std::move(proxy), this->refLight(), scale, matrix,
  1272. this->ks(), this->shininess(), boundaryMode, srcBounds);
  1273. }
  1274. #endif
  1275. ///////////////////////////////////////////////////////////////////////////////
  1276. #if SK_SUPPORT_GPU
  1277. static SkString emitNormalFunc(BoundaryMode mode,
  1278. const char* pointToNormalName,
  1279. const char* sobelFuncName) {
  1280. SkString result;
  1281. switch (mode) {
  1282. case kTopLeft_BoundaryMode:
  1283. result.printf("\treturn %s(%s(0.0, 0.0, m[4], m[5], m[7], m[8], %g),\n"
  1284. "\t %s(0.0, 0.0, m[4], m[7], m[5], m[8], %g),\n"
  1285. "\t surfaceScale);\n",
  1286. pointToNormalName, sobelFuncName, gTwoThirds,
  1287. sobelFuncName, gTwoThirds);
  1288. break;
  1289. case kTop_BoundaryMode:
  1290. result.printf("\treturn %s(%s(0.0, 0.0, m[3], m[5], m[6], m[8], %g),\n"
  1291. "\t %s(0.0, 0.0, m[4], m[7], m[5], m[8], %g),\n"
  1292. "\t surfaceScale);\n",
  1293. pointToNormalName, sobelFuncName, gOneThird,
  1294. sobelFuncName, gOneHalf);
  1295. break;
  1296. case kTopRight_BoundaryMode:
  1297. result.printf("\treturn %s(%s( 0.0, 0.0, m[3], m[4], m[6], m[7], %g),\n"
  1298. "\t %s(m[3], m[6], m[4], m[7], 0.0, 0.0, %g),\n"
  1299. "\t surfaceScale);\n",
  1300. pointToNormalName, sobelFuncName, gTwoThirds,
  1301. sobelFuncName, gTwoThirds);
  1302. break;
  1303. case kLeft_BoundaryMode:
  1304. result.printf("\treturn %s(%s(m[1], m[2], m[4], m[5], m[7], m[8], %g),\n"
  1305. "\t %s( 0.0, 0.0, m[1], m[7], m[2], m[8], %g),\n"
  1306. "\t surfaceScale);\n",
  1307. pointToNormalName, sobelFuncName, gOneHalf,
  1308. sobelFuncName, gOneThird);
  1309. break;
  1310. case kInterior_BoundaryMode:
  1311. result.printf("\treturn %s(%s(m[0], m[2], m[3], m[5], m[6], m[8], %g),\n"
  1312. "\t %s(m[0], m[6], m[1], m[7], m[2], m[8], %g),\n"
  1313. "\t surfaceScale);\n",
  1314. pointToNormalName, sobelFuncName, gOneQuarter,
  1315. sobelFuncName, gOneQuarter);
  1316. break;
  1317. case kRight_BoundaryMode:
  1318. result.printf("\treturn %s(%s(m[0], m[1], m[3], m[4], m[6], m[7], %g),\n"
  1319. "\t %s(m[0], m[6], m[1], m[7], 0.0, 0.0, %g),\n"
  1320. "\t surfaceScale);\n",
  1321. pointToNormalName, sobelFuncName, gOneHalf,
  1322. sobelFuncName, gOneThird);
  1323. break;
  1324. case kBottomLeft_BoundaryMode:
  1325. result.printf("\treturn %s(%s(m[1], m[2], m[4], m[5], 0.0, 0.0, %g),\n"
  1326. "\t %s( 0.0, 0.0, m[1], m[4], m[2], m[5], %g),\n"
  1327. "\t surfaceScale);\n",
  1328. pointToNormalName, sobelFuncName, gTwoThirds,
  1329. sobelFuncName, gTwoThirds);
  1330. break;
  1331. case kBottom_BoundaryMode:
  1332. result.printf("\treturn %s(%s(m[0], m[2], m[3], m[5], 0.0, 0.0, %g),\n"
  1333. "\t %s(m[0], m[3], m[1], m[4], m[2], m[5], %g),\n"
  1334. "\t surfaceScale);\n",
  1335. pointToNormalName, sobelFuncName, gOneThird,
  1336. sobelFuncName, gOneHalf);
  1337. break;
  1338. case kBottomRight_BoundaryMode:
  1339. result.printf("\treturn %s(%s(m[0], m[1], m[3], m[4], 0.0, 0.0, %g),\n"
  1340. "\t %s(m[0], m[3], m[1], m[4], 0.0, 0.0, %g),\n"
  1341. "\t surfaceScale);\n",
  1342. pointToNormalName, sobelFuncName, gTwoThirds,
  1343. sobelFuncName, gTwoThirds);
  1344. break;
  1345. default:
  1346. SkASSERT(false);
  1347. break;
  1348. }
  1349. return result;
  1350. }
  1351. class GrGLLightingEffect : public GrGLSLFragmentProcessor {
  1352. public:
  1353. GrGLLightingEffect() : fLight(nullptr) { }
  1354. ~GrGLLightingEffect() override { delete fLight; }
  1355. void emitCode(EmitArgs&) override;
  1356. static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b);
  1357. protected:
  1358. /**
  1359. * Subclasses of GrGLLightingEffect must call INHERITED::onSetData();
  1360. */
  1361. void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
  1362. virtual void emitLightFunc(GrGLSLUniformHandler*,
  1363. GrGLSLFPFragmentBuilder*,
  1364. SkString* funcName) = 0;
  1365. private:
  1366. typedef GrGLSLFragmentProcessor INHERITED;
  1367. UniformHandle fImageIncrementUni;
  1368. UniformHandle fSurfaceScaleUni;
  1369. GrTextureDomain::GLDomain fDomain;
  1370. GrGLLight* fLight;
  1371. };
  1372. ///////////////////////////////////////////////////////////////////////////////
  1373. class GrGLDiffuseLightingEffect : public GrGLLightingEffect {
  1374. public:
  1375. void emitLightFunc(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, SkString* funcName) override;
  1376. protected:
  1377. void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
  1378. private:
  1379. typedef GrGLLightingEffect INHERITED;
  1380. UniformHandle fKDUni;
  1381. };
  1382. ///////////////////////////////////////////////////////////////////////////////
  1383. class GrGLSpecularLightingEffect : public GrGLLightingEffect {
  1384. public:
  1385. void emitLightFunc(GrGLSLUniformHandler*, GrGLSLFPFragmentBuilder*, SkString* funcName) override;
  1386. protected:
  1387. void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
  1388. private:
  1389. typedef GrGLLightingEffect INHERITED;
  1390. UniformHandle fKSUni;
  1391. UniformHandle fShininessUni;
  1392. };
  1393. ///////////////////////////////////////////////////////////////////////////////
  1394. static GrTextureDomain create_domain(GrTextureProxy* proxy, const SkIRect* srcBounds,
  1395. GrTextureDomain::Mode mode) {
  1396. if (srcBounds) {
  1397. SkRect texelDomain = GrTextureDomain::MakeTexelDomain(*srcBounds, mode);
  1398. return GrTextureDomain(proxy, texelDomain, mode, mode);
  1399. } else {
  1400. return GrTextureDomain::IgnoredDomain();
  1401. }
  1402. }
  1403. GrLightingEffect::GrLightingEffect(ClassID classID,
  1404. sk_sp<GrTextureProxy> proxy,
  1405. sk_sp<const SkImageFilterLight> light,
  1406. SkScalar surfaceScale,
  1407. const SkMatrix& matrix,
  1408. BoundaryMode boundaryMode,
  1409. const SkIRect* srcBounds)
  1410. // Perhaps this could advertise the opaque or coverage-as-alpha optimizations?
  1411. : INHERITED(classID, kNone_OptimizationFlags)
  1412. , fCoordTransform(proxy.get())
  1413. , fDomain(create_domain(proxy.get(), srcBounds, GrTextureDomain::kDecal_Mode))
  1414. , fTextureSampler(std::move(proxy))
  1415. , fLight(std::move(light))
  1416. , fSurfaceScale(surfaceScale)
  1417. , fFilterMatrix(matrix)
  1418. , fBoundaryMode(boundaryMode) {
  1419. this->addCoordTransform(&fCoordTransform);
  1420. this->setTextureSamplerCnt(1);
  1421. }
  1422. GrLightingEffect::GrLightingEffect(const GrLightingEffect& that)
  1423. : INHERITED(that.classID(), that.optimizationFlags())
  1424. , fCoordTransform(that.fCoordTransform)
  1425. , fDomain(that.fDomain)
  1426. , fTextureSampler(that.fTextureSampler)
  1427. , fLight(that.fLight)
  1428. , fSurfaceScale(that.fSurfaceScale)
  1429. , fFilterMatrix(that.fFilterMatrix)
  1430. , fBoundaryMode(that.fBoundaryMode) {
  1431. this->addCoordTransform(&fCoordTransform);
  1432. this->setTextureSamplerCnt(1);
  1433. }
  1434. bool GrLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
  1435. const GrLightingEffect& s = sBase.cast<GrLightingEffect>();
  1436. return fLight->isEqual(*s.fLight) &&
  1437. fSurfaceScale == s.fSurfaceScale &&
  1438. fBoundaryMode == s.fBoundaryMode;
  1439. }
  1440. ///////////////////////////////////////////////////////////////////////////////
  1441. GrDiffuseLightingEffect::GrDiffuseLightingEffect(sk_sp<GrTextureProxy> proxy,
  1442. sk_sp<const SkImageFilterLight>light,
  1443. SkScalar surfaceScale,
  1444. const SkMatrix& matrix,
  1445. SkScalar kd,
  1446. BoundaryMode boundaryMode,
  1447. const SkIRect* srcBounds)
  1448. : INHERITED(kGrDiffuseLightingEffect_ClassID, std::move(proxy), std::move(light),
  1449. surfaceScale, matrix, boundaryMode, srcBounds)
  1450. , fKD(kd) {}
  1451. GrDiffuseLightingEffect::GrDiffuseLightingEffect(const GrDiffuseLightingEffect& that)
  1452. : INHERITED(that), fKD(that.fKD) {}
  1453. bool GrDiffuseLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
  1454. const GrDiffuseLightingEffect& s = sBase.cast<GrDiffuseLightingEffect>();
  1455. return INHERITED::onIsEqual(sBase) && this->kd() == s.kd();
  1456. }
  1457. void GrDiffuseLightingEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
  1458. GrProcessorKeyBuilder* b) const {
  1459. GrGLDiffuseLightingEffect::GenKey(*this, caps, b);
  1460. }
  1461. GrGLSLFragmentProcessor* GrDiffuseLightingEffect::onCreateGLSLInstance() const {
  1462. return new GrGLDiffuseLightingEffect;
  1463. }
  1464. GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDiffuseLightingEffect);
  1465. #if GR_TEST_UTILS
  1466. static SkPoint3 random_point3(SkRandom* random) {
  1467. return SkPoint3::Make(SkScalarToFloat(random->nextSScalar1()),
  1468. SkScalarToFloat(random->nextSScalar1()),
  1469. SkScalarToFloat(random->nextSScalar1()));
  1470. }
  1471. static SkImageFilterLight* create_random_light(SkRandom* random) {
  1472. int type = random->nextULessThan(3);
  1473. switch (type) {
  1474. case 0: {
  1475. return new SkDistantLight(random_point3(random), random->nextU());
  1476. }
  1477. case 1: {
  1478. return new SkPointLight(random_point3(random), random->nextU());
  1479. }
  1480. case 2: {
  1481. return new SkSpotLight(random_point3(random), random_point3(random),
  1482. random->nextUScalar1(), random->nextUScalar1(), random->nextU());
  1483. }
  1484. default:
  1485. SK_ABORT("Unexpected value.");
  1486. return nullptr;
  1487. }
  1488. }
  1489. std::unique_ptr<GrFragmentProcessor> GrDiffuseLightingEffect::TestCreate(GrProcessorTestData* d) {
  1490. int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
  1491. : GrProcessorUnitTest::kAlphaTextureIdx;
  1492. sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
  1493. SkScalar surfaceScale = d->fRandom->nextSScalar1();
  1494. SkScalar kd = d->fRandom->nextUScalar1();
  1495. sk_sp<SkImageFilterLight> light(create_random_light(d->fRandom));
  1496. SkMatrix matrix;
  1497. for (int i = 0; i < 9; i++) {
  1498. matrix[i] = d->fRandom->nextUScalar1();
  1499. }
  1500. SkIRect srcBounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, proxy->width()),
  1501. d->fRandom->nextRangeU(0, proxy->height()),
  1502. d->fRandom->nextRangeU(0, proxy->width()),
  1503. d->fRandom->nextRangeU(0, proxy->height()));
  1504. BoundaryMode mode = static_cast<BoundaryMode>(d->fRandom->nextU() % kBoundaryModeCount);
  1505. return GrDiffuseLightingEffect::Make(std::move(proxy), std::move(light), surfaceScale, matrix,
  1506. kd, mode, &srcBounds);
  1507. }
  1508. #endif
  1509. ///////////////////////////////////////////////////////////////////////////////
  1510. void GrGLLightingEffect::emitCode(EmitArgs& args) {
  1511. const GrLightingEffect& le = args.fFp.cast<GrLightingEffect>();
  1512. if (!fLight) {
  1513. fLight = le.light()->createGLLight();
  1514. }
  1515. GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
  1516. fImageIncrementUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
  1517. kHalf2_GrSLType, "ImageIncrement");
  1518. fSurfaceScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
  1519. kHalf_GrSLType, "SurfaceScale");
  1520. fLight->emitLightColorUniform(uniformHandler);
  1521. GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
  1522. SkString lightFunc;
  1523. this->emitLightFunc(uniformHandler, fragBuilder, &lightFunc);
  1524. const GrShaderVar gSobelArgs[] = {
  1525. GrShaderVar("a", kHalf_GrSLType),
  1526. GrShaderVar("b", kHalf_GrSLType),
  1527. GrShaderVar("c", kHalf_GrSLType),
  1528. GrShaderVar("d", kHalf_GrSLType),
  1529. GrShaderVar("e", kHalf_GrSLType),
  1530. GrShaderVar("f", kHalf_GrSLType),
  1531. GrShaderVar("scale", kHalf_GrSLType),
  1532. };
  1533. SkString sobelFuncName;
  1534. SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
  1535. fragBuilder->emitFunction(kHalf_GrSLType,
  1536. "sobel",
  1537. SK_ARRAY_COUNT(gSobelArgs),
  1538. gSobelArgs,
  1539. "\treturn (-a + b - 2.0 * c + 2.0 * d -e + f) * scale;\n",
  1540. &sobelFuncName);
  1541. const GrShaderVar gPointToNormalArgs[] = {
  1542. GrShaderVar("x", kHalf_GrSLType),
  1543. GrShaderVar("y", kHalf_GrSLType),
  1544. GrShaderVar("scale", kHalf_GrSLType),
  1545. };
  1546. SkString pointToNormalName;
  1547. fragBuilder->emitFunction(kHalf3_GrSLType,
  1548. "pointToNormal",
  1549. SK_ARRAY_COUNT(gPointToNormalArgs),
  1550. gPointToNormalArgs,
  1551. "\treturn normalize(half3(-x * scale, -y * scale, 1));\n",
  1552. &pointToNormalName);
  1553. const GrShaderVar gInteriorNormalArgs[] = {
  1554. GrShaderVar("m", kHalf_GrSLType, 9),
  1555. GrShaderVar("surfaceScale", kHalf_GrSLType),
  1556. };
  1557. SkString normalBody = emitNormalFunc(le.boundaryMode(),
  1558. pointToNormalName.c_str(),
  1559. sobelFuncName.c_str());
  1560. SkString normalName;
  1561. fragBuilder->emitFunction(kHalf3_GrSLType,
  1562. "normal",
  1563. SK_ARRAY_COUNT(gInteriorNormalArgs),
  1564. gInteriorNormalArgs,
  1565. normalBody.c_str(),
  1566. &normalName);
  1567. fragBuilder->codeAppendf("\t\tfloat2 coord = %s;\n", coords2D.c_str());
  1568. fragBuilder->codeAppend("\t\thalf m[9];\n");
  1569. const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
  1570. const char* surfScale = uniformHandler->getUniformCStr(fSurfaceScaleUni);
  1571. int index = 0;
  1572. for (int dy = 1; dy >= -1; dy--) {
  1573. for (int dx = -1; dx <= 1; dx++) {
  1574. SkString texCoords;
  1575. texCoords.appendf("coord + half2(%d, %d) * %s", dx, dy, imgInc);
  1576. SkString temp;
  1577. temp.appendf("temp%d", index);
  1578. fragBuilder->codeAppendf("half4 %s;", temp.c_str());
  1579. fDomain.sampleTexture(fragBuilder,
  1580. args.fUniformHandler,
  1581. args.fShaderCaps,
  1582. le.domain(),
  1583. temp.c_str(),
  1584. texCoords,
  1585. args.fTexSamplers[0]);
  1586. fragBuilder->codeAppendf("m[%d] = %s.a;", index, temp.c_str());
  1587. index++;
  1588. }
  1589. }
  1590. fragBuilder->codeAppend("\t\thalf3 surfaceToLight = ");
  1591. SkString arg;
  1592. arg.appendf("%s * m[4]", surfScale);
  1593. fLight->emitSurfaceToLight(uniformHandler, fragBuilder, arg.c_str());
  1594. fragBuilder->codeAppend(";\n");
  1595. fragBuilder->codeAppendf("\t\t%s = %s(%s(m, %s), surfaceToLight, ",
  1596. args.fOutputColor, lightFunc.c_str(), normalName.c_str(), surfScale);
  1597. fLight->emitLightColor(uniformHandler, fragBuilder, "surfaceToLight");
  1598. fragBuilder->codeAppend(");\n");
  1599. fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
  1600. }
  1601. void GrGLLightingEffect::GenKey(const GrProcessor& proc,
  1602. const GrShaderCaps& caps, GrProcessorKeyBuilder* b) {
  1603. const GrLightingEffect& lighting = proc.cast<GrLightingEffect>();
  1604. b->add32(lighting.boundaryMode() << 2 | lighting.light()->type());
  1605. b->add32(GrTextureDomain::GLDomain::DomainKey(lighting.domain()));
  1606. }
  1607. void GrGLLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman,
  1608. const GrFragmentProcessor& proc) {
  1609. const GrLightingEffect& lighting = proc.cast<GrLightingEffect>();
  1610. if (!fLight) {
  1611. fLight = lighting.light()->createGLLight();
  1612. }
  1613. GrTextureProxy* proxy = lighting.textureSampler(0).proxy();
  1614. GrTexture* texture = proxy->peekTexture();
  1615. float ySign = proxy->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f;
  1616. pdman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->height());
  1617. pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale());
  1618. sk_sp<SkImageFilterLight> transformedLight(
  1619. lighting.light()->transform(lighting.filterMatrix()));
  1620. fDomain.setData(pdman, lighting.domain(), proxy, lighting.textureSampler(0).samplerState());
  1621. fLight->setData(pdman, transformedLight.get());
  1622. }
  1623. ///////////////////////////////////////////////////////////////////////////////
  1624. ///////////////////////////////////////////////////////////////////////////////
  1625. void GrGLDiffuseLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHandler,
  1626. GrGLSLFPFragmentBuilder* fragBuilder,
  1627. SkString* funcName) {
  1628. const char* kd;
  1629. fKDUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "KD", &kd);
  1630. const GrShaderVar gLightArgs[] = {
  1631. GrShaderVar("normal", kHalf3_GrSLType),
  1632. GrShaderVar("surfaceToLight", kHalf3_GrSLType),
  1633. GrShaderVar("lightColor", kHalf3_GrSLType)
  1634. };
  1635. SkString lightBody;
  1636. lightBody.appendf("\thalf colorScale = %s * dot(normal, surfaceToLight);\n", kd);
  1637. lightBody.appendf("\treturn half4(lightColor * saturate(colorScale), 1.0);\n");
  1638. fragBuilder->emitFunction(kHalf4_GrSLType,
  1639. "light",
  1640. SK_ARRAY_COUNT(gLightArgs),
  1641. gLightArgs,
  1642. lightBody.c_str(),
  1643. funcName);
  1644. }
  1645. void GrGLDiffuseLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman,
  1646. const GrFragmentProcessor& proc) {
  1647. INHERITED::onSetData(pdman, proc);
  1648. const GrDiffuseLightingEffect& diffuse = proc.cast<GrDiffuseLightingEffect>();
  1649. pdman.set1f(fKDUni, diffuse.kd());
  1650. }
  1651. ///////////////////////////////////////////////////////////////////////////////
  1652. GrSpecularLightingEffect::GrSpecularLightingEffect(sk_sp<GrTextureProxy> proxy,
  1653. sk_sp<const SkImageFilterLight> light,
  1654. SkScalar surfaceScale,
  1655. const SkMatrix& matrix,
  1656. SkScalar ks,
  1657. SkScalar shininess,
  1658. BoundaryMode boundaryMode,
  1659. const SkIRect* srcBounds)
  1660. : INHERITED(kGrSpecularLightingEffect_ClassID, std::move(proxy), std::move(light),
  1661. surfaceScale, matrix, boundaryMode, srcBounds)
  1662. , fKS(ks)
  1663. , fShininess(shininess) {}
  1664. GrSpecularLightingEffect::GrSpecularLightingEffect(const GrSpecularLightingEffect& that)
  1665. : INHERITED(that), fKS(that.fKS), fShininess(that.fShininess) {}
  1666. bool GrSpecularLightingEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
  1667. const GrSpecularLightingEffect& s = sBase.cast<GrSpecularLightingEffect>();
  1668. return INHERITED::onIsEqual(sBase) &&
  1669. this->ks() == s.ks() &&
  1670. this->shininess() == s.shininess();
  1671. }
  1672. void GrSpecularLightingEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
  1673. GrProcessorKeyBuilder* b) const {
  1674. GrGLSpecularLightingEffect::GenKey(*this, caps, b);
  1675. }
  1676. GrGLSLFragmentProcessor* GrSpecularLightingEffect::onCreateGLSLInstance() const {
  1677. return new GrGLSpecularLightingEffect;
  1678. }
  1679. GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSpecularLightingEffect);
  1680. #if GR_TEST_UTILS
  1681. std::unique_ptr<GrFragmentProcessor> GrSpecularLightingEffect::TestCreate(GrProcessorTestData* d) {
  1682. int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
  1683. : GrProcessorUnitTest::kAlphaTextureIdx;
  1684. sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
  1685. SkScalar surfaceScale = d->fRandom->nextSScalar1();
  1686. SkScalar ks = d->fRandom->nextUScalar1();
  1687. SkScalar shininess = d->fRandom->nextUScalar1();
  1688. sk_sp<SkImageFilterLight> light(create_random_light(d->fRandom));
  1689. SkMatrix matrix;
  1690. for (int i = 0; i < 9; i++) {
  1691. matrix[i] = d->fRandom->nextUScalar1();
  1692. }
  1693. BoundaryMode mode = static_cast<BoundaryMode>(d->fRandom->nextU() % kBoundaryModeCount);
  1694. SkIRect srcBounds = SkIRect::MakeXYWH(d->fRandom->nextRangeU(0, proxy->width()),
  1695. d->fRandom->nextRangeU(0, proxy->height()),
  1696. d->fRandom->nextRangeU(0, proxy->width()),
  1697. d->fRandom->nextRangeU(0, proxy->height()));
  1698. return GrSpecularLightingEffect::Make(std::move(proxy), std::move(light), surfaceScale, matrix,
  1699. ks, shininess, mode, &srcBounds);
  1700. }
  1701. #endif
  1702. ///////////////////////////////////////////////////////////////////////////////
  1703. void GrGLSpecularLightingEffect::emitLightFunc(GrGLSLUniformHandler* uniformHandler,
  1704. GrGLSLFPFragmentBuilder* fragBuilder,
  1705. SkString* funcName) {
  1706. const char* ks;
  1707. const char* shininess;
  1708. fKSUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "KS", &ks);
  1709. fShininessUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
  1710. kHalf_GrSLType,
  1711. "Shininess",
  1712. &shininess);
  1713. const GrShaderVar gLightArgs[] = {
  1714. GrShaderVar("normal", kHalf3_GrSLType),
  1715. GrShaderVar("surfaceToLight", kHalf3_GrSLType),
  1716. GrShaderVar("lightColor", kHalf3_GrSLType)
  1717. };
  1718. SkString lightBody;
  1719. lightBody.appendf("\thalf3 halfDir = half3(normalize(surfaceToLight + half3(0, 0, 1)));\n");
  1720. lightBody.appendf("\thalf colorScale = half(%s * pow(dot(normal, halfDir), %s));\n",
  1721. ks, shininess);
  1722. lightBody.appendf("\thalf3 color = lightColor * saturate(colorScale);\n");
  1723. lightBody.appendf("\treturn half4(color, max(max(color.r, color.g), color.b));\n");
  1724. fragBuilder->emitFunction(kHalf4_GrSLType,
  1725. "light",
  1726. SK_ARRAY_COUNT(gLightArgs),
  1727. gLightArgs,
  1728. lightBody.c_str(),
  1729. funcName);
  1730. }
  1731. void GrGLSpecularLightingEffect::onSetData(const GrGLSLProgramDataManager& pdman,
  1732. const GrFragmentProcessor& effect) {
  1733. INHERITED::onSetData(pdman, effect);
  1734. const GrSpecularLightingEffect& spec = effect.cast<GrSpecularLightingEffect>();
  1735. pdman.set1f(fKSUni, spec.ks());
  1736. pdman.set1f(fShininessUni, spec.shininess());
  1737. }
  1738. ///////////////////////////////////////////////////////////////////////////////
  1739. void GrGLLight::emitLightColorUniform(GrGLSLUniformHandler* uniformHandler) {
  1740. fColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType, "LightColor");
  1741. }
  1742. void GrGLLight::emitLightColor(GrGLSLUniformHandler* uniformHandler,
  1743. GrGLSLFPFragmentBuilder* fragBuilder,
  1744. const char *surfaceToLight) {
  1745. fragBuilder->codeAppend(uniformHandler->getUniformCStr(this->lightColorUni()));
  1746. }
  1747. void GrGLLight::setData(const GrGLSLProgramDataManager& pdman,
  1748. const SkImageFilterLight* light) const {
  1749. setUniformPoint3(pdman, fColorUni,
  1750. light->color().makeScale(SkScalarInvert(SkIntToScalar(255))));
  1751. }
  1752. ///////////////////////////////////////////////////////////////////////////////
  1753. void GrGLDistantLight::setData(const GrGLSLProgramDataManager& pdman,
  1754. const SkImageFilterLight* light) const {
  1755. INHERITED::setData(pdman, light);
  1756. SkASSERT(light->type() == SkImageFilterLight::kDistant_LightType);
  1757. const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(light);
  1758. setUniformNormal3(pdman, fDirectionUni, distantLight->direction());
  1759. }
  1760. void GrGLDistantLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler,
  1761. GrGLSLFPFragmentBuilder* fragBuilder,
  1762. const char* z) {
  1763. const char* dir;
  1764. fDirectionUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
  1765. "LightDirection", &dir);
  1766. fragBuilder->codeAppend(dir);
  1767. }
  1768. ///////////////////////////////////////////////////////////////////////////////
  1769. void GrGLPointLight::setData(const GrGLSLProgramDataManager& pdman,
  1770. const SkImageFilterLight* light) const {
  1771. INHERITED::setData(pdman, light);
  1772. SkASSERT(light->type() == SkImageFilterLight::kPoint_LightType);
  1773. const SkPointLight* pointLight = static_cast<const SkPointLight*>(light);
  1774. setUniformPoint3(pdman, fLocationUni, pointLight->location());
  1775. }
  1776. void GrGLPointLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler,
  1777. GrGLSLFPFragmentBuilder* fragBuilder,
  1778. const char* z) {
  1779. const char* loc;
  1780. fLocationUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
  1781. "LightLocation", &loc);
  1782. fragBuilder->codeAppendf("normalize(%s - half3(sk_FragCoord.xy, %s))",
  1783. loc, z);
  1784. }
  1785. ///////////////////////////////////////////////////////////////////////////////
  1786. void GrGLSpotLight::setData(const GrGLSLProgramDataManager& pdman,
  1787. const SkImageFilterLight* light) const {
  1788. INHERITED::setData(pdman, light);
  1789. SkASSERT(light->type() == SkImageFilterLight::kSpot_LightType);
  1790. const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light);
  1791. setUniformPoint3(pdman, fLocationUni, spotLight->location());
  1792. pdman.set1f(fExponentUni, spotLight->specularExponent());
  1793. pdman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle());
  1794. pdman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle());
  1795. pdman.set1f(fConeScaleUni, spotLight->coneScale());
  1796. setUniformNormal3(pdman, fSUni, spotLight->s());
  1797. }
  1798. void GrGLSpotLight::emitSurfaceToLight(GrGLSLUniformHandler* uniformHandler,
  1799. GrGLSLFPFragmentBuilder* fragBuilder,
  1800. const char* z) {
  1801. const char* location;
  1802. fLocationUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType,
  1803. "LightLocation", &location);
  1804. fragBuilder->codeAppendf("normalize(%s - half3(sk_FragCoord.xy, %s))",
  1805. location, z);
  1806. }
  1807. void GrGLSpotLight::emitLightColor(GrGLSLUniformHandler* uniformHandler,
  1808. GrGLSLFPFragmentBuilder* fragBuilder,
  1809. const char *surfaceToLight) {
  1810. const char* color = uniformHandler->getUniformCStr(this->lightColorUni()); // created by parent class.
  1811. const char* exponent;
  1812. const char* cosInner;
  1813. const char* cosOuter;
  1814. const char* coneScale;
  1815. const char* s;
  1816. fExponentUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
  1817. "Exponent", &exponent);
  1818. fCosInnerConeAngleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
  1819. "CosInnerConeAngle", &cosInner);
  1820. fCosOuterConeAngleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
  1821. "CosOuterConeAngle", &cosOuter);
  1822. fConeScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType,
  1823. "ConeScale", &coneScale);
  1824. fSUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf3_GrSLType, "S", &s);
  1825. const GrShaderVar gLightColorArgs[] = {
  1826. GrShaderVar("surfaceToLight", kHalf3_GrSLType)
  1827. };
  1828. SkString lightColorBody;
  1829. lightColorBody.appendf("\thalf cosAngle = -dot(surfaceToLight, %s);\n", s);
  1830. lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter);
  1831. lightColorBody.appendf("\t\treturn half3(0);\n");
  1832. lightColorBody.appendf("\t}\n");
  1833. lightColorBody.appendf("\thalf scale = pow(cosAngle, %s);\n", exponent);
  1834. lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner);
  1835. lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n",
  1836. color, cosOuter, coneScale);
  1837. lightColorBody.appendf("\t}\n");
  1838. lightColorBody.appendf("\treturn %s;\n", color);
  1839. fragBuilder->emitFunction(kHalf3_GrSLType,
  1840. "lightColor",
  1841. SK_ARRAY_COUNT(gLightColorArgs),
  1842. gLightColorArgs,
  1843. lightColorBody.c_str(),
  1844. &fLightColorFunc);
  1845. fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight);
  1846. }
  1847. #endif
  1848. void SkLightingImageFilter::RegisterFlattenables() {
  1849. SK_REGISTER_FLATTENABLE(SkDiffuseLightingImageFilter);
  1850. SK_REGISTER_FLATTENABLE(SkSpecularLightingImageFilter);
  1851. }