From: Hermet Park Date: Fri, 10 May 2019 11:40:14 +0000 (+0900) Subject: evas image: Support android 9 patch feature. X-Git-Tag: submit/tizen/20190530.111225~149 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F46%2F205946%2F11;p=platform%2Fupstream%2Fefl.git evas image: Support android 9 patch feature. Change-Id: Ie80783b234ea5b6173965c975df007a3e301a2e7 --- diff --git a/src/lib/elementary/efl_ui_image.c b/src/lib/elementary/efl_ui_image.c index 8d13155..191a2c6 100644 --- a/src/lib/elementary/efl_ui_image.c +++ b/src/lib/elementary/efl_ui_image.c @@ -763,6 +763,10 @@ _efl_ui_image_sizing_eval(Evas_Object *obj) { if (!elm_widget_is_legacy(obj)) sd->scale = efl_gfx_entity_scale_get(obj) * elm_config_scale_get(); + + /* TIZEN_ONLY(20190507): Android style Nine-Patch feature */ + if (sd->img && !sd->edje) + evas_object_image_border_scale_set(sd->img, efl_gfx_entity_scale_get(obj) * elm_config_scale_get()); } /* END */ diff --git a/src/lib/evas/canvas/efl_canvas_image.c b/src/lib/evas/canvas/efl_canvas_image.c index 9b19be0..3e69668 100644 --- a/src/lib/evas/canvas/efl_canvas_image.c +++ b/src/lib/evas/canvas/efl_canvas_image.c @@ -39,6 +39,8 @@ _evas_image_file_load(Eo *eo_obj) return EINA_TRUE; } + tizen_9patch_apply(o, f); //TIZEN_ONLY(190509): Android style Nine-Patch feature + obj = efl_data_scope_get(eo_obj, EFL_CANVAS_OBJECT_CLASS); evas_object_async_block(obj); _evas_image_init_set(f, key, eo_obj, obj, o, &lo); diff --git a/src/lib/evas/canvas/evas_image_private.h b/src/lib/evas/canvas/evas_image_private.h index 1137524..de2131b 100755 --- a/src/lib/evas/canvas/evas_image_private.h +++ b/src/lib/evas/canvas/evas_image_private.h @@ -49,6 +49,10 @@ struct _Evas_Object_Image_Load_Opts } scale_load; Eina_Bool orientation : 1; Eina_Bool can_load_colormap : 1; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + Eina_Bool tizen_9patch_enabled : 1; + Eina_Bool tizen_9patch_fetched :1; }; struct _Evas_Object_Image_Pixels @@ -228,6 +232,17 @@ void *_evas_image_data_convert_internal(Evas_Image_Data *o, void *data, Evas_Col void _evas_image_unload(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj, Eina_Bool dirty); void _evas_image_load(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj, Evas_Image_Data *o); +/* TIZEN_ONLY(190509): Android style Nine-Patch feature */ +typedef struct +{ + int l, r, t, b; +} tizen_9patch_data; + +extern Eina_Hash *tizen_9patch_hash; +void tizen_9patch_apply(Evas_Image_Data *o, const Eina_File *f); +void tizen_9patch_fetch(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj, Evas_Image_Data *o); +// + # define EINA_COW_IMAGE_STATE_WRITE_BEGIN(Obj, Write) \ EINA_COW_WRITE_BEGIN(evas_object_image_state_cow, Obj->cur, Evas_Object_Image_State, Write) diff --git a/src/lib/evas/canvas/evas_main.c b/src/lib/evas/canvas/evas_main.c index c6a3fb5..7986dac 100644 --- a/src/lib/evas/canvas/evas_main.c +++ b/src/lib/evas/canvas/evas_main.c @@ -180,6 +180,9 @@ evas_init(void) _efl_gfx_image_load_error_init(); + /* TIZEN_ONLY(190509): Android style Nine-Patch feature */ + tizen_9patch_hash = eina_hash_string_superfast_new(free); + return _evas_init_count; //TIZEN_ONLY(20171114) : support evas gl thread @@ -219,6 +222,11 @@ evas_shutdown(void) if (--_evas_init_count != 0) return _evas_init_count; + /* TIZEN_ONLY(190509): Android style Nine-Patch feature */ + eina_hash_free(tizen_9patch_hash); + tizen_9patch_hash = NULL; + // + eina_log_timing(_evas_log_dom_global, EINA_LOG_STATE_START, EINA_LOG_STATE_SHUTDOWN); diff --git a/src/lib/evas/canvas/evas_object_image.c b/src/lib/evas/canvas/evas_object_image.c index 573d11a..3c1729a 100755 --- a/src/lib/evas/canvas/evas_object_image.c +++ b/src/lib/evas/canvas/evas_object_image.c @@ -81,7 +81,8 @@ static const Evas_Object_Func object_func = }; static const Evas_Object_Image_Load_Opts default_load_opts = { - 0, 0.0, 0, 0, { 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, 0 + 0, 0.0, 0, 0, { 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, 0, + EINA_FALSE, EINA_FALSE //TIZEN_ONLY(190507): Android style Nine-Patch feature }; static const Evas_Object_Image_Pixels default_pixels = { @@ -539,6 +540,13 @@ _efl_canvas_image_internal_efl_gfx_image_border_set(Eo *eo_obj, Evas_Image_Data state_write->border.b = b; } EINA_COW_IMAGE_STATE_WRITE_END(o, state_write); + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_enabled = EINA_FALSE; + load_write->tizen_9patch_fetched = EINA_FALSE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); + o->changed = EINA_TRUE; evas_object_change(eo_obj, obj); } @@ -2366,6 +2374,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = bl; inh = bt; outx = ox; outy = oy; outw = bsl; outh = bst; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; ++iny; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .##. // | | @@ -2374,6 +2389,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = imw - bl - br; inh = bt; outx = ox + bsl; outy = oy; outw = iw - bsl - bsr; outh = bst; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; ++iny; inw -=2; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--# // | | @@ -2382,6 +2404,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = br; inh = bt; outx = ox + iw - bsr; outy = oy; outw = bsr; outh = bst; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + --inx; ++iny; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--. // # | @@ -2390,6 +2419,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = bl; inh = imh - bt - bb; outx = ox; outy = oy + bst; outw = bsl; outh = ih - bst - bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; ++iny; inh -= 2; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--. // |##| @@ -2400,6 +2436,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = imw - bl - br; inh = imh - bt - bb; outx = ox + bsl; outy = oy + bst; outw = iw - bsl - bsr; outh = ih - bst - bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; ++iny; inw -= 2; inh -=2; + } + if ((o->cur->border.fill == EVAS_BORDER_FILL_SOLID) && (obj->cur->cache.clip.a == 255) && (!obj->clip.mask) && @@ -2419,6 +2462,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = br; inh = imh - bt - bb; outx = ox + iw - bsr; outy = oy + bst; outw = bsr; outh = ih - bst - bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + --inx; ++iny; inh -= 2; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--. // | | @@ -2427,6 +2477,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = bl; inh = bb; outx = ox; outy = oy + ih - bsb; outw = bsl; outh = bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; --iny; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--. // | | @@ -2435,6 +2492,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = imw - bl - br; inh = bb; outx = ox + bsl; outy = oy + ih - bsb; outw = iw - bsl - bsr; outh = bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + ++inx; --iny; inw -= 2; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); // .--. // | | @@ -2443,6 +2507,13 @@ _evas_image_render(Eo *eo_obj, Evas_Object_Protected_Data *obj, inw = br; inh = bb; outx = ox + iw - bsr; outy = oy + ih - bsb; outw = bsr; outh = bsb; + + /* TIZEN_ONLY(190507): Android style Nine-Patch feature */ + if (o->load_opts->tizen_9patch_fetched) + { + --inx; --iny; + } + _draw_image(obj, engine, output, context, surface, pixels, inx, iny, inw, inh, outx, outy, outw, outh, o->cur->smooth_scale, do_async); } idy += idh; @@ -2477,6 +2548,8 @@ evas_object_image_render_pre(Evas_Object *eo_obj, if ((o->cur->fill.w < 1) || (o->cur->fill.h < 1)) return; + tizen_9patch_fetch(eo_obj, obj, o); //TIZEN_ONLY(190509): Android style Nine-Patch feature + /* if someone is clipping this obj - go calculate the clipper */ if (obj->cur->clipper) { @@ -3745,6 +3818,197 @@ _efl_canvas_image_internal_efl_object_debug_name_override(Eo *eo_obj, Evas_Image } } +/* TIZEN_ONLY(190509): Android style Nine-Patch feature */ +Eina_Hash *tizen_9patch_hash = NULL; + +void +tizen_9patch_apply(Evas_Image_Data *o, const Eina_File *f) +{ + const char *filename = eina_file_filename_get(f); + if (!filename) return; + + int len = strlen(filename); + if (len <= 6) return; + + //Support only png format + if (!strncmp(filename + (len - 6),".#.png", 6)) + { + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_enabled = EINA_TRUE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); + } + else + { + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_enabled = EINA_FALSE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); + } +} + +void +tizen_9patch_fetch(Evas_Object *eo_obj, Evas_Object_Protected_Data *obj, Evas_Image_Data *o) +{ + const char *filename; + tizen_9patch_data *t9d = NULL; + + if (!o->load_opts->tizen_9patch_enabled || o->load_opts->tizen_9patch_fetched) return; + + //Possibly user set borders manually... + if ((o->cur->border.l > 0) || (o->cur->border.r > 0) || + (o->cur->border.t > 0) || (o->cur->border.b > 0)) + goto disable_9patch; + + //Try cached 9patch info. + filename = eina_file_filename_get(o->cur->f); + + if (filename) + { + t9d = eina_hash_find(tizen_9patch_hash, filename); + if (t9d) + { + EINA_COW_IMAGE_STATE_WRITE_BEGIN(o, state_write) + { + state_write->border.l = t9d->l; + state_write->border.r = t9d->r; + state_write->border.t = t9d->t; + state_write->border.b = t9d->b; + } + EINA_COW_IMAGE_STATE_WRITE_END(o, state_write); + + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_fetched = EINA_TRUE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); + + return; + } + } + + + void *output = _evas_object_image_output_find(obj); + if (!output) goto err; + + int imagew, imageh, uvw, uvh; + void *image = _evas_image_pixels_get(eo_obj, obj, ENC, output, NULL, NULL, 0, 0, + &imagew, &imageh, &uvw, &uvh, EINA_TRUE, EINA_FALSE); + if (!image) goto err; + + //We assume the 9 patch image is PNG + Evas_Colorspace cspace = ENFN->image_file_colorspace_get(ENC, image); + if (cspace != EVAS_COLORSPACE_ARGB8888) + { + ERR("Only allow PNG format as the 9 patch image!, obj = %p", obj); + goto disable_9patch; + } + + DATA32 *pixels = NULL; + + if (ENFN->image_data_direct_get) + { + Eina_Slice sl; + if (ENFN->image_data_direct_get(ENC, image, 0, &sl, &cspace, EINA_TRUE, NULL)) + pixels = (DATA32 *)sl.mem; + } + if (!pixels) + { + //Try other methods? + goto err; + } + + //Start fetching... + DATA32 *p; + int l = 0, r = 0, t = 0, b = 0; + + //Left border + p = pixels; + for (int i = 0; i < imagew; ++i) + { + if (*p == 0xff000000) + { + l = (i - 1); + break; + } + ++p; + } + //Right border + p = pixels + (imagew - 1); + for (int i = 0; i < imagew; ++i) + { + if (*p == 0xff000000) + { + r = (i - 1); + break; + } + --p; + } + //Top border + p = pixels; + for (int i = 0; i < imageh; ++i) + { + if (*p == 0xff000000) + { + t = (i - 1); + break; + } + p += imagew; + } + //Bottom border + p = pixels + (imagew * (imageh - 1)); + for (int i = 0; i < imageh; ++i) + { + if (*p == 0xff000000) + { + b = (i - 1); + break; + } + p -= imagew; + } + + //Verify + if ((l < 0) || (r < 0) || (t < 0) || (b < 0) || + (l + r >= imagew) || (t + b >= imageh)) + goto err; + + //Setup border size + EINA_COW_IMAGE_STATE_WRITE_BEGIN(o, state_write) + { + state_write->border.l = l; + state_write->border.r = r; + state_write->border.t = t; + state_write->border.b = b; + } + EINA_COW_IMAGE_STATE_WRITE_END(o, state_write); + + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_fetched = EINA_TRUE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); + + //Cache 9patch info + if (filename) + { + t9d = (tizen_9patch_data*) malloc(sizeof(tizen_9patch_data)); + if (!t9d) return; + t9d->l = l; + t9d->r = r; + t9d->t = t; + t9d->b = b; + if (!eina_hash_add(tizen_9patch_hash, filename, t9d)) + { + ERR("Filed to add a 9patch cache data!"); + free(t9d); + } + } + return; + +err: + ERR("Couldn't fetch 9 patch!, obj = %p", obj); + +disable_9patch: + EINA_COW_LOAD_OPTS_WRITE_BEGIN(o, load_write) + load_write->tizen_9patch_enabled = EINA_FALSE; + EINA_COW_LOAD_OPTS_WRITE_END(o, load_write); +} +// + #define EFL_CANVAS_IMAGE_INTERNAL_EXTRA_OPS \ EFL_OBJECT_OP_FUNC(efl_dbg_info_get, _efl_canvas_image_internal_efl_object_dbg_info_get)