Add a new API to get OPR 40/120140/12
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 21 Mar 2017 23:55:34 +0000 (08:55 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 22 Mar 2017 08:23:34 +0000 (17:23 +0900)
If the application uses the API, the screenshot privilege is needed.

Privilege:
 - http://tizen.org/privilege/screenshot

Change-Id: I0cd0523d64f9f6e345319151ce44719fd27284e7
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
packaging/libwidget_viewer.spec
watch-control/CMakeLists.txt
watch-control/include/watch_control.h
watch-control/src/control.c

index 601183a..f04ee13 100644 (file)
@@ -37,6 +37,7 @@ BuildRequires: pkgconfig(ecore-wayland)
 BuildRequires: pkgconfig(wayland-tbm-client)
 BuildRequires: pkgconfig(screen_connector_watcher)
 BuildRequires: pkgconfig(screen_connector_watcher_evas)
+BuildRequires: pkgconfig(capi-ui-efl-util)
 
 %description
 API for creating a new instance of the widget and managing its life-cycle.
index 83bbe71..a83ccaf 100644 (file)
@@ -17,6 +17,7 @@ pkg_check_modules(watch-control REQUIRED
        capi-appfw-application
        aul
        screen_connector_watcher_evas
+       capi-ui-efl-util
 )
 
 SET(BUILD_SOURCE
index 4469f16..c314c35 100644 (file)
@@ -42,6 +42,16 @@ extern int watch_policy_set_size_hint(watch_policy_size_hint hint);
 extern int watch_manager_set_resource_id(int resource_id);
 extern int watch_manager_get_resource_id(Evas_Object *watch, int *resource_id);
 
+/**
+ * @brief Gets the opr of the Evas Object
+ * @since_tizen 4.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/screenshot
+ * @param[out] opr The OPR(On Pixel Ratio)
+ * @return @c 0 on success, otherwise a negative error value
+ */
+extern int watch_manager_get_opr(float *opr);
+
 #ifdef __cplusplus
 }
 #endif
index 9c716b6..1c96f06 100644 (file)
@@ -24,6 +24,9 @@
 #include <unistd.h>
 #include "watch_control.h"
 #include <aul.h>
+#include <efl_util.h>
+#include <Elementary.h>
+#include <system_info.h>
 
 #define API __attribute__((visibility("default")))
 
@@ -38,6 +41,8 @@
 #define _D LOGD
 #endif
 
+#define KEY_SCREEN_SHAPE_CIRCLE "http://tizen.org/feature/screen.shape.circle"
+
 static int __watch_viewer_initialized = 0;
 static int __watch_size_policy = WATCH_POLICY_HINT_EXPAND;
 
@@ -84,8 +89,10 @@ static int __watch_viewer_init(Evas_Object *win)
 
 static void __watch_viewer_fini()
 {
-       if (__win)
+       if (__win) {
                evas_object_event_callback_del(__win, EVAS_CALLBACK_RESIZE, __win_resized);
+               __win = NULL;
+       }
 
        screen_connector_toolkit_evas_stop_visibility_notify();
        screen_connector_toolkit_evas_fini(SCREEN_CONNECTOR_SCREEEN_TYPE_WATCH);
@@ -122,6 +129,7 @@ static void __screen_connector_toolkit_evas_added_cb(const char *appid, const ch
        _D("obj added");
        _D("w: %d, h: %d, x: %d y: %d", w, h, x, y);
        evas_object_smart_callback_call(__win, WATCH_SMART_SIGNAL_ADDED, image);
+       evas_object_data_set(__win, "tbm,watch", image);
 }
 
 static void __screen_connector_toolkit_evas_removed_cb(const char *appid, const char *instance_id, int pid,
@@ -129,6 +137,7 @@ static void __screen_connector_toolkit_evas_removed_cb(const char *appid, const
 {
        _D("obj removed");
        evas_object_smart_callback_call(__win, WATCH_SMART_SIGNAL_REMOVED, image);
+       evas_object_data_set(__win, "tbm,watch", NULL);
 }
 
 static void __screen_connector_toolkit_evas_updated_cb(const char *appid, const char *instance_id, int pid,
@@ -246,3 +255,126 @@ API int watch_policy_set_size_hint(watch_policy_size_hint hint)
        return 0;
 }
 
+static float __get_opr(void *source_data, int width, int height)
+{
+       int *source;
+       int x;
+       int y;
+       int idx;
+       int max_rad;
+       int rad;
+       int pos;
+       unsigned int r = 0;
+       unsigned int g = 0;
+       unsigned int b = 0;
+       unsigned int pixel_sum;
+       float opr;
+       float max_opr;
+       bool shape_circle = false;
+
+       system_info_get_platform_bool(KEY_SCREEN_SHAPE_CIRCLE, &shape_circle);
+
+       source = source_data;
+       pos = width / 2;
+       max_rad = (height * height) >> 2;
+
+       for (y = 0; y < height; ++y) {
+               for (x = 0; x < width; ++x) {
+                       if (shape_circle) {
+                               rad = (pos - x) * (pos - x) + (pos - y) * (pos - y);
+                               if (rad <= max_rad) {
+                                       idx = y * width + x;
+                                       r += ((source[idx] & 0x00ff0000) >> 16);
+                                       g += ((source[idx] & 0x0000ff00) >> 8);
+                                       b += (source[idx] & 0x000000ff);
+                               }
+                       } else {
+                               idx = y * width + x;
+                               r += ((source[idx] & 0x00ff0000) >> 16);
+                               g += ((source[idx] & 0x0000ff00) >> 8);
+                               b += (source[idx] & 0x000000ff);
+                       }
+               }
+       }
+
+       if (shape_circle)
+               max_opr = (width / 2) * (height / 2) * 3.14 * 3 * 255;
+       else
+               max_opr = width * height * 3 * 255;
+
+       pixel_sum = r + g + b;
+       opr = ((float)pixel_sum) / max_opr;
+
+       return opr;
+}
+
+API int watch_manager_get_opr(float *opr)
+{
+       Evas_Object *image;
+       efl_util_screenshot_h screenshot;
+       tbm_surface_h surface;
+       tbm_surface_info_s info = { 0, };
+       void *source_data;
+       int width = 0;
+       int height = 0;
+       int ret;
+
+       if (opr == NULL) {
+               _E("Invalid parameter");
+               return -1;
+       }
+
+       if (__win == NULL) {
+               _E("Window is NULL");
+               return -1;
+       }
+
+       image = evas_object_data_get(__win, "tbm,watch");
+       if (image == NULL) {
+               _E("Failed to get image");
+               return -1;
+       }
+
+       evas_object_geometry_get(image, NULL, NULL, &width, &height);
+       _D("width(%d), height(%d)", width, height);
+
+       screenshot = efl_util_screenshot_initialize(width, height);
+       if (screenshot == NULL) {
+               _E("Failed to initialize efl util screenshot - %d",
+                               get_last_result());
+               return -1;
+       }
+
+       surface = efl_util_screenshot_take_tbm_surface(screenshot);
+       if (surface == NULL) {
+               _E("Failed to take tbm surface - %d", get_last_result());
+               efl_util_screenshot_deinitialize(screenshot);
+               return -1;
+       }
+
+       ret = tbm_surface_map(surface, TBM_SURF_OPTION_READ, &info);
+       if (ret != TBM_SURFACE_ERROR_NONE) {
+               _E("Failed to map tbm surface - %d", ret);
+               tbm_surface_destroy(surface);
+               efl_util_screenshot_deinitialize(screenshot);
+               return -1;
+       }
+
+       source_data = info.planes[0].ptr;
+       if (source_data == NULL) {
+               _E("Failed to get source");
+               tbm_surface_unmap(surface);
+               tbm_surface_destroy(surface);
+               efl_util_screenshot_deinitialize(screenshot);
+               return -1;
+       }
+
+       *opr = __get_opr(source_data, width, height);
+       _D("On Pixel Ratio(%f)", *opr);
+
+       tbm_surface_unmap(surface);
+       tbm_surface_destroy(surface);
+       efl_util_screenshot_deinitialize(screenshot);
+
+       return 0;
+}