blob: 4c9d574ef80ebd1db1c81b42b5133de3e07dbdfe [file] [log] [blame]
Tianyu Jiang83e514e2019-05-15 15:05:30 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "GraphicBufferOverBinder_test"
18
19#include <binder/IServiceManager.h>
20#include <binder/Parcel.h>
21#include <binder/ProcessState.h>
22#include <gtest/gtest.h>
Tianyu Jiang83e514e2019-05-15 15:05:30 -070023#include <ui/GraphicBuffer.h>
24#include <utils/Log.h>
25
26namespace android {
27
28constexpr uint32_t kTestWidth = 1024;
29constexpr uint32_t kTestHeight = 1;
30constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB;
31constexpr uint32_t kTestLayerCount = 1;
32constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN;
33static const String16 kTestServiceName = String16("GraphicBufferOverBinderTestService");
34enum GraphicBufferOverBinderTestServiceCode {
35 GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
Tianyu Jiang83e514e2019-05-15 15:05:30 -070036};
37
38class GraphicBufferOverBinderTestService : public BBinder {
39public:
40 GraphicBufferOverBinderTestService() {
41 // GraphicBuffer
42 mGraphicBuffer = new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount,
43 kTestUsage);
Tianyu Jiang83e514e2019-05-15 15:05:30 -070044 }
45
46 ~GraphicBufferOverBinderTestService() = default;
47
48 virtual status_t onTransact(uint32_t code, const Parcel& /*data*/, Parcel* reply,
49 uint32_t /*flags*/ = 0) {
50 switch (code) {
51 case GRAPHIC_BUFFER: {
52 return reply->write(*mGraphicBuffer);
53 }
Tianyu Jiang83e514e2019-05-15 15:05:30 -070054 default:
55 return UNKNOWN_TRANSACTION;
56 };
57 }
58
59protected:
60 sp<GraphicBuffer> mGraphicBuffer;
Tianyu Jiang83e514e2019-05-15 15:05:30 -070061};
62
63static int runBinderServer() {
64 ProcessState::self()->startThreadPool();
65
66 sp<IServiceManager> sm = defaultServiceManager();
67 sp<GraphicBufferOverBinderTestService> service = new GraphicBufferOverBinderTestService;
68 sm->addService(kTestServiceName, service, false);
69
70 ALOGI("Binder server running...");
71
72 while (true) {
73 int stat, retval;
74 retval = wait(&stat);
75 if (retval == -1 && errno == ECHILD) {
76 break;
77 }
78 }
79
80 ALOGI("Binder server exiting...");
81 return 0;
82}
83
84class GraphicBufferOverBinderTest : public ::testing::TestWithParam<uint32_t> {
85protected:
86 virtual void SetUp() {
87 mService = defaultServiceManager()->getService(kTestServiceName);
88 if (mService == nullptr) {
89 ALOGE("Failed to connect to the test service.");
90 return;
91 }
92
93 ALOGI("Binder service is ready for client.");
94 }
95
96 status_t GetGraphicBuffer(sp<GraphicBuffer>* outBuf, uint32_t opCode) {
97 Parcel data;
98 Parcel reply;
99 status_t error = mService->transact(opCode, data, &reply);
100 if (error != NO_ERROR) {
101 ALOGE("Failed to get graphic buffer over binder, error=%d.", error);
102 return error;
103 }
104
105 *outBuf = new GraphicBuffer();
106 return reply.read(**outBuf);
107 }
108
109private:
110 sp<IBinder> mService;
111};
112
113TEST_F(GraphicBufferOverBinderTest, SendGraphicBufferOverBinder) {
114 sp<GraphicBuffer> gb;
115 EXPECT_EQ(GetGraphicBuffer(&gb, GRAPHIC_BUFFER), OK);
116 EXPECT_NE(gb, nullptr);
Tianyu Jiang83e514e2019-05-15 15:05:30 -0700117 void* vaddr;
118 EXPECT_EQ(gb->lock(kTestUsage, &vaddr), OK);
119 EXPECT_EQ(gb->unlock(), OK);
120}
121
122} // namespace android
123
124int main(int argc, char** argv) {
125 pid_t pid = fork();
126 if (pid == 0) {
127 android::ProcessState::self()->startThreadPool();
128 ::testing::InitGoogleTest(&argc, argv);
129 return RUN_ALL_TESTS();
130
131 } else {
132 ALOGI("Test process pid: %d.", pid);
133 return android::runBinderServer();
134 }
135}