Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 017a742f authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 8618453 from 2b4e7652 to tm-qpr1-release

Change-Id: Ief3f03eabca011f182241500437b03cffc5ab8dd
parents 520aa4bd 2b4e7652
Loading
Loading
Loading
Loading
+98 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

namespace android::ftl {

template <typename, template <typename> class>
class Future;

namespace details {

template <typename T>
struct future_result {
  using type = T;
};

template <typename T>
struct future_result<std::future<T>> {
  using type = T;
};

template <typename T>
struct future_result<std::shared_future<T>> {
  using type = T;
};

template <typename T, template <typename> class FutureImpl>
struct future_result<Future<T, FutureImpl>> {
  using type = T;
};

template <typename T>
using future_result_t = typename future_result<T>::type;

struct ValueTag {};

template <typename, typename T, template <typename> class>
class BaseFuture;

template <typename Self, typename T>
class BaseFuture<Self, T, std::future> {
  using Impl = std::future<T>;

 public:
  Future<T, std::shared_future> share() {
    if (T* value = std::get_if<T>(&self())) {
      return {ValueTag{}, std::move(*value)};
    }

    return std::get<Impl>(self()).share();
  }

 protected:
  T get() {
    if (T* value = std::get_if<T>(&self())) {
      return std::move(*value);
    }

    return std::get<Impl>(self()).get();
  }

 private:
  auto& self() { return static_cast<Self&>(*this).future_; }
};

template <typename Self, typename T>
class BaseFuture<Self, T, std::shared_future> {
  using Impl = std::shared_future<T>;

 protected:
  const T& get() const {
    if (const T* value = std::get_if<T>(&self())) {
      return *value;
    }

    return std::get<Impl>(self()).get();
  }

 private:
  const auto& self() const { return static_cast<const Self&>(*this).future_; }
};

}  // namespace details
}  // namespace android::ftl
+85 −61
Original line number Diff line number Diff line
@@ -19,91 +19,115 @@
#include <future>
#include <type_traits>
#include <utility>
#include <variant>

#include <ftl/details/future.h>

namespace android::ftl {

// Creates a future that defers a function call until its result is queried.
// Thin wrapper around FutureImpl<T> (concretely std::future<T> or std::shared_future<T>) with
// extensions for pure values (created via ftl::yield) and continuations.
//
//   auto future = ftl::defer([](int x) { return x + 1; }, 99);
//   assert(future.get() == 100);
// See also SharedFuture<T> shorthand below.
//
template <typename F, typename... Args>
inline auto defer(F&& f, Args&&... args) {
  return std::async(std::launch::deferred, std::forward<F>(f), std::forward<Args>(args)...);
}
template <typename T, template <typename> class FutureImpl = std::future>
class Future final : public details::BaseFuture<Future<T, FutureImpl>, T, FutureImpl> {
  using Base = details::BaseFuture<Future, T, FutureImpl>;

// Creates a future that wraps a value.
//
//   auto future = ftl::yield(42);
//   assert(future.get() == 42);
//
//   auto ptr = std::make_unique<char>('!');
//   auto future = ftl::yield(std::move(ptr));
//   assert(*future.get() == '!');
//
template <typename T>
inline std::future<T> yield(T&& v) {
  return defer([](T&& v) { return std::forward<T>(v); }, std::forward<T>(v));
}
  friend Base;                                            // For BaseFuture<...>::self.
  friend details::BaseFuture<Future<T>, T, std::future>;  // For BaseFuture<...>::share.

namespace details {
 public:
  // Constructs an invalid future.
  Future() : future_(std::in_place_type<FutureImpl<T>>) {}

template <typename T>
struct future_result {
  using type = T;
};
  // Constructs a future from its standard counterpart, implicitly.
  Future(FutureImpl<T>&& f) : future_(std::move(f)) {}

template <typename T>
struct future_result<std::future<T>> {
  using type = T;
};
  bool valid() const {
    return std::holds_alternative<T>(future_) || std::get<FutureImpl<T>>(future_).valid();
  }

template <typename T>
using future_result_t = typename future_result<T>::type;
  // Forwarding functions. Base::share is only defined when FutureImpl is std::future, whereas the
  // following are defined for either FutureImpl:
  using Base::get;

// Attaches a continuation to a future. The continuation is a function that maps T to either R or
// std::future<R>. In the former case, the chain wraps the result in a future as if by ftl::yield.
  // Attaches a continuation to the future. The continuation is a function that maps T to either R
  // or ftl::Future<R>. In the former case, the chain wraps the result in a future as if by
  // ftl::yield.
  //
  //   auto future = ftl::yield(123);
//   std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
  //   ftl::Future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
  //
//   std::future<char> chain =
//       ftl::chain(std::move(future))
  //   auto chain =
  //       ftl::Future(std::move(future))
  //           .then([](int x) { return static_cast<std::size_t>(x % 2); })
  //           .then([&futures](std::size_t i) { return std::move(futures[i]); });
  //
  //   assert(chain.get() == 'b');
  //
template <typename T>
struct Chain {
  // Implicit conversion.
  Chain(std::future<T>&& f) : future(std::move(f)) {}
  operator std::future<T>&&() && { return std::move(future); }

  T get() && { return future.get(); }

  template <typename F, typename R = std::invoke_result_t<F, T>>
  auto then(F&& op) && -> Chain<future_result_t<R>> {
  auto then(F&& op) && -> Future<details::future_result_t<R>> {
    return defer(
        [](auto&& f, F&& op) {
          R r = op(f.get());
          if constexpr (std::is_same_v<R, future_result_t<R>>) {
          if constexpr (std::is_same_v<R, details::future_result_t<R>>) {
            return r;
          } else {
            return r.get();
          }
        },
        std::move(future), std::forward<F>(op));
        std::move(*this), std::forward<F>(op));
  }

  std::future<T> future;
};
 private:
  template <typename V>
  friend Future<V> yield(V&&);

  template <typename V, typename... Args>
  friend Future<V> yield(Args&&...);

}  // namespace details
  template <typename... Args>
  Future(details::ValueTag, Args&&... args)
      : future_(std::in_place_type<T>, std::forward<Args>(args)...) {}

  std::variant<T, FutureImpl<T>> future_;
};

template <typename T>
inline auto chain(std::future<T>&& f) -> details::Chain<T> {
  return std::move(f);
using SharedFuture = Future<T, std::shared_future>;

// Deduction guide for implicit conversion.
template <typename T, template <typename> class FutureImpl>
Future(FutureImpl<T>&&) -> Future<T, FutureImpl>;

// Creates a future that wraps a value.
//
//   auto future = ftl::yield(42);
//   assert(future.get() == 42);
//
//   auto ptr = std::make_unique<char>('!');
//   auto future = ftl::yield(std::move(ptr));
//   assert(*future.get() == '!');
//
template <typename V>
inline Future<V> yield(V&& value) {
  return {details::ValueTag{}, std::move(value)};
}

template <typename V, typename... Args>
inline Future<V> yield(Args&&... args) {
  return {details::ValueTag{}, std::forward<Args>(args)...};
}

// Creates a future that defers a function call until its result is queried.
//
//   auto future = ftl::defer([](int x) { return x + 1; }, 99);
//   assert(future.get() == 100);
//
template <typename F, typename... Args>
inline auto defer(F&& f, Args&&... args) {
  return Future(std::async(std::launch::deferred, std::forward<F>(f), std::forward<Args>(args)...));
}

}  // namespace android::ftl
+5 −5
Original line number Diff line number Diff line
@@ -42,9 +42,9 @@ TEST(Future, Example) {
  }
  {
    auto future = ftl::yield(123);
    std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
    ftl::Future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};

    std::future<char> chain = ftl::chain(std::move(future))
    ftl::Future<char> chain = ftl::Future(std::move(future))
                                  .then([](int x) { return static_cast<size_t>(x % 2); })
                                  .then([&futures](size_t i) { return std::move(futures[i]); });

@@ -71,7 +71,7 @@ TEST(Future, Chain) {
    return ByteVector{str.begin(), str.end()};
  });

  std::packaged_task<std::future<ByteVector>(ByteVector)> decrement_bytes(
  std::packaged_task<ftl::Future<ByteVector>(ByteVector)> decrement_bytes(
      [](ByteVector bytes) { return ftl::defer(decrement, std::move(bytes)); });

  auto fetch = fetch_string.get_future();
@@ -81,7 +81,7 @@ TEST(Future, Chain) {

  EXPECT_EQ(
      "hello, world",
      ftl::chain(std::move(fetch))
      ftl::Future(std::move(fetch))
          .then([](const char* str) { return std::string(str); })
          .then([&](std::string str) {
            auto append = append_string.get_future();
@@ -93,7 +93,7 @@ TEST(Future, Chain) {
            decrement_thread = std::thread(std::move(decrement_bytes), std::move(bytes));
            return decrement;
          })
          .then([](std::future<ByteVector> bytes) { return bytes; })
          .then([](ftl::Future<ByteVector> bytes) { return bytes; })
          .then([](const ByteVector& bytes) { return std::string(bytes.begin(), bytes.end()); })
          .get());

+2 −2
Original line number Diff line number Diff line
@@ -153,11 +153,11 @@ typedef struct {
    uint32_t uid;

    // The start time of the period in nanoseconds. The clock must be
    // CLOCK_MONOTONIC, as returned by the ktime_get_ns(void) function.
    // CLOCK_MONOTONIC_RAW, as returned by the ktime_get_raw_ns(void) function.
    uint64_t start_time_ns;

    // The end time of the period in nanoseconds. The clock must be
    // CLOCK_MONOTONIC, as returned by the ktime_get_ns(void) function.
    // CLOCK_MONOTONIC_RAW, as returned by the ktime_get_raw_ns(void) function.
    uint64_t end_time_ns;

    // The amount of time the GPU was running GPU work for |uid| during the
+99 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _UI_INPUT_DISPATCHER_DEBUG_CONFIG_H
#define _UI_INPUT_DISPATCHER_DEBUG_CONFIG_H

#define LOG_TAG "InputDispatcher"

#include <log/log.h>
#include <log/log_event_list.h>

namespace android::inputdispatcher {
/**
 * Log detailed debug messages about each inbound event notification to the dispatcher.
 * Enable this via "adb shell setprop log.tag.InputDispatcherInboundEvent DEBUG" (requires restart)
 */
const bool DEBUG_INBOUND_EVENT_DETAILS =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "InboundEvent", ANDROID_LOG_INFO);

/**
 * Log detailed debug messages about each outbound event processed by the dispatcher.
 * Enable this via "adb shell setprop log.tag.InputDispatcherOutboundEvent DEBUG" (requires restart)
 */
const bool DEBUG_OUTBOUND_EVENT_DETAILS =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "OutboundEvent", ANDROID_LOG_INFO);

/**
 * Log debug messages about the dispatch cycle.
 * Enable this via "adb shell setprop log.tag.InputDispatcherDispatchCycle DEBUG" (requires restart)
 */
const bool DEBUG_DISPATCH_CYCLE =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "DispatchCycle", ANDROID_LOG_INFO);

/**
 * Log debug messages about channel creation
 * Enable this via "adb shell setprop log.tag.InputDispatcherChannelCreation DEBUG" (requires
 * restart)
 */
const bool DEBUG_CHANNEL_CREATION =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "ChannelCreation", ANDROID_LOG_INFO);

/**
 * Log debug messages about input event injection.
 * Enable this via "adb shell setprop log.tag.InputDispatcherInjection DEBUG" (requires restart)
 */
const bool DEBUG_INJECTION =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Injection", ANDROID_LOG_INFO);

/**
 * Log debug messages about input focus tracking.
 * Enable this via "adb shell setprop log.tag.InputDispatcherFocus DEBUG" (requires restart)
 */
const bool DEBUG_FOCUS =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Focus", ANDROID_LOG_INFO);

/**
 * Log debug messages about touch mode event
 * Enable this via "adb shell setprop log.tag.InputDispatcherTouchMode DEBUG" (requires restart)
 */
const bool DEBUG_TOUCH_MODE =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "TouchMode", ANDROID_LOG_INFO);

/**
 * Log debug messages about touch occlusion
 * Enable this via "adb shell setprop log.tag.InputDispatcherTouchOcclusion DEBUG" (requires
 * restart)
 */
const bool DEBUG_TOUCH_OCCLUSION =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "TouchOcclusion", ANDROID_LOG_INFO);

/**
 * Log debug messages about the app switch latency optimization.
 * Enable this via "adb shell setprop log.tag.InputDispatcherAppSwitch DEBUG" (requires restart)
 */
const bool DEBUG_APP_SWITCH =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "AppSwitch", ANDROID_LOG_INFO);

/**
 * Log debug messages about hover events.
 * Enable this via "adb shell setprop log.tag.InputDispatcherHover DEBUG" (requires restart)
 */
const bool DEBUG_HOVER =
        __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Hover", ANDROID_LOG_INFO);
} // namespace android::inputdispatcher

#endif // _UI_INPUT_DISPATCHER_DEBUG_CONFIG_H
 No newline at end of file
Loading