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

Commit 4e2b71fe authored by Dominik Laskowski's avatar Dominik Laskowski
Browse files

FTL: Import std::future utilities from SF

Now including docs and expanded tests.

Bug: 160012986
Test: ftl_test
Change-Id: If9eb25646bb33ebc417ea87e6718b46fe0b87cf3
parent e21dbed9
Loading
Loading
Loading
Loading

include/ftl/future.h

0 → 100644
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 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

#include <future>
#include <type_traits>
#include <utility>

namespace android::ftl {

// 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 std::async(std::launch::deferred, std::forward<F>(f), std::forward<Args>(args)...);
}

// 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));
}

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>
using future_result_t = typename future_result<T>::type;

// 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.
//
//   auto future = ftl::yield(123);
//   std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};
//
//   std::future<char> chain =
//       ftl::chain(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>> {
    return defer(
        [](auto&& f, F&& op) {
          R r = op(f.get());
          if constexpr (std::is_same_v<R, future_result_t<R>>) {
            return r;
          } else {
            return r.get();
          }
        },
        std::move(future), std::forward<F>(op));
  }

  std::future<T> future;
};

}  // namespace details

template <typename T>
inline auto chain(std::future<T>&& f) -> details::Chain<T> {
  return std::move(f);
}

}  // namespace android::ftl
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ cc_test {
        address: true,
    },
    srcs: [
        "future_test.cpp",
        "small_map_test.cpp",
        "small_vector_test.cpp",
        "static_vector_test.cpp",
+105 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 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.
 */

#include <ftl/future.h>
#include <gtest/gtest.h>

#include <algorithm>
#include <future>
#include <string>
#include <thread>
#include <vector>

namespace android::test {

// Keep in sync with example usage in header file.
TEST(Future, Example) {
  {
    auto future = ftl::defer([](int x) { return x + 1; }, 99);
    EXPECT_EQ(future.get(), 100);
  }
  {
    auto future = ftl::yield(42);
    EXPECT_EQ(future.get(), 42);
  }
  {
    auto ptr = std::make_unique<char>('!');
    auto future = ftl::yield(std::move(ptr));
    EXPECT_EQ(*future.get(), '!');
  }
  {
    auto future = ftl::yield(123);
    std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')};

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

    EXPECT_EQ(chain.get(), 'b');
  }
}

namespace {

using ByteVector = std::vector<uint8_t>;

ByteVector decrement(ByteVector bytes) {
  std::transform(bytes.begin(), bytes.end(), bytes.begin(), [](auto b) { return b - 1; });
  return bytes;
}

}  // namespace

TEST(Future, Chain) {
  std::packaged_task<const char*()> fetch_string([] { return "ifmmp-"; });

  std::packaged_task<ByteVector(std::string)> append_string([](std::string str) {
    str += "!xpsme";
    return ByteVector{str.begin(), str.end()};
  });

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

  auto fetch = fetch_string.get_future();
  std::thread fetch_thread(std::move(fetch_string));

  std::thread append_thread, decrement_thread;

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

  fetch_thread.join();
  append_thread.join();
  decrement_thread.join();
}

}  // namespace android::test
+4 −5
Original line number Diff line number Diff line
@@ -26,18 +26,17 @@

#include "HWC2.h"

#include <android/configuration.h>
#include <ftl/future.h>
#include <ui/Fence.h>
#include <ui/FloatRect.h>
#include <ui/GraphicBuffer.h>

#include <android/configuration.h>

#include <inttypes.h>
#include <algorithm>
#include <cinttypes>
#include <iterator>
#include <set>

#include "../Promise.h"
#include "ComposerHal.h"

namespace android {
@@ -647,7 +646,7 @@ Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests
}

std::future<Error> Display::setDisplayBrightness(float brightness) {
    return promise::defer([composer = &mComposer, id = mId, brightness] {
    return ftl::defer([composer = &mComposer, id = mId, brightness] {
        const auto intError = composer->setDisplayBrightness(id, brightness);
        return static_cast<Error>(intError);
    });
+3 −3
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <compositionengine/Output.h>
#include <compositionengine/OutputLayer.h>
#include <compositionengine/impl/OutputLayerCompositionState.h>
#include <ftl/future.h>
#include <log/log.h>
#include <ui/DebugUtils.h>
#include <ui/GraphicBuffer.h>
@@ -37,7 +38,6 @@
#include <utils/Trace.h>

#include "../Layer.h" // needed only for debugging
#include "../Promise.h"
#include "../SurfaceFlinger.h"
#include "../SurfaceFlingerProperties.h"
#include "ComposerHal.h"
@@ -792,10 +792,10 @@ status_t HWComposer::getDisplayedContentSample(HalDisplayId displayId, uint64_t

std::future<status_t> HWComposer::setDisplayBrightness(PhysicalDisplayId displayId,
                                                       float brightness) {
    RETURN_IF_INVALID_DISPLAY(displayId, promise::yield<status_t>(BAD_INDEX));
    RETURN_IF_INVALID_DISPLAY(displayId, ftl::yield<status_t>(BAD_INDEX));
    auto& display = mDisplayData[displayId].hwcDisplay;

    return promise::chain(display->setDisplayBrightness(brightness))
    return ftl::chain(display->setDisplayBrightness(brightness))
            .then([displayId](hal::Error error) -> status_t {
                if (error == hal::Error::UNSUPPORTED) {
                    RETURN_IF_HWC_ERROR(error, displayId, INVALID_OPERATION);
Loading