blob: 3122ca8befd47d685295d72a78b25f9d5943caea [file] [log] [blame]
Steve Kondik6f9ab852017-07-09 21:30:20 -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
maxwen95dceef2019-12-07 12:00:17 +010018#define LOG_TAG "VNCFlinger:VirtualDisplay"
Steve Kondik6f9ab852017-07-09 21:30:20 -070019#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>
maxwenad0a9222020-09-20 14:40:41 +020025#include <input/DisplayViewport.h>
Steve Kondik6f9ab852017-07-09 21:30:20 -070026#include "VirtualDisplay.h"
27
28using namespace vncflinger;
29
maxwen80ee9662021-10-05 21:08:06 +020030VirtualDisplay::VirtualDisplay(ui::DisplayMode* mode, ui::DisplayState* state,
maxwenad0a9222020-09-20 14:40:41 +020031 uint32_t width, uint32_t height,
Steve Kondik6f9ab852017-07-09 21:30:20 -070032 sp<CpuConsumer::FrameAvailableListener> listener) {
33 mWidth = width;
34 mHeight = height;
35
maxwenad0a9222020-09-20 14:40:41 +020036 if (state->orientation == ui::ROTATION_0 || state->orientation == ui::ROTATION_180) {
maxwen80ee9662021-10-05 21:08:06 +020037 mSourceRect = Rect(mode->resolution.width, mode->resolution.height);
Steve Kondik6f9ab852017-07-09 21:30:20 -070038 } else {
maxwen80ee9662021-10-05 21:08:06 +020039 mSourceRect = Rect(mode->resolution.height, mode->resolution.width);
Steve Kondik6f9ab852017-07-09 21:30:20 -070040 }
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
maxwen95dceef2019-12-07 12:00:17 +010054 mDisplayToken = SurfaceComposerClient::createDisplay(String8("VNC-VirtualDisplay"), false /*secure*/);
Steve Kondik6f9ab852017-07-09 21:30:20 -070055
maxwen9dcd4d12019-12-07 01:23:56 +010056 SurfaceComposerClient::Transaction t;
maxwen95dceef2019-12-07 12:00:17 +010057 t.setDisplaySurface(mDisplayToken, mProducer);
maxwenad0a9222020-09-20 14:40:41 +020058 t.setDisplayProjection(mDisplayToken, state->orientation, mSourceRect, displayRect);
maxwen95dceef2019-12-07 12:00:17 +010059 t.setDisplayLayerStack(mDisplayToken, 0); // default stack
maxwen9dcd4d12019-12-07 01:23:56 +010060 t.apply();
Steve Kondik6f9ab852017-07-09 21:30:20 -070061
62 ALOGV("Virtual display (%ux%u [viewport=%ux%u] created", width, height, displayRect.getWidth(),
63 displayRect.getHeight());
64}
65
66VirtualDisplay::~VirtualDisplay() {
67 mCpuConsumer.clear();
68 mProducer.clear();
maxwen95dceef2019-12-07 12:00:17 +010069 SurfaceComposerClient::destroyDisplay(mDisplayToken);
Steve Kondik6f9ab852017-07-09 21:30:20 -070070
71 ALOGV("Virtual display destroyed");
72}
73
74Rect 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}