• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <ui/DisplayInfo.h>
25 
26 #include <sys/epoll.h>
27 
28 #include <gralloc_cb_bp.h>
29 #include <qemu_pipe_bp.h>
30 
31 #include "utils/Log.h"
32 #include "nativehelper/JNIHelp.h"
33 #include <nativehelper/ScopedLocalRef.h>
34 #include "jni.h"
35 #include "android_runtime/AndroidRuntime.h"
36 #include "android_runtime/android_view_Surface.h"
37 
38 #define MAX_DISPLAYS 10
39 
40 using namespace android;
41 
42 static int gFd = 0;
43 static const uint8_t ADD = 1;
44 static const uint8_t DEL = 2;
45 static const uint8_t QUERY = 3;
46 static const uint8_t BIND = 4;
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 ADD: {
130             ALOGV("received add event");
131             *arrp = ADD;
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         }
138         case DEL: {
139             ALOGV("received del event");
140             *arrp = DEL;
141             *(arrp + 1) = *(uint32_t*)(&args[1]);
142             env->ReleaseIntArrayElements(arr, arrp, JNI_COMMIT);
143             break;
144         }
145         default: {
146             ALOGE("unexpected event %d", args[0]);
147             return false;
148         }
149     }
150     return true;
151 }
152 
nativeReleaseListener(JNIEnv * env,jobject obj,jint id)153 static bool nativeReleaseListener(JNIEnv* env, jobject obj, jint id) {
154     if (gFrameListener[id].get()) {
155         ALOGV("clear gFrameListener %d", id);
156         gFrameListener[id].clear();
157         gFrameListener[id] = nullptr;
158     }
159     return true;
160 }
161 
nativeResizeListener(JNIEnv * env,jobject obj,jint id,jint w,jint h)162 static bool nativeResizeListener(JNIEnv* env, jobject obj, jint id, jint w, jint h) {
163     if (gFrameListener[id]) {
164         gFrameListener[id]->setDefaultBufferSize(w, h);
165         return true;
166     }
167     return false;
168 }
169 
170 static JNINativeMethod sMethods[] = {
171     { "nativeOpen", "()I", (void*) nativeOpen },
172     { "nativeCreateSurface", "(III)Landroid/view/Surface;", (void*) nativeCreateSurface },
173     { "nativeReadPipe", "([I)Z", (void*) nativeReadPipe},
174     { "nativeReleaseListener", "(I)Z", (void*) nativeReleaseListener},
175     { "nativeResizeListener", "(III)Z", (void*) nativeResizeListener},
176 };
177 
178 /*
179  * JNI Initialization
180  */
JNI_OnLoad(JavaVM * jvm,void * reserved)181 jint JNI_OnLoad(JavaVM *jvm, void *reserved)
182 {
183     JNIEnv *env;
184 
185     // Check JNI version
186     if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6)) {
187         ALOGE("JNI version mismatch error");
188         return JNI_ERR;
189     }
190 
191     jniRegisterNativeMethods(env, "com/android/emulator/multidisplay/MultiDisplayService",
192                                     sMethods, NELEM(sMethods));
193 
194     return JNI_VERSION_1_6;
195 }
196