1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #define LOG_TAG "android_emulator_multidisplay_JNI"
19 #include <gui/BufferQueue.h>
20 #include <gui/BufferItemConsumer.h>
21 #include <gui/Surface.h>
22 #include <gui/ISurfaceComposer.h>
23 #include <gui/SurfaceComposerClient.h>
24 
25 #include <sys/epoll.h>
26 
27 #include <gralloc_cb_bp.h>
28 #include <qemu_pipe_bp.h>
29 
30 #include "utils/Log.h"
31 #include "nativehelper/JNIHelp.h"
32 #include <nativehelper/ScopedLocalRef.h>
33 #include "jni.h"
34 #include "android_runtime/AndroidRuntime.h"
35 #include "android_runtime/android_view_Surface.h"
36 
37 #define MAX_DISPLAYS 10
38 
39 using namespace android;
40 
41 static int gFd = 0;
42 static const uint8_t ADD = 1;
43 static const uint8_t DEL = 2;
44 static const uint8_t QUERY = 3;
45 static const uint8_t BIND = 4;
46 static const uint8_t SET_DISPLAY = 0x10;
47 
fillMsg(std::vector<uint8_t> & buf,uint8_t cmd,uint8_t * data,uint32_t size)48 static void fillMsg(std::vector<uint8_t>& buf, uint8_t cmd, uint8_t* data, uint32_t size) {
49     // msg format is size(4B) + cmd(1B) + data(size B)
50     uint32_t totalSize = size + 1;
51     uint8_t* p = (uint8_t*)&totalSize;
52     buf.insert(buf.end(), p, p + 4);
53     buf.push_back(cmd);
54     if (data) {
55         buf.insert(buf.end(), data, data + size);
56     }
57 }
58 
59 struct FrameListener : public ConsumerBase::FrameAvailableListener {
60     sp<BufferItemConsumer> mConsumer;
61     uint32_t mId;
62     uint32_t mCb;
63 public:
onFrameAvailableFrameListener64     void onFrameAvailable(const BufferItem& item) override {
65         BufferItem bufferItem;
66         mConsumer->acquireBuffer(&bufferItem, 0);
67         ANativeWindowBuffer* b = bufferItem.mGraphicBuffer->getNativeBuffer();
68         if (b && b->handle) {
69             const cb_handle_t* cb = cb_handle_t::from(b->handle);
70             if (mCb != cb->hostHandle) {
71                 ALOGI("sent cb %d", cb->hostHandle);
72                 mCb = cb->hostHandle;
73                 uint32_t data[] = {mId, mCb};
74                 std::vector<uint8_t> buf;
75                 fillMsg(buf, BIND, (uint8_t*)data, sizeof(data));
76                 qemu_pipe_write_fully(gFd, buf.data(), buf.size());
77             }
78         }
79         else {
80             ALOGE("cannot get native buffer handler");
81         }
82         mConsumer->releaseBuffer(bufferItem);
83     }
setDefaultBufferSizeFrameListener84     void setDefaultBufferSize(uint32_t w, uint32_t h) {
85         mConsumer->setDefaultBufferSize(w, h);
86     }
FrameListenerFrameListener87     FrameListener(sp<BufferItemConsumer>& consumer, uint32_t id)
88         : mConsumer(consumer), mId(id), mCb(0) { }
89 };
90 
91 sp<FrameListener> gFrameListener[MAX_DISPLAYS + 1];
92 
nativeCreateSurface(JNIEnv * env,jobject obj,jint id,jint width,jint height)93 static jobject nativeCreateSurface(JNIEnv *env, jobject obj, jint id, jint width, jint height)
94 {
95     ALOGI("create surface for %d", id);
96     // Create surface for this new display
97     sp<IGraphicBufferProducer> producer;
98     sp<IGraphicBufferConsumer> consumer;
99     sp<BufferItemConsumer> bufferItemConsumer;
100     BufferQueue::createBufferQueue(&producer, &consumer);
101     bufferItemConsumer = new BufferItemConsumer(consumer, GRALLOC_USAGE_HW_RENDER);
102     gFrameListener[id] = new FrameListener(bufferItemConsumer, id);
103     gFrameListener[id]->setDefaultBufferSize(width, height);
104     bufferItemConsumer->setFrameAvailableListener(gFrameListener[id]);
105     return android_view_Surface_createFromIGraphicBufferProducer(env, producer);
106 }
107 
nativeOpen(JNIEnv * env,jobject obj)108 static jint nativeOpen(JNIEnv* env, jobject obj) {
109     // Open pipe
110     gFd = qemu_pipe_open_ns(NULL, "multidisplay", O_RDWR);
111     if (gFd < 0) {
112         ALOGE("Error opening multidisplay pipe %d", gFd);
113     } else {
114         std::vector<uint8_t> buf;
115         fillMsg(buf, QUERY, nullptr, 0);
116         qemu_pipe_write_fully(gFd, buf.data(), buf.size());
117         ALOGI("multidisplay pipe connected!!!");
118     }
119     return gFd;
120 }
121 
nativeReadPipe(JNIEnv * env,jobject obj,jintArray arr)122 static bool nativeReadPipe(JNIEnv* env, jobject obj, jintArray arr) {
123     int* arrp = env->GetIntArrayElements(arr, 0);
124     uint32_t length;
125     qemu_pipe_read_fully(gFd, &length, sizeof(length));
126     std::vector<uint8_t> args(length, 0);
127     qemu_pipe_read_fully(gFd, args.data(), (size_t)length);
128     switch(args[0]) {
129         case SET_DISPLAY:
130             ALOGV("received setdisplay event");
131             *arrp = SET_DISPLAY;
132             for (int i = 1; i < 6; i++) {
133                 *(arrp + i) = *(uint32_t*)(&args[(i - 1) * 4 + 1]);
134             }
135             env->ReleaseIntArrayElements(arr, arrp, JNI_COMMIT);
136             break;
137         case ADD: {
138             ALOGV("received add event");
139             *arrp = ADD;
140             for (int i = 1; i < 6; i++) {
141                 *(arrp + i) = *(uint32_t*)(&args[(i - 1) * 4 + 1]);
142             }
143             env->ReleaseIntArrayElements(arr, arrp, JNI_COMMIT);
144             break;
145         }
146         case DEL: {
147             ALOGV("received del event");
148             *arrp = DEL;
149             *(arrp + 1) = *(uint32_t*)(&args[1]);
150             env->ReleaseIntArrayElements(arr, arrp, JNI_COMMIT);
151             break;
152         }
153         default: {
154             ALOGE("unexpected event %d", args[0]);
155             return false;
156         }
157     }
158     return true;
159 }
160 
nativeReleaseListener(JNIEnv * env,jobject obj,jint id)161 static bool nativeReleaseListener(JNIEnv* env, jobject obj, jint id) {
162     if (gFrameListener[id].get()) {
163         ALOGV("clear gFrameListener %d", id);
164         gFrameListener[id].clear();
165         gFrameListener[id] = nullptr;
166     }
167     return true;
168 }
169 
nativeResizeListener(JNIEnv * env,jobject obj,jint id,jint w,jint h)170 static bool nativeResizeListener(JNIEnv* env, jobject obj, jint id, jint w, jint h) {
171     if (gFrameListener[id]) {
172         gFrameListener[id]->setDefaultBufferSize(w, h);
173         return true;
174     }
175     return false;
176 }
177 
178 static JNINativeMethod sMethods[] = {
179     { "nativeOpen", "()I", (void*) nativeOpen },
180     { "nativeCreateSurface", "(III)Landroid/view/Surface;", (void*) nativeCreateSurface },
181     { "nativeReadPipe", "([I)Z", (void*) nativeReadPipe},
182     { "nativeReleaseListener", "(I)Z", (void*) nativeReleaseListener},
183     { "nativeResizeListener", "(III)Z", (void*) nativeResizeListener},
184 };
185 
186 /*
187  * JNI Initialization
188  */
JNI_OnLoad(JavaVM * jvm,void * reserved)189 jint JNI_OnLoad(JavaVM *jvm, void *reserved)
190 {
191     JNIEnv *env;
192 
193     // Check JNI version
194     if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6)) {
195         ALOGE("JNI version mismatch error");
196         return JNI_ERR;
197     }
198 
199     jniRegisterNativeMethods(env, "com/android/emulator/multidisplay/MultiDisplayService",
200                                     sMethods, NELEM(sMethods));
201 
202     return JNI_VERSION_1_6;
203 }
204