1 /*
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>
23 #include <ui/GraphicBuffer.h>
24 #include <utils/Log.h>
25 
26 namespace android {
27 
28 constexpr uint32_t kTestWidth = 1024;
29 constexpr uint32_t kTestHeight = 1;
30 constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB;
31 constexpr uint32_t kTestLayerCount = 1;
32 constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN;
33 static const String16 kTestServiceName = String16("GraphicBufferOverBinderTestService");
34 enum GraphicBufferOverBinderTestServiceCode {
35     GRAPHIC_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
36 };
37 
38 class GraphicBufferOverBinderTestService : public BBinder {
39 public:
GraphicBufferOverBinderTestService()40     GraphicBufferOverBinderTestService() {
41         // GraphicBuffer
42         mGraphicBuffer = new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount,
43                                            kTestUsage);
44     }
45 
46     ~GraphicBufferOverBinderTestService() = default;
47 
onTransact(uint32_t code,const Parcel &,Parcel * reply,uint32_t=0)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             }
54             default:
55                 return UNKNOWN_TRANSACTION;
56         };
57     }
58 
59 protected:
60     sp<GraphicBuffer> mGraphicBuffer;
61 };
62 
runBinderServer()63 static 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 
84 class GraphicBufferOverBinderTest : public ::testing::TestWithParam<uint32_t> {
85 protected:
SetUp()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 
GetGraphicBuffer(sp<GraphicBuffer> * outBuf,uint32_t opCode)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 
109 private:
110     sp<IBinder> mService;
111 };
112 
TEST_F(GraphicBufferOverBinderTest,SendGraphicBufferOverBinder)113 TEST_F(GraphicBufferOverBinderTest, SendGraphicBufferOverBinder) {
114     sp<GraphicBuffer> gb;
115     EXPECT_EQ(GetGraphicBuffer(&gb, GRAPHIC_BUFFER), OK);
116     EXPECT_NE(gb, nullptr);
117     void* vaddr;
118     EXPECT_EQ(gb->lock(kTestUsage, &vaddr), OK);
119     EXPECT_EQ(gb->unlock(), OK);
120 }
121 
122 } // namespace android
123 
main(int argc,char ** argv)124 int 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 }
136