Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #ifndef VDS_H |
| 18 | #define VDS_H |
| 19 | |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 20 | #include "Program.h" |
| 21 | #include "EglWindow.h" |
| 22 | |
| 23 | #include <gui/BufferQueue.h> |
| 24 | #include <gui/GLConsumer.h> |
| 25 | #include <gui/IGraphicBufferProducer.h> |
| 26 | #include <ui/DisplayInfo.h> |
| 27 | #include <utils/Thread.h> |
| 28 | |
Steve Kondik | dda1100 | 2017-06-13 08:20:27 -0700 | [diff] [blame] | 29 | #include <rfb/rfb.h> |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 30 | |
| 31 | #define NUM_PBO 2 |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | /* |
| 36 | * Support for "frames" output format. |
| 37 | */ |
| 38 | class VirtualDisplay : public GLConsumer::FrameAvailableListener, Thread { |
| 39 | public: |
Steve Kondik | dda1100 | 2017-06-13 08:20:27 -0700 | [diff] [blame] | 40 | VirtualDisplay(rfbScreenInfoPtr vncScreen) : Thread(false), |
| 41 | mVNCScreen(vncScreen), |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 42 | mThreadResult(UNKNOWN_ERROR), |
| 43 | mState(UNINITIALIZED), |
| 44 | mIndex(0) |
| 45 | {} |
| 46 | |
| 47 | // Create an "input surface", similar in purpose to a MediaCodec input |
| 48 | // surface, that the virtual display can send buffers to. Also configures |
| 49 | // EGL with a pbuffer surface on the current thread. |
Steve Kondik | 7225c7f | 2017-06-14 00:06:16 -0700 | [diff] [blame^] | 50 | virtual status_t start(const DisplayInfo& mainDpyInfo); |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 51 | |
Steve Kondik | dda1100 | 2017-06-13 08:20:27 -0700 | [diff] [blame] | 52 | virtual status_t stop(); |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 53 | |
| 54 | static bool isDeviceRotated(int orientation); |
| 55 | |
| 56 | private: |
| 57 | VirtualDisplay(const VirtualDisplay&); |
| 58 | VirtualDisplay& operator=(const VirtualDisplay&); |
| 59 | |
| 60 | // Destruction via RefBase. |
| 61 | virtual ~VirtualDisplay() { |
| 62 | assert(mState == UNINITIALIZED || mState == STOPPED); |
| 63 | } |
| 64 | |
| 65 | virtual status_t setDisplayProjection(const sp<IBinder>& dpy, |
| 66 | const DisplayInfo& mainDpyInfo); |
| 67 | |
| 68 | // (overrides GLConsumer::FrameAvailableListener method) |
| 69 | virtual void onFrameAvailable(const BufferItem& item); |
| 70 | |
| 71 | // (overrides Thread method) |
| 72 | virtual bool threadLoop(); |
| 73 | |
| 74 | // One-time setup (essentially object construction on the overlay thread). |
| 75 | status_t setup_l(); |
| 76 | |
| 77 | // Release all resources held. |
| 78 | void release_l(); |
| 79 | |
| 80 | // Process a frame received from the virtual display. |
| 81 | void* processFrame_l(); |
| 82 | |
Steve Kondik | dda1100 | 2017-06-13 08:20:27 -0700 | [diff] [blame] | 83 | rfbScreenInfoPtr mVNCScreen; |
| 84 | |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 85 | uint32_t mHeight, mWidth; |
| 86 | bool mRotate; |
| 87 | |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 88 | // Used to wait for the FrameAvailableListener callback. |
| 89 | Mutex mMutex; |
| 90 | |
| 91 | // Initialization gate. |
| 92 | Condition mStartCond; |
| 93 | |
| 94 | // Thread status, mostly useful during startup. |
| 95 | status_t mThreadResult; |
Steve Kondik | 7225c7f | 2017-06-14 00:06:16 -0700 | [diff] [blame^] | 96 | |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 97 | // Overlay thread state. States advance from left to right; object may |
| 98 | // not be restarted. |
| 99 | enum { UNINITIALIZED, INIT, RUNNING, STOPPING, STOPPED } mState; |
| 100 | |
| 101 | // Event notification. Overlay thread sleeps on this until a frame |
| 102 | // arrives or it's time to shut down. |
| 103 | Condition mEventCond; |
| 104 | |
| 105 | // Producer side of queue, passed into the virtual display. |
| 106 | // The consumer end feeds into our GLConsumer. |
| 107 | sp<IGraphicBufferProducer> mProducer; |
| 108 | |
| 109 | // This receives frames from the virtual display and makes them available |
| 110 | // as an external texture. |
| 111 | sp<GLConsumer> mGlConsumer; |
| 112 | |
| 113 | // EGL display / context / surface. |
| 114 | EglWindow mEglWindow; |
| 115 | |
| 116 | // GL rendering support. |
| 117 | Program mExtTexProgram; |
| 118 | |
| 119 | // External texture, updated by GLConsumer. |
| 120 | GLuint mExtTextureName; |
| 121 | |
| 122 | // Pixel data buffers. |
| 123 | size_t mBufSize; |
Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame] | 124 | GLuint* mPBO; |
| 125 | unsigned int mIndex; |
| 126 | |
| 127 | sp<IBinder> mDpy; |
| 128 | }; |
| 129 | |
| 130 | }; // namespace android |
| 131 | |
| 132 | #endif /* VDS_H */ |