blob: fe4a34b57b279495f1ff33854d10c84468f87b62 [file] [log] [blame]
Steve Kondik95027ea2017-06-14 17:22:58 -07001//
2// vncflinger - Copyright (C) 2017 Steve Kondik
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17
Steve Kondik55db0532017-06-12 11:27:18 -070018#ifndef VNCFLINGER_H
19#define VNCFLINGER_H
20
Steve Kondik77754522017-06-14 17:00:33 -070021#include <gui/CpuConsumer.h>
Steve Kondik55db0532017-06-12 11:27:18 -070022#include <ui/DisplayInfo.h>
23
Steve Kondik5f7b0a82017-06-16 09:04:43 -070024#include <rfb/rfb.h>
Steve Kondik55db0532017-06-12 11:27:18 -070025
26#define VNC_PORT 5901
Steve Kondik5f7b0a82017-06-16 09:04:43 -070027#define NUM_BUFS 1
Steve Kondik55db0532017-06-12 11:27:18 -070028
29namespace android {
30
Steve Kondik7225c7f2017-06-14 00:06:16 -070031class VNCFlinger {
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070032 public:
33 VNCFlinger(int argc, char** argv) : mArgc(argc), mArgv(argv), mOrientation(-1) {
Steve Kondik55db0532017-06-12 11:27:18 -070034 }
35
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070036 virtual ~VNCFlinger() {
37 }
Steve Kondik55db0532017-06-12 11:27:18 -070038
39 virtual status_t start();
40 virtual status_t stop();
41
Steve Kondik7225c7f2017-06-14 00:06:16 -070042 virtual size_t addClient();
43 virtual size_t removeClient();
Steve Kondik55db0532017-06-12 11:27:18 -070044
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070045 private:
Steve Kondik77754522017-06-14 17:00:33 -070046 class FrameListener : public CpuConsumer::FrameAvailableListener {
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070047 public:
48 FrameListener(VNCFlinger* vnc) : mVNC(vnc) {
49 }
Steve Kondik55db0532017-06-12 11:27:18 -070050
Steve Kondik77754522017-06-14 17:00:33 -070051 virtual void onFrameAvailable(const BufferItem& item);
52
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070053 private:
54 FrameListener(FrameListener&) {
55 }
56 VNCFlinger* mVNC;
Steve Kondik77754522017-06-14 17:00:33 -070057 };
58
59 virtual void eventLoop();
60
61 virtual status_t createVirtualDisplay();
Steve Kondik6ec5bc82017-06-15 01:31:51 -070062 virtual status_t destroyVirtualDisplayLocked();
Steve Kondik77754522017-06-14 17:00:33 -070063 virtual status_t createVNCServer();
64
65 virtual void processFrame();
66
Steve Kondik6ec5bc82017-06-15 01:31:51 -070067 virtual bool isDeviceRotated(int orientation);
68 virtual bool updateDisplayProjection();
Steve Kondik5f7b0a82017-06-16 09:04:43 -070069 virtual bool updateFBSize(CpuConsumer::LockedBuffer& buf);
Steve Kondik6ec5bc82017-06-15 01:31:51 -070070
Steve Kondik77754522017-06-14 17:00:33 -070071 // vncserver callbacks
Steve Kondik55db0532017-06-12 11:27:18 -070072 static ClientGoneHookPtr onClientGone(rfbClientPtr cl);
73 static enum rfbNewClientAction onNewClient(rfbClientPtr cl);
Steve Kondikef4e8652017-06-14 15:07:54 -070074 static void onFrameStart(rfbClientPtr cl);
75 static void onFrameDone(rfbClientPtr cl, int result);
Steve Kondik2c9d0cf2017-06-15 23:39:29 -070076 static void rfbLogger(const char* format, ...);
77
78 int mArgc;
79 char** mArgv;
Steve Kondik7225c7f2017-06-14 00:06:16 -070080
Steve Kondik77754522017-06-14 17:00:33 -070081 bool mRunning;
82 bool mFrameAvailable;
Steve Kondik6ec5bc82017-06-15 01:31:51 -070083 bool mRotate;
84 bool mVDSActive;
Steve Kondik77754522017-06-14 17:00:33 -070085
86 Mutex mEventMutex;
87 Mutex mUpdateMutex;
88
89 Condition mEventCond;
Steve Kondik7225c7f2017-06-14 00:06:16 -070090
Steve Kondik5f7b0a82017-06-16 09:04:43 -070091 uint32_t mWidth, mHeight;
92 int32_t mOrientation;
Steve Kondik55db0532017-06-12 11:27:18 -070093
94 size_t mClientCount;
Steve Kondik77754522017-06-14 17:00:33 -070095
Steve Kondik5f7b0a82017-06-16 09:04:43 -070096 // Framebuffers
97 uint64_t mFrameNumber;
98 uint64_t mFrameSize;
99 nsecs_t mFrameStartWhen;
Steve Kondik6ec5bc82017-06-15 01:31:51 -0700100
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700101 // Server instance
102 rfbScreenInfoPtr mVNCScreen;
103
104 // Primary display
105 sp<IBinder> mMainDpy;
106
107 // Virtual display
108 sp<IBinder> mDpy;
Steve Kondik77754522017-06-14 17:00:33 -0700109
110 // Producer side of queue, passed into the virtual display.
111 sp<IGraphicBufferProducer> mProducer;
112
113 // This receives frames from the virtual display and makes them available
114 sp<CpuConsumer> mCpuConsumer;
115
Steve Kondik2c9d0cf2017-06-15 23:39:29 -0700116 // Consumer callback
117 sp<FrameListener> mListener;
Steve Kondik55db0532017-06-12 11:27:18 -0700118};
Steve Kondik55db0532017-06-12 11:27:18 -0700119};
120#endif