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

Commit 61977a66 authored by Jerome Gaillard's avatar Jerome Gaillard Committed by Android (Google) Code Review
Browse files

Merge "Make a CommonPool for host builds" into main

parents aaad2758 9b2b762c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -547,6 +547,7 @@ cc_defaults {
        "hwui/MinikinUtils.cpp",
        "hwui/PaintImpl.cpp",
        "hwui/Typeface.cpp",
        "thread/CommonPool.cpp",
        "utils/Blur.cpp",
        "utils/Color.cpp",
        "utils/LinearAllocator.cpp",
@@ -623,7 +624,6 @@ cc_defaults {
                "renderthread/RenderThread.cpp",
                "renderthread/HintSessionWrapper.cpp",
                "service/GraphicsStatsService.cpp",
                "thread/CommonPool.cpp",
                "utils/GLUtils.cpp",
                "utils/NdkUtils.cpp",
                "AutoBackendTextureRelease.cpp",
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 FRAMEWORKS_BASE_COMMONPOOLBASE_H
#define FRAMEWORKS_BASE_COMMONPOOLBASE_H

#include <sys/resource.h>

#include "renderthread/RenderThread.h"

namespace android {
namespace uirenderer {

class CommonPoolBase {
    PREVENT_COPY_AND_ASSIGN(CommonPoolBase);

protected:
    CommonPoolBase() {}

    void setupThread(int i, std::mutex& mLock, std::vector<int>& tids,
                     std::vector<std::condition_variable>& tidConditionVars) {
        std::array<char, 20> name{"hwuiTask"};
        snprintf(name.data(), name.size(), "hwuiTask%d", i);
        auto self = pthread_self();
        pthread_setname_np(self, name.data());
        {
            std::unique_lock lock(mLock);
            tids[i] = pthread_gettid_np(self);
            tidConditionVars[i].notify_one();
        }
        setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
        auto startHook = renderthread::RenderThread::getOnStartHook();
        if (startHook) {
            startHook(name.data());
        }
    }

    bool supportsTid() { return true; }
};

}  // namespace uirenderer
}  // namespace android

#endif  // FRAMEWORKS_BASE_COMMONPOOLBASE_H
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 FRAMEWORKS_BASE_COMMONPOOLBASE_H
#define FRAMEWORKS_BASE_COMMONPOOLBASE_H

#include <condition_variable>
#include <mutex>
#include <vector>

#include "renderthread/RenderThread.h"

namespace android {
namespace uirenderer {

class CommonPoolBase {
    PREVENT_COPY_AND_ASSIGN(CommonPoolBase);

protected:
    CommonPoolBase() {}

    void setupThread(int i, std::mutex& mLock, std::vector<int>& tids,
                     std::vector<std::condition_variable>& tidConditionVars) {
        std::array<char, 20> name{"hwuiTask"};
        snprintf(name.data(), name.size(), "hwuiTask%d", i);
        {
            std::unique_lock lock(mLock);
            tids[i] = -1;
            tidConditionVars[i].notify_one();
        }
        auto startHook = renderthread::RenderThread::getOnStartHook();
        if (startHook) {
            startHook(name.data());
        }
    }

    bool supportsTid() { return false; }
};

}  // namespace uirenderer
}  // namespace android

#endif  // FRAMEWORKS_BASE_COMMONPOOLBASE_H
+6 −21
Original line number Diff line number Diff line
@@ -16,16 +16,14 @@

#include "CommonPool.h"

#include <sys/resource.h>
#include <utils/Trace.h>
#include "renderthread/RenderThread.h"

#include <array>

namespace android {
namespace uirenderer {

CommonPool::CommonPool() {
CommonPool::CommonPool() : CommonPoolBase() {
    ATRACE_CALL();

    CommonPool* pool = this;
@@ -36,22 +34,7 @@ CommonPool::CommonPool() {
    // Create 2 workers
    for (int i = 0; i < THREAD_COUNT; i++) {
        std::thread worker([pool, i, &mLock, &tids, &tidConditionVars] {
            {
                std::array<char, 20> name{"hwuiTask"};
                snprintf(name.data(), name.size(), "hwuiTask%d", i);
                auto self = pthread_self();
                pthread_setname_np(self, name.data());
                {
                    std::unique_lock lock(mLock);
                    tids[i] = pthread_gettid_np(self);
                    tidConditionVars[i].notify_one();
                }
                setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
                auto startHook = renderthread::RenderThread::getOnStartHook();
                if (startHook) {
                    startHook(name.data());
                }
            }
            pool->setupThread(i, mLock, tids, tidConditionVars);
            pool->workerLoop();
        });
        worker.detach();
@@ -64,8 +47,10 @@ CommonPool::CommonPool() {
            }
        }
    }
    if (pool->supportsTid()) {
        mWorkerThreadIds = std::move(tids);
    }
}

CommonPool& CommonPool::instance() {
    static CommonPool pool;
@@ -95,7 +80,7 @@ void CommonPool::enqueue(Task&& task) {

void CommonPool::workerLoop() {
    std::unique_lock lock(mLock);
    while (true) {
    while (!mIsStopping) {
        if (!mWorkQueue.hasWork()) {
            mWaitingThreads++;
            mCondition.wait(lock);
+9 −4
Original line number Diff line number Diff line
@@ -17,8 +17,6 @@
#ifndef FRAMEWORKS_BASE_COMMONPOOL_H
#define FRAMEWORKS_BASE_COMMONPOOL_H

#include "utils/Macros.h"

#include <log/log.h>

#include <condition_variable>
@@ -27,6 +25,9 @@
#include <mutex>
#include <vector>

#include "thread/CommonPoolBase.h"
#include "utils/Macros.h"

namespace android {
namespace uirenderer {

@@ -73,7 +74,7 @@ private:
    int mTail = 0;
};

class CommonPool {
class CommonPool : private CommonPoolBase {
    PREVENT_COPY_AND_ASSIGN(CommonPool);

public:
@@ -107,7 +108,10 @@ private:
    static CommonPool& instance();

    CommonPool();
    ~CommonPool() {}
    ~CommonPool() {
        mIsStopping = true;
        mCondition.notify_all();
    }

    void enqueue(Task&&);
    void doWaitForIdle();
@@ -120,6 +124,7 @@ private:
    std::condition_variable mCondition;
    int mWaitingThreads = 0;
    ArrayQueue<Task, QUEUE_SIZE> mWorkQueue;
    std::atomic_bool mIsStopping = false;
};

}  // namespace uirenderer