<commit_before>/** @file hybris-fb.c
 *
 * mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
 * <p>
 * Copyright (C) 2013-2017 Jolla Ltd.
 * <p>
 * @author <NAME> <<EMAIL>>
 *
 * mce-plugin-libhybris is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * mce-plugin-libhybris is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with mce-plugin-libhybris; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "hybris-fb.h"
#include "plugin-logging.h"

#include "plugin-api.h"

#include <system/window.h>
#include <hardware/fb.h>

/* ========================================================================= *
 * PROTOTYPES
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * FRAMEBUFFER_PLUGIN
 * ------------------------------------------------------------------------- */

bool        hybris_plugin_fb_load         (void);
void        hybris_plugin_fb_unload       (void);

static int  hybris_plugin_fb_open_device  (struct framebuffer_device_t **pdevice);
static void hybris_plugin_fb_close_device (struct framebuffer_device_t **pdevice);

/* ------------------------------------------------------------------------- *
 * FRAMEBUFFER_DEVICE
 * ------------------------------------------------------------------------- */

bool        hybris_device_fb_init         (void);
void        hybris_device_fb_quit         (void);
bool        hybris_device_fb_set_power    (bool state);

/* ========================================================================= *
 * FRAMEBUFFER_PLUGIN
 * ========================================================================= */

/** Handle for libhybris framebuffer plugin */
static const  struct hw_module_t *hybris_plugin_fb_handle = 0;

/** Load libhybris framebuffer plugin
 *
 * @return true on success, false on failure
 */
bool
hybris_plugin_fb_load(void)
{
  static bool done = false;

  if( done ) {
    goto cleanup;
  }

  done = true;

  hw_get_module(GRALLOC_HARDWARE_FB0, &hybris_plugin_fb_handle);

  if( !hybris_plugin_fb_handle ) {
    mce_log(LL_WARN, "failed to open frame buffer module");
    goto cleanup;
  }

  mce_log(LL_DEBUG, "hybris_plugin_fb_handle = %p",
          hybris_plugin_fb_handle);

cleanup:

  return hybris_plugin_fb_handle != 0;
}

/** Unload libhybris framebuffer plugin
 */
void
hybris_plugin_fb_unload(void)
{
  /* Close devices related to the plugin */
  hybris_device_fb_quit();

  /* actually unload the module */
  // FIXME: how to unload libhybris modules?
}

/** Convenience function for opening frame buffer device
 *
 * Similar to what we might or might not have available from hardware/fb.h
 */
static int
hybris_plugin_fb_open_device(struct framebuffer_device_t ** pdevice)
{
  int ack = false;

  if( !hybris_plugin_fb_load() ) {
    goto cleanup;
  }

  const struct hw_module_t *mod = hybris_plugin_fb_handle;
  const char               *id  = GRALLOC_HARDWARE_FB0;
  struct hw_device_t       *dev = 0;

  if( !mod->methods->open(mod, id, &dev) ) {
    goto cleanup;
  }

  *pdevice = (struct framebuffer_device_t*)dev;

  ack = true;

cleanup:

  return ack;
}

/** Convenience function for closing frame buffer device
 *
 * Similar to what we might or might not have available from hardware/fb.h
 */
static void
hybris_plugin_fb_close_device(struct framebuffer_device_t **pdevice)
{
  struct framebuffer_device_t *device;

  if( (device = *pdevice) ) {
    *pdevice = 0, device->common.close(&device->common);
  }
}

/* ========================================================================= *
 * FRAMEBUFFER_DEVICE
 * ========================================================================= */

/** Pointer to libhybris frame buffer device object */
static struct framebuffer_device_t *hybris_device_fb_handle = 0;

/** Initialize libhybris frame buffer device object
 *
 * @return true on success, false on failure
 */
bool
hybris_device_fb_init(void)
{
  static bool done = false;

  if( done ) {
    goto cleanup;
  }

  done = true;

  hybris_plugin_fb_open_device(&hybris_device_fb_handle);

  if( !hybris_device_fb_handle ) {
    mce_log(LL_ERR, "failed to open framebuffer device");
    goto cleanup;
  }

  mce_log(LL_DEBUG, "hybris_device_fb_handle = %p",
          hybris_device_fb_handle);

cleanup:

  return hybris_device_fb_handle != 0;
}

/** Release libhybris frame buffer device object
 */
void
hybris_device_fb_quit(void)
{
  hybris_plugin_fb_close_device(&hybris_device_fb_handle);
}

/** Set frame buffer power state via libhybris
 *
 * @param state true to power on, false to power off
 *
 * @return true on success, false on failure
 */
bool
hybris_device_fb_set_power(bool state)
{
  bool ack = false;

  if( !hybris_device_fb_init() ) {
    goto cleanup;
  }

  if( hybris_device_fb_handle->enableScreen(hybris_device_fb_handle, state) < 0 ) {
    goto cleanup;
  }

  ack = true;

cleanup:

  return ack;
}<commit_msg>[hybris-fb] Add support for blank/unblank via hw composer. JB#42611<commit_after>/** @file hybris-fb.c
 *
 * mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
 * <p>
 * Copyright (C) 2013-2017 Jolla Ltd.
 * <p>
 * @author <NAME> <<EMAIL>>
 *
 * mce-plugin-libhybris is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * mce-plugin-libhybris is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with mce-plugin-libhybris; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "hybris-fb.h"
#include "plugin-logging.h"

#include "plugin-api.h"

#include <system/window.h>
#include <hardware/gralloc.h>
#include <hardware/fb.h>
#include <hardware/hwcomposer.h>

/* ========================================================================= *
 * PROTOTYPES
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * FRAMEBUFFER_PLUGIN
 * ------------------------------------------------------------------------- */

bool        hybris_plugin_fb_load         (void);
void        hybris_plugin_fb_unload       (void);

/* ------------------------------------------------------------------------- *
 * FRAMEBUFFER_DEVICE
 * ------------------------------------------------------------------------- */

bool        hybris_device_fb_init         (void);
void        hybris_device_fb_quit         (void);
bool        hybris_device_fb_set_power    (bool state);

/* ========================================================================= *
 * FRAMEBUFFER_PLUGIN
 * ========================================================================= */

/** Handle for libhybris framebuffer plugin */
static const  struct hw_module_t *hybris_plugin_fb_handle = 0;

/** Handle for libhybris hw composer plugin */
static const  struct hw_module_t *hybris_plugin_hwc_handle = 0;

/** Load libhybris framebuffer plugin
 *
 * @return true on success, false on failure
 */
bool
hybris_plugin_fb_load(void)
{
  static bool done = false;

  if( done ) {
    goto cleanup;
  }

  done = true;

  /* Load framebuffer module */
  hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hybris_plugin_fb_handle);
  if( !hybris_plugin_fb_handle ) {
    mce_log(LL_DEBUG, "failed to open frame buffer module");
  }

  /* Load hw composer module */
  hw_get_module(HWC_HARDWARE_MODULE_ID, &hybris_plugin_hwc_handle);
  if( !hybris_plugin_hwc_handle ) {
    mce_log(LL_DEBUG, "failed to open hw composer module");
  }

  /* Both fb and hwc are optional, but having neither is unexpected */
  if( !hybris_plugin_fb_handle && !hybris_plugin_hwc_handle ) {
    mce_log(LL_WARN, "could not open neither fb nor hwc module");
  }

cleanup:
  return hybris_plugin_fb_handle || hybris_plugin_hwc_handle;
}

/** Unload libhybris framebuffer plugin
 */
void
hybris_plugin_fb_unload(void)
{
  /* Close open devices */
  hybris_device_fb_quit();

  /* Unload modules */
  // FIXME: how to unload libhybris modules?
  hybris_plugin_fb_handle = 0;
  hybris_plugin_hwc_handle = 0;
}

/* ========================================================================= *
 * FRAMEBUFFER_DEVICE
 * ========================================================================= */

/** Pointer to libhybris frame buffer device object */
static framebuffer_device_t *hybris_device_fb_handle = 0;

/** Pointer to libhybris frame buffer device object */
static hwc_composer_device_1_t *hybris_device_hwc_handle = 0;

/** Initialize libhybris frame buffer device object
 *
 * @return true on success, false on failure
 */
bool
hybris_device_fb_init(void)
{
  static bool ack = false;
  static bool done = false;

  if( done ) {
    goto cleanup;
  }

  done = true;

  if( !hybris_plugin_fb_load() ) {
    goto cleanup;
  }

  /* Open frame buffer device */
  if( hybris_plugin_fb_handle ) {
    hybris_plugin_fb_handle->methods->open(hybris_plugin_fb_handle,
                                           GRALLOC_HARDWARE_FB0,
                                           (hw_device_t**)&hybris_device_fb_handle);
    if( !hybris_device_fb_handle ) {
      mce_log(LL_WARN, "failed to open frame buffer device");
    }
  }

  /* Open hw composer device */
  if( hybris_plugin_hwc_handle ) {
    hybris_plugin_hwc_handle->methods->open(hybris_plugin_hwc_handle,
                                            HWC_HARDWARE_COMPOSER,
                                            (hw_device_t**)&hybris_device_hwc_handle);
    if( !hybris_device_hwc_handle ) {
      mce_log(LL_WARN, "failed to open hw composer device");
    }
  }

  /* What we'd like to use is:
   *
   * 1. hwc_dev->setPowerMode()
   * 2. hwc_dev->blank()
   * 2. fb_dev->enableScreen()
   *
   * While all are optional, having none available is unexpected.
   */

  if( hybris_device_hwc_handle ) {
#ifdef HWC_DEVICE_API_VERSION_1_4
    if( hybris_device_hwc_handle->common.version >= HWC_DEVICE_API_VERSION_1_4 &&
        hybris_device_hwc_handle->setPowerMode ) {
      mce_log(LL_DEBUG, "using hw composer setPowerMode() method");
      ack = true;
      goto cleanup;
    }
#endif
    if( hybris_device_hwc_handle->blank ) {
      mce_log(LL_DEBUG, "using hw composer blank() method");
      ack = true;
      goto cleanup;
    }
  }

  if( hybris_device_fb_handle ) {
    if( hybris_device_fb_handle->enableScreen ) {
      mce_log(LL_DEBUG, "using framebuffer enableScreen() method");
      ack = true;
      goto cleanup;
    }
  }

  mce_log(LL_WARN, "no known display power control interfaces");

cleanup:

  return ack;
}

/** Release libhybris frame buffer device object
 */
void
hybris_device_fb_quit(void)
{
  if( hybris_device_hwc_handle ) {
    hybris_device_hwc_handle->common.close(&hybris_device_hwc_handle->common);
    hybris_device_hwc_handle = 0;
  }

  if( hybris_device_fb_handle ) {
    hybris_device_fb_handle->common.close(&hybris_device_fb_handle->common);
    hybris_device_fb_handle = 0;
  }
}

/** Set frame buffer power state via libhybris
 *
 * @param state true to power on, false to power off
 *
 * @return true on success, false on failure
 */
bool
hybris_device_fb_set_power(bool state)
{
  bool ack = false;

  if( !hybris_device_fb_init() ) {
    goto cleanup;
  }

  /* Try hwc methods */
  if( hybris_device_hwc_handle ) {
#ifdef HWC_DEVICE_API_VERSION_1_4
    if( hybris_device_hwc_handle->common.version >= HWC_DEVICE_API_VERSION_1_4 &&
        hybris_device_hwc_handle->setPowerMode ) {
      int disp = 0;
      int mode = state ? HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF;
      int err = hybris_device_hwc_handle->setPowerMode(hybris_device_hwc_handle,
                                                       disp, mode);
      mce_log(err ? LL_WARN : LL_DEBUG, "setPowerMode(%d, %d) -> err=%d ",
              disp, mode, err);
      ack = !err;
      goto cleanup;
    }
#endif
    if( hybris_device_hwc_handle->blank ) {
      int disp = 0;
      int blank = state ? false : true;
      int err = hybris_device_hwc_handle->blank(hybris_device_hwc_handle,
                                                disp, blank);
      mce_log(err ? LL_WARN : LL_DEBUG, "blank(%d, %d) -> err=%d ",
              disp, blank, err);
      ack = !err;
      goto cleanup;
    }
  }

  /* Try fb methods */
  if( hybris_device_fb_handle ) {
    if( hybris_device_fb_handle->enableScreen ) {
      int err = hybris_device_fb_handle->enableScreen(hybris_device_fb_handle,
                                                      state);
      mce_log(err ? LL_WARN : LL_DEBUG, "enableScreen(%d) -> err=%d ",
              state, err);
      ack = !err;
      goto cleanup;
    }
  }

  /* Failed */
  mce_log(LL_WARN, "no known display power control interfaces");

cleanup:

  return ack;
}
{'fraction_non_alphanumeric': 0.19355545813239808, 'fraction_numerical': 0.005772322080958644, 'mean_word_length': 2.8642010163749294, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278564', 'n_tokens_mistral': 4255, 'n_tokens_neox': 4074, 'n_words': 1058}