From 468b1815d29d9bbe513f2d8eb4afc0b066715b52 Mon Sep 17 00:00:00 2001 From: reed Date: Sun, 19 Oct 2014 11:42:54 -0700 Subject: add blurimagefilter to lua BUG=skia: TBR= Review URL: https://codereview.chromium.org/663993002 --- resources/slides.lua | 42 ++++++++++++++++++++++------- src/utils/SkLua.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 16 deletions(-) diff --git a/resources/slides.lua b/resources/slides.lua index a4de5b74d6..b2c9b902c3 100644 --- a/resources/slides.lua +++ b/resources/slides.lua @@ -138,8 +138,18 @@ function slide_transition(prev, next, is_forward) return rec end +function sqr(value) return value * value end + +function ease(value) +function set_blur(paint, alpha) + local sigma = sqr(1 - alpha) * 20 +-- paint:setImageFilter(Sk.newBlurImageFilter(sigma, sigma)) + paint:setAlpha(alpha) +end + function fade_slide_transition(prev, next, is_forward) local rec = { + paint = Sk.newPaint(), prevDrawable = prev, nextDrawable = next, proc = function(self, canvas, drawSlideProc) @@ -147,8 +157,12 @@ function fade_slide_transition(prev, next, is_forward) drawSlideProc(canvas) return nil end - self.prevDrawable:draw(canvas, self.prev_x, 0, self.prev_a) - self.nextDrawable:draw(canvas, self.next_x, 0, self.next_a) + + set_blur(self.paint, self.prev_a) + self.prevDrawable:draw(canvas, self.prev_x, 0, self.paint) + + set_blur(self.paint, self.next_a) + self.nextDrawable:draw(canvas, self.next_x, 0, self.paint) self:step() return self end @@ -263,6 +277,20 @@ function new_drawable_image(img) } end +function convert_to_picture_drawable(slide) + local rec = Sk.newPictureRecorder() + drawSlide(rec:beginRecording(640, 480), slide, gTemplate) + return new_drawable_picture(rec:endRecording()) +end + +function convert_to_image_drawable(slide) + local surf = Sk.newRasterSurface(640, 480) + drawSlide(surf:getCanvas(), slide, gTemplate) + return new_drawable_image(surf:newImageSnapshot()) +end + +gMakeDrawable = convert_to_picture_drawable + function spawn_transition(prevSlide, nextSlide, is_forward) local transition if is_forward then @@ -275,14 +303,8 @@ function spawn_transition(prevSlide, nextSlide, is_forward) transition = fade_slide_transition end - local rec = Sk.newPictureRecorder() - - drawSlide(rec:beginRecording(640, 480), prevSlide, gTemplate) - local prevDrawable = new_drawable_picture(rec:endRecording()) - - drawSlide(rec:beginRecording(640, 480), nextSlide, gTemplate) - local nextDrawable = new_drawable_picture(rec:endRecording()) - + local prevDrawable = gMakeDrawable(prevSlide) + local nextDrawable = gMakeDrawable(nextSlide) gCurrAnimation = transition(prevDrawable, nextDrawable, is_forward) end diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp index dd79a34b90..ba8a4c841e 100644 --- a/src/utils/SkLua.cpp +++ b/src/utils/SkLua.cpp @@ -11,6 +11,7 @@ #include "GrReducedClip.h" #endif +#include "SkBlurImageFilter.h" #include "SkCanvas.h" #include "SkData.h" #include "SkDecodingImageGenerator.h" @@ -43,6 +44,7 @@ template const char* get_mtname(); DEF_MTNAME(SkCanvas) DEF_MTNAME(SkDocument) DEF_MTNAME(SkImage) +DEF_MTNAME(SkImageFilter) DEF_MTNAME(SkMatrix) DEF_MTNAME(SkRRect) DEF_MTNAME(SkPath) @@ -402,6 +404,10 @@ static SkScalar getfield_scalar_default(lua_State* L, int index, const char key[ return value; } +static SkScalar byte2unit(U8CPU byte) { + return byte / 255.0f; +} + static U8CPU unit2byte(SkScalar x) { if (x <= 0) { return 0; @@ -786,6 +792,16 @@ static int lpaint_isVerticalText(lua_State* L) { return 1; } +static int lpaint_getAlpha(lua_State* L) { + SkLua(L).pushScalar(byte2unit(get_obj(L, 1)->getAlpha())); + return 1; +} + +static int lpaint_setAlpha(lua_State* L) { + get_obj(L, 1)->setAlpha(unit2byte(lua2scalar(L, 2))); + return 0; +} + static int lpaint_getColor(lua_State* L) { SkLua(L).pushColor(get_obj(L, 1)->getColor()); return 1; @@ -959,17 +975,33 @@ static int lpaint_getEffects(lua_State* L) { const SkPaint* paint = get_obj(L, 1); lua_newtable(L); - setfield_bool_if(L, "looper", !!paint->getLooper()); - setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); - setfield_bool_if(L, "rasterizer", !!paint->getRasterizer()); - setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); - setfield_bool_if(L, "shader", !!paint->getShader()); + setfield_bool_if(L, "looper", !!paint->getLooper()); + setfield_bool_if(L, "pathEffect", !!paint->getPathEffect()); + setfield_bool_if(L, "rasterizer", !!paint->getRasterizer()); + setfield_bool_if(L, "maskFilter", !!paint->getMaskFilter()); + setfield_bool_if(L, "shader", !!paint->getShader()); setfield_bool_if(L, "colorFilter", !!paint->getColorFilter()); setfield_bool_if(L, "imageFilter", !!paint->getImageFilter()); - setfield_bool_if(L, "xfermode", !!paint->getXfermode()); + setfield_bool_if(L, "xfermode", !!paint->getXfermode()); return 1; } +static int lpaint_getImageFilter(lua_State* L) { + const SkPaint* paint = get_obj(L, 1); + SkImageFilter* imf = paint->getImageFilter(); + if (imf) { + push_ref(L, imf); + return 1; + } + return 0; +} + +static int lpaint_setImageFilter(lua_State* L) { + SkPaint* paint = get_obj(L, 1); + paint->setImageFilter(get_ref(L, 2)); + return 0; +} + static int lpaint_getShader(lua_State* L) { const SkPaint* paint = get_obj(L, 1); SkShader* shader = paint->getShader(); @@ -1010,6 +1042,8 @@ static const struct luaL_Reg gSkPaint_Methods[] = { { "isEmbeddedBitmapText", lpaint_isEmbeddedBitmapText }, { "isAutohinted", lpaint_isAutohinted }, { "isVerticalText", lpaint_isVerticalText }, + { "getAlpha", lpaint_getAlpha }, + { "setAlpha", lpaint_setAlpha }, { "getColor", lpaint_getColor }, { "setColor", lpaint_setColor }, { "getTextSize", lpaint_getTextSize }, @@ -1033,6 +1067,8 @@ static const struct luaL_Reg gSkPaint_Methods[] = { { "measureText", lpaint_measureText }, { "getFontMetrics", lpaint_getFontMetrics }, { "getEffects", lpaint_getEffects }, + { "getImageFilter", lpaint_getImageFilter }, + { "setImageFilter", lpaint_setImageFilter }, { "getShader", lpaint_getShader }, { "getPathEffect", lpaint_getPathEffect }, { "__gc", lpaint_gc }, @@ -1157,6 +1193,18 @@ static const struct luaL_Reg gSkPathEffect_Methods[] = { /////////////////////////////////////////////////////////////////////////////// +static int lpimagefilter_gc(lua_State* L) { + get_ref(L, 1)->unref(); + return 0; +} + +static const struct luaL_Reg gSkImageFilter_Methods[] = { + { "__gc", lpimagefilter_gc }, + { NULL, NULL } +}; + +/////////////////////////////////////////////////////////////////////////////// + static int lmatrix_getType(lua_State* L) { SkMatrix::TypeMask mask = get_obj(L, 1)->getType(); @@ -1688,6 +1736,19 @@ static int lsk_newDocumentPDF(lua_State* L) { } } +static int lsk_newBlurImageFilter(lua_State* L) { + SkScalar sigmaX = lua2scalar_def(L, 1, 0); + SkScalar sigmaY = lua2scalar_def(L, 2, 0); + SkImageFilter* imf = SkBlurImageFilter::Create(sigmaX, sigmaY); + if (NULL == imf) { + lua_pushnil(L); + } else { + push_ref(L, imf); + imf->unref(); + } + return 1; +} + static int lsk_newMatrix(lua_State* L) { push_new(L)->reset(); return 1; @@ -1776,6 +1837,7 @@ static void register_Sk(lua_State* L) { setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); setfield_function(L, "loadImage", lsk_loadImage); + setfield_function(L, "newBlurImageFilter", lsk_newBlurImageFilter); setfield_function(L, "newMatrix", lsk_newMatrix); setfield_function(L, "newPaint", lsk_newPaint); setfield_function(L, "newPath", lsk_newPath); @@ -1800,6 +1862,7 @@ void SkLua::Load(lua_State* L) { REG_CLASS(L, SkCanvas); REG_CLASS(L, SkDocument); REG_CLASS(L, SkImage); + REG_CLASS(L, SkImageFilter); REG_CLASS(L, SkPaint); REG_CLASS(L, SkPath); REG_CLASS(L, SkPathEffect); -- cgit v1.2.3