Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: src/utils/SkLua.cpp

Issue 663993002: add blurimagefilter to lua (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « resources/slides.lua ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkLua.h" 8 #include "SkLua.h"
9 9
10 #if SK_SUPPORT_GPU 10 #if SK_SUPPORT_GPU
11 #include "GrReducedClip.h" 11 #include "GrReducedClip.h"
12 #endif 12 #endif
13 13
14 #include "SkBlurImageFilter.h"
14 #include "SkCanvas.h" 15 #include "SkCanvas.h"
15 #include "SkData.h" 16 #include "SkData.h"
16 #include "SkDecodingImageGenerator.h" 17 #include "SkDecodingImageGenerator.h"
17 #include "SkDocument.h" 18 #include "SkDocument.h"
18 #include "SkImage.h" 19 #include "SkImage.h"
19 #include "SkMatrix.h" 20 #include "SkMatrix.h"
20 #include "SkPaint.h" 21 #include "SkPaint.h"
21 #include "SkPath.h" 22 #include "SkPath.h"
22 #include "SkPictureRecorder.h" 23 #include "SkPictureRecorder.h"
23 #include "SkPixelRef.h" 24 #include "SkPixelRef.h"
(...skipping 12 matching lines...) Expand all
36 // return the metatable name for a given class 37 // return the metatable name for a given class
37 template <typename T> const char* get_mtname(); 38 template <typename T> const char* get_mtname();
38 #define DEF_MTNAME(T) \ 39 #define DEF_MTNAME(T) \
39 template <> const char* get_mtname<T>() { \ 40 template <> const char* get_mtname<T>() { \
40 return #T "_LuaMetaTableName"; \ 41 return #T "_LuaMetaTableName"; \
41 } 42 }
42 43
43 DEF_MTNAME(SkCanvas) 44 DEF_MTNAME(SkCanvas)
44 DEF_MTNAME(SkDocument) 45 DEF_MTNAME(SkDocument)
45 DEF_MTNAME(SkImage) 46 DEF_MTNAME(SkImage)
47 DEF_MTNAME(SkImageFilter)
46 DEF_MTNAME(SkMatrix) 48 DEF_MTNAME(SkMatrix)
47 DEF_MTNAME(SkRRect) 49 DEF_MTNAME(SkRRect)
48 DEF_MTNAME(SkPath) 50 DEF_MTNAME(SkPath)
49 DEF_MTNAME(SkPaint) 51 DEF_MTNAME(SkPaint)
50 DEF_MTNAME(SkPathEffect) 52 DEF_MTNAME(SkPathEffect)
51 DEF_MTNAME(SkPicture) 53 DEF_MTNAME(SkPicture)
52 DEF_MTNAME(SkPictureRecorder) 54 DEF_MTNAME(SkPictureRecorder)
53 DEF_MTNAME(SkShader) 55 DEF_MTNAME(SkShader)
54 DEF_MTNAME(SkSurface) 56 DEF_MTNAME(SkSurface)
55 DEF_MTNAME(SkTextBlob) 57 DEF_MTNAME(SkTextBlob)
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 SkScalar value; 397 SkScalar value;
396 if (lua_isnil(L, -1)) { 398 if (lua_isnil(L, -1)) {
397 value = def; 399 value = def;
398 } else { 400 } else {
399 value = lua2scalar(L, -1); 401 value = lua2scalar(L, -1);
400 } 402 }
401 lua_pop(L, 1); 403 lua_pop(L, 1);
402 return value; 404 return value;
403 } 405 }
404 406
407 static SkScalar byte2unit(U8CPU byte) {
408 return byte / 255.0f;
409 }
410
405 static U8CPU unit2byte(SkScalar x) { 411 static U8CPU unit2byte(SkScalar x) {
406 if (x <= 0) { 412 if (x <= 0) {
407 return 0; 413 return 0;
408 } else if (x >= 1) { 414 } else if (x >= 1) {
409 return 255; 415 return 255;
410 } else { 416 } else {
411 return SkScalarRoundToInt(x * 255); 417 return SkScalarRoundToInt(x * 255);
412 } 418 }
413 } 419 }
414 420
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 static int lpaint_isAutohinted(lua_State* L) { 785 static int lpaint_isAutohinted(lua_State* L) {
780 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted()); 786 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAutohinted());
781 return 1; 787 return 1;
782 } 788 }
783 789
784 static int lpaint_isVerticalText(lua_State* L) { 790 static int lpaint_isVerticalText(lua_State* L) {
785 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText()); 791 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isVerticalText());
786 return 1; 792 return 1;
787 } 793 }
788 794
795 static int lpaint_getAlpha(lua_State* L) {
796 SkLua(L).pushScalar(byte2unit(get_obj<SkPaint>(L, 1)->getAlpha()));
797 return 1;
798 }
799
800 static int lpaint_setAlpha(lua_State* L) {
801 get_obj<SkPaint>(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2)));
802 return 0;
803 }
804
789 static int lpaint_getColor(lua_State* L) { 805 static int lpaint_getColor(lua_State* L) {
790 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor()); 806 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
791 return 1; 807 return 1;
792 } 808 }
793 809
794 static int lpaint_setColor(lua_State* L) { 810 static int lpaint_setColor(lua_State* L) {
795 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2)); 811 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
796 return 0; 812 return 0;
797 } 813 }
798 814
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 setfield_scalar(L, "bottom", fm.fBottom); 968 setfield_scalar(L, "bottom", fm.fBottom);
953 setfield_scalar(L, "leading", fm.fLeading); 969 setfield_scalar(L, "leading", fm.fLeading);
954 SkLua(L).pushScalar(height); 970 SkLua(L).pushScalar(height);
955 return 2; 971 return 2;
956 } 972 }
957 973
958 static int lpaint_getEffects(lua_State* L) { 974 static int lpaint_getEffects(lua_State* L) {
959 const SkPaint* paint = get_obj<SkPaint>(L, 1); 975 const SkPaint* paint = get_obj<SkPaint>(L, 1);
960 976
961 lua_newtable(L); 977 lua_newtable(L);
962 setfield_bool_if(L, "looper", !!paint->getLooper()); 978 setfield_bool_if(L, "looper", !!paint->getLooper());
963 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); 979 setfield_bool_if(L, "pathEffect", !!paint->getPathEffect());
964 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer()); 980 setfield_bool_if(L, "rasterizer", !!paint->getRasterizer());
965 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); 981 setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter());
966 setfield_bool_if(L, "shader", !!paint->getShader()); 982 setfield_bool_if(L, "shader", !!paint->getShader());
967 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); 983 setfield_bool_if(L, "colorFilter", !!paint->getColorFilter());
968 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); 984 setfield_bool_if(L, "imageFilter", !!paint->getImageFilter());
969 setfield_bool_if(L, "xfermode", !!paint->getXfermode()); 985 setfield_bool_if(L, "xfermode", !!paint->getXfermode());
970 return 1; 986 return 1;
971 } 987 }
972 988
989 static int lpaint_getImageFilter(lua_State* L) {
990 const SkPaint* paint = get_obj<SkPaint>(L, 1);
991 SkImageFilter* imf = paint->getImageFilter();
992 if (imf) {
993 push_ref(L, imf);
994 return 1;
995 }
996 return 0;
997 }
998
999 static int lpaint_setImageFilter(lua_State* L) {
1000 SkPaint* paint = get_obj<SkPaint>(L, 1);
1001 paint->setImageFilter(get_ref<SkImageFilter>(L, 2));
1002 return 0;
1003 }
1004
973 static int lpaint_getShader(lua_State* L) { 1005 static int lpaint_getShader(lua_State* L) {
974 const SkPaint* paint = get_obj<SkPaint>(L, 1); 1006 const SkPaint* paint = get_obj<SkPaint>(L, 1);
975 SkShader* shader = paint->getShader(); 1007 SkShader* shader = paint->getShader();
976 if (shader) { 1008 if (shader) {
977 push_ref(L, shader); 1009 push_ref(L, shader);
978 return 1; 1010 return 1;
979 } 1011 }
980 return 0; 1012 return 0;
981 } 1013 }
982 1014
(...skipping 20 matching lines...) Expand all
1003 { "isStrikeThruText", lpaint_isStrikeThruText }, 1035 { "isStrikeThruText", lpaint_isStrikeThruText },
1004 { "isFakeBoldText", lpaint_isFakeBoldText }, 1036 { "isFakeBoldText", lpaint_isFakeBoldText },
1005 { "isLinearText", lpaint_isLinearText }, 1037 { "isLinearText", lpaint_isLinearText },
1006 { "isSubpixelText", lpaint_isSubpixelText }, 1038 { "isSubpixelText", lpaint_isSubpixelText },
1007 { "setSubpixelText", lpaint_setSubpixelText }, 1039 { "setSubpixelText", lpaint_setSubpixelText },
1008 { "isDevKernText", lpaint_isDevKernText }, 1040 { "isDevKernText", lpaint_isDevKernText },
1009 { "isLCDRenderText", lpaint_isLCDRenderText }, 1041 { "isLCDRenderText", lpaint_isLCDRenderText },
1010 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, 1042 { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText },
1011 { "isAutohinted", lpaint_isAutohinted }, 1043 { "isAutohinted", lpaint_isAutohinted },
1012 { "isVerticalText", lpaint_isVerticalText }, 1044 { "isVerticalText", lpaint_isVerticalText },
1045 { "getAlpha", lpaint_getAlpha },
1046 { "setAlpha", lpaint_setAlpha },
1013 { "getColor", lpaint_getColor }, 1047 { "getColor", lpaint_getColor },
1014 { "setColor", lpaint_setColor }, 1048 { "setColor", lpaint_setColor },
1015 { "getTextSize", lpaint_getTextSize }, 1049 { "getTextSize", lpaint_getTextSize },
1016 { "setTextSize", lpaint_setTextSize }, 1050 { "setTextSize", lpaint_setTextSize },
1017 { "getTextScaleX", lpaint_getTextScaleX }, 1051 { "getTextScaleX", lpaint_getTextScaleX },
1018 { "getTextSkewX", lpaint_getTextSkewX }, 1052 { "getTextSkewX", lpaint_getTextSkewX },
1019 { "getTypeface", lpaint_getTypeface }, 1053 { "getTypeface", lpaint_getTypeface },
1020 { "setTypeface", lpaint_setTypeface }, 1054 { "setTypeface", lpaint_setTypeface },
1021 { "getHinting", lpaint_getHinting }, 1055 { "getHinting", lpaint_getHinting },
1022 { "getFontID", lpaint_getFontID }, 1056 { "getFontID", lpaint_getFontID },
1023 { "getTextAlign", lpaint_getTextAlign }, 1057 { "getTextAlign", lpaint_getTextAlign },
1024 { "setTextAlign", lpaint_setTextAlign }, 1058 { "setTextAlign", lpaint_setTextAlign },
1025 { "getStroke", lpaint_getStroke }, 1059 { "getStroke", lpaint_getStroke },
1026 { "setStroke", lpaint_setStroke }, 1060 { "setStroke", lpaint_setStroke },
1027 { "getStrokeCap", lpaint_getStrokeCap }, 1061 { "getStrokeCap", lpaint_getStrokeCap },
1028 { "getStrokeJoin", lpaint_getStrokeJoin }, 1062 { "getStrokeJoin", lpaint_getStrokeJoin },
1029 { "getTextEncoding", lpaint_getTextEncoding }, 1063 { "getTextEncoding", lpaint_getTextEncoding },
1030 { "getStrokeWidth", lpaint_getStrokeWidth }, 1064 { "getStrokeWidth", lpaint_getStrokeWidth },
1031 { "setStrokeWidth", lpaint_setStrokeWidth }, 1065 { "setStrokeWidth", lpaint_setStrokeWidth },
1032 { "getStrokeMiter", lpaint_getStrokeMiter }, 1066 { "getStrokeMiter", lpaint_getStrokeMiter },
1033 { "measureText", lpaint_measureText }, 1067 { "measureText", lpaint_measureText },
1034 { "getFontMetrics", lpaint_getFontMetrics }, 1068 { "getFontMetrics", lpaint_getFontMetrics },
1035 { "getEffects", lpaint_getEffects }, 1069 { "getEffects", lpaint_getEffects },
1070 { "getImageFilter", lpaint_getImageFilter },
1071 { "setImageFilter", lpaint_setImageFilter },
1036 { "getShader", lpaint_getShader }, 1072 { "getShader", lpaint_getShader },
1037 { "getPathEffect", lpaint_getPathEffect }, 1073 { "getPathEffect", lpaint_getPathEffect },
1038 { "__gc", lpaint_gc }, 1074 { "__gc", lpaint_gc },
1039 { NULL, NULL } 1075 { NULL, NULL }
1040 }; 1076 };
1041 1077
1042 /////////////////////////////////////////////////////////////////////////////// 1078 ///////////////////////////////////////////////////////////////////////////////
1043 1079
1044 static const char* mode2string(SkShader::TileMode mode) { 1080 static const char* mode2string(SkShader::TileMode mode) {
1045 static const char* gNames[] = { "clamp", "repeat", "mirror" }; 1081 static const char* gNames[] = { "clamp", "repeat", "mirror" };
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1150 } 1186 }
1151 1187
1152 static const struct luaL_Reg gSkPathEffect_Methods[] = { 1188 static const struct luaL_Reg gSkPathEffect_Methods[] = {
1153 { "asADash", lpatheffect_asADash }, 1189 { "asADash", lpatheffect_asADash },
1154 { "__gc", lpatheffect_gc }, 1190 { "__gc", lpatheffect_gc },
1155 { NULL, NULL } 1191 { NULL, NULL }
1156 }; 1192 };
1157 1193
1158 /////////////////////////////////////////////////////////////////////////////// 1194 ///////////////////////////////////////////////////////////////////////////////
1159 1195
1196 static int lpimagefilter_gc(lua_State* L) {
1197 get_ref<SkImageFilter>(L, 1)->unref();
1198 return 0;
1199 }
1200
1201 static const struct luaL_Reg gSkImageFilter_Methods[] = {
1202 { "__gc", lpimagefilter_gc },
1203 { NULL, NULL }
1204 };
1205
1206 ///////////////////////////////////////////////////////////////////////////////
1207
1160 static int lmatrix_getType(lua_State* L) { 1208 static int lmatrix_getType(lua_State* L) {
1161 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType(); 1209 SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
1162 1210
1163 lua_newtable(L); 1211 lua_newtable(L);
1164 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask )); 1212 setfield_boolean(L, "translate", SkToBool(mask & SkMatrix::kTranslate_Mask ));
1165 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask)); 1213 setfield_boolean(L, "scale", SkToBool(mask & SkMatrix::kScale_Mask));
1166 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask)); 1214 setfield_boolean(L, "affine", SkToBool(mask & SkMatrix::kAffine_Mask));
1167 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Ma sk)); 1215 setfield_boolean(L, "perspective", SkToBool(mask & SkMatrix::kPerspective_Ma sk));
1168 return 1; 1216 return 1;
1169 } 1217 }
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
1681 if (NULL == doc) { 1729 if (NULL == doc) {
1682 // do I need to push a nil on the stack and return 1? 1730 // do I need to push a nil on the stack and return 1?
1683 return 0; 1731 return 0;
1684 } else { 1732 } else {
1685 push_ref(L, doc); 1733 push_ref(L, doc);
1686 doc->unref(); 1734 doc->unref();
1687 return 1; 1735 return 1;
1688 } 1736 }
1689 } 1737 }
1690 1738
1739 static int lsk_newBlurImageFilter(lua_State* L) {
1740 SkScalar sigmaX = lua2scalar_def(L, 1, 0);
1741 SkScalar sigmaY = lua2scalar_def(L, 2, 0);
1742 SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY);
1743 if (NULL == imf) {
1744 lua_pushnil(L);
1745 } else {
1746 push_ref(L, imf);
1747 imf->unref();
1748 }
1749 return 1;
1750 }
1751
1691 static int lsk_newMatrix(lua_State* L) { 1752 static int lsk_newMatrix(lua_State* L) {
1692 push_new<SkMatrix>(L)->reset(); 1753 push_new<SkMatrix>(L)->reset();
1693 return 1; 1754 return 1;
1694 } 1755 }
1695 1756
1696 static int lsk_newPaint(lua_State* L) { 1757 static int lsk_newPaint(lua_State* L) {
1697 push_new<SkPaint>(L); 1758 push_new<SkPaint>(L);
1698 return 1; 1759 return 1;
1699 } 1760 }
1700 1761
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1769 } 1830 }
1770 1831
1771 static void register_Sk(lua_State* L) { 1832 static void register_Sk(lua_State* L) {
1772 lua_newtable(L); 1833 lua_newtable(L);
1773 lua_pushvalue(L, -1); 1834 lua_pushvalue(L, -1);
1774 lua_setglobal(L, "Sk"); 1835 lua_setglobal(L, "Sk");
1775 // the Sk table is still on top 1836 // the Sk table is still on top
1776 1837
1777 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); 1838 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
1778 setfield_function(L, "loadImage", lsk_loadImage); 1839 setfield_function(L, "loadImage", lsk_loadImage);
1840 setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter);
1779 setfield_function(L, "newMatrix", lsk_newMatrix); 1841 setfield_function(L, "newMatrix", lsk_newMatrix);
1780 setfield_function(L, "newPaint", lsk_newPaint); 1842 setfield_function(L, "newPaint", lsk_newPaint);
1781 setfield_function(L, "newPath", lsk_newPath); 1843 setfield_function(L, "newPath", lsk_newPath);
1782 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); 1844 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder);
1783 setfield_function(L, "newRRect", lsk_newRRect); 1845 setfield_function(L, "newRRect", lsk_newRRect);
1784 setfield_function(L, "newRasterSurface", lsk_newRasterSurface); 1846 setfield_function(L, "newRasterSurface", lsk_newRasterSurface);
1785 setfield_function(L, "newTypeface", lsk_newTypeface); 1847 setfield_function(L, "newTypeface", lsk_newTypeface);
1786 lua_pop(L, 1); // pop off the Sk table 1848 lua_pop(L, 1); // pop off the Sk table
1787 } 1849 }
1788 1850
1789 #define REG_CLASS(L, C) \ 1851 #define REG_CLASS(L, C) \
1790 do { \ 1852 do { \
1791 luaL_newmetatable(L, get_mtname<C>()); \ 1853 luaL_newmetatable(L, get_mtname<C>()); \
1792 lua_pushvalue(L, -1); \ 1854 lua_pushvalue(L, -1); \
1793 lua_setfield(L, -2, "__index"); \ 1855 lua_setfield(L, -2, "__index"); \
1794 luaL_setfuncs(L, g##C##_Methods, 0); \ 1856 luaL_setfuncs(L, g##C##_Methods, 0); \
1795 lua_pop(L, 1); /* pop off the meta-table */ \ 1857 lua_pop(L, 1); /* pop off the meta-table */ \
1796 } while (0) 1858 } while (0)
1797 1859
1798 void SkLua::Load(lua_State* L) { 1860 void SkLua::Load(lua_State* L) {
1799 register_Sk(L); 1861 register_Sk(L);
1800 REG_CLASS(L, SkCanvas); 1862 REG_CLASS(L, SkCanvas);
1801 REG_CLASS(L, SkDocument); 1863 REG_CLASS(L, SkDocument);
1802 REG_CLASS(L, SkImage); 1864 REG_CLASS(L, SkImage);
1865 REG_CLASS(L, SkImageFilter);
1803 REG_CLASS(L, SkPaint); 1866 REG_CLASS(L, SkPaint);
1804 REG_CLASS(L, SkPath); 1867 REG_CLASS(L, SkPath);
1805 REG_CLASS(L, SkPathEffect); 1868 REG_CLASS(L, SkPathEffect);
1806 REG_CLASS(L, SkPicture); 1869 REG_CLASS(L, SkPicture);
1807 REG_CLASS(L, SkPictureRecorder); 1870 REG_CLASS(L, SkPictureRecorder);
1808 REG_CLASS(L, SkRRect); 1871 REG_CLASS(L, SkRRect);
1809 REG_CLASS(L, SkShader); 1872 REG_CLASS(L, SkShader);
1810 REG_CLASS(L, SkSurface); 1873 REG_CLASS(L, SkSurface);
1811 REG_CLASS(L, SkTypeface); 1874 REG_CLASS(L, SkTypeface);
1812 REG_CLASS(L, SkMatrix); 1875 REG_CLASS(L, SkMatrix);
1813 } 1876 }
1814 1877
1815 extern "C" int luaopen_skia(lua_State* L); 1878 extern "C" int luaopen_skia(lua_State* L);
1816 extern "C" int luaopen_skia(lua_State* L) { 1879 extern "C" int luaopen_skia(lua_State* L) {
1817 SkLua::Load(L); 1880 SkLua::Load(L);
1818 return 0; 1881 return 0;
1819 } 1882 }
OLDNEW
« no previous file with comments | « resources/slides.lua ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698