Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 1 | // |
| 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 | |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 18 | #define LOG_TAG "VNCFlinger:VirtualDisplay" |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <gui/BufferQueue.h> |
| 22 | #include <gui/CpuConsumer.h> |
| 23 | #include <gui/IGraphicBufferConsumer.h> |
| 24 | #include <gui/SurfaceComposerClient.h> |
| 25 | |
| 26 | #include "VirtualDisplay.h" |
| 27 | |
| 28 | using namespace vncflinger; |
| 29 | |
| 30 | VirtualDisplay::VirtualDisplay(DisplayInfo* info, uint32_t width, uint32_t height, |
| 31 | sp<CpuConsumer::FrameAvailableListener> listener) { |
| 32 | mWidth = width; |
| 33 | mHeight = height; |
| 34 | |
| 35 | if (info->orientation == DISPLAY_ORIENTATION_0 || info->orientation == DISPLAY_ORIENTATION_180) { |
| 36 | mSourceRect = Rect(info->w, info->h); |
| 37 | } else { |
| 38 | mSourceRect = Rect(info->h, info->w); |
| 39 | } |
| 40 | |
| 41 | Rect displayRect = getDisplayRect(); |
| 42 | |
| 43 | sp<IGraphicBufferConsumer> consumer; |
| 44 | BufferQueue::createBufferQueue(&mProducer, &consumer); |
| 45 | mCpuConsumer = new CpuConsumer(consumer, 1); |
| 46 | mCpuConsumer->setName(String8("vds-to-cpu")); |
| 47 | mCpuConsumer->setDefaultBufferSize(width, height); |
| 48 | mProducer->setMaxDequeuedBufferCount(4); |
| 49 | consumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBX_8888); |
| 50 | |
| 51 | mCpuConsumer->setFrameAvailableListener(listener); |
| 52 | |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 53 | mDisplayToken = SurfaceComposerClient::createDisplay(String8("VNC-VirtualDisplay"), false /*secure*/); |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 54 | |
maxwen | 9dcd4d1 | 2019-12-07 01:23:56 +0100 | [diff] [blame] | 55 | SurfaceComposerClient::Transaction t; |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 56 | t.setDisplaySurface(mDisplayToken, mProducer); |
| 57 | t.setDisplayProjection(mDisplayToken, 0, mSourceRect, displayRect); |
| 58 | t.setDisplayLayerStack(mDisplayToken, 0); // default stack |
maxwen | 9dcd4d1 | 2019-12-07 01:23:56 +0100 | [diff] [blame] | 59 | t.apply(); |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 60 | |
| 61 | ALOGV("Virtual display (%ux%u [viewport=%ux%u] created", width, height, displayRect.getWidth(), |
| 62 | displayRect.getHeight()); |
| 63 | } |
| 64 | |
| 65 | VirtualDisplay::~VirtualDisplay() { |
| 66 | mCpuConsumer.clear(); |
| 67 | mProducer.clear(); |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 68 | SurfaceComposerClient::destroyDisplay(mDisplayToken); |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 69 | |
| 70 | ALOGV("Virtual display destroyed"); |
| 71 | } |
| 72 | |
| 73 | Rect VirtualDisplay::getDisplayRect() { |
| 74 | uint32_t outWidth, outHeight; |
| 75 | if (mWidth > (uint32_t)((float)mWidth * aspectRatio())) { |
| 76 | // limited by narrow width; reduce height |
| 77 | outWidth = mWidth; |
| 78 | outHeight = (uint32_t)((float)mWidth * aspectRatio()); |
| 79 | } else { |
| 80 | // limited by short height; restrict width |
| 81 | outHeight = mHeight; |
| 82 | outWidth = (uint32_t)((float)mHeight / aspectRatio()); |
| 83 | } |
| 84 | |
| 85 | // position the desktop in the viewport while preserving |
| 86 | // the source aspect ratio. we do this in case the client |
| 87 | // has resized the window and to deal with orientation |
| 88 | // changes set up by updateDisplayProjection |
| 89 | uint32_t offX, offY; |
| 90 | offX = (mWidth - outWidth) / 2; |
| 91 | offY = (mHeight - outHeight) / 2; |
| 92 | return Rect(offX, offY, offX + outWidth, offY + outHeight); |
| 93 | } |