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