summaryrefslogtreecommitdiff
path: root/libs/gui/tests/testserver/TestServer.cpp
blob: 17d1b4abe93480960adc54f7d73b07605346f4b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * 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.
 */

#define LOG_TAG "TestServer"

#include <android-base/stringprintf.h>
#include <binder/IInterface.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/Status.h>
#include <gui/BufferQueue.h>
#include <gui/IConsumerListener.h>
#include <gui/IGraphicBufferConsumer.h>
#include <gui/IGraphicBufferProducer.h>
#include <gui/view/Surface.h>
#include <libgui_test_server/BnTestServer.h>
#include <log/log.h>
#include <utils/Errors.h>

#include <cstdint>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <vector>

#include <fcntl.h>
#include <unistd.h>

#include "TestServer.h"

namespace android {

namespace {
class TestConsumerListener : public IConsumerListener {
    virtual void onFrameAvailable(const BufferItem&) override {}
    virtual void onBuffersReleased() override {}
    virtual void onSidebandStreamChanged() override {}
};

class TestServiceImpl : public libgui_test_server::BnTestServer {
public:
    TestServiceImpl(const char* name) : mName(name) {}

    virtual binder::Status createProducer(view::Surface* out) override {
        std::lock_guard<std::mutex> lock(mMutex);

        BufferQueueHolder bq;
        BufferQueue::createBufferQueue(&bq.producer, &bq.consumer);
        sp<TestConsumerListener> listener = sp<TestConsumerListener>::make();
        bq.consumer->consumerConnect(listener, /*controlledByApp*/ true);

        uint64_t id = 0;
        bq.producer->getUniqueId(&id);
        std::string name = base::StringPrintf("%s-%" PRIu64, mName, id);

        out->name = String16(name.c_str());
        out->graphicBufferProducer = bq.producer;
        mBqs.push_back(std::move(bq));

        return binder::Status::ok();
    }

    virtual binder::Status killNow() override {
        ALOGE("LibGUI Test Service %s dying in response to killNow", mName);
        _exit(0);
        // Not reached:
        return binder::Status::ok();
    }

private:
    std::mutex mMutex;
    const char* mName;

    struct BufferQueueHolder {
        sp<IGraphicBufferProducer> producer;
        sp<IGraphicBufferConsumer> consumer;
    };

    std::vector<BufferQueueHolder> mBqs;
};
} // namespace

int TestServerMain(const char* name) {
    ProcessState::self()->startThreadPool();

    sp<TestServiceImpl> testService = sp<TestServiceImpl>::make(name);
    ALOGE("service");
    sp<IServiceManager> serviceManager(defaultServiceManager());
    LOG_ALWAYS_FATAL_IF(OK != serviceManager->addService(String16(name), testService));

    ALOGD("LibGUI Test Service %s STARTED", name);

    IPCThreadState::self()->joinThreadPool();

    ALOGW("LibGUI Test Service %s DIED", name);

    return 0;
}

} // namespace android