blob: 1854a55655237205457de80454e3264ff5548454 [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>
25
26#include "VirtualDisplay.h"
27
28using namespace vncflinger;
29
30VirtualDisplay::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
maxwen95dceef2019-12-07 12:00:17 +010053 mDisplayToken = SurfaceComposerClient::createDisplay(String8("VNC-VirtualDisplay"), false /*secure*/);
Steve Kondik6f9ab852017-07-09 21:30:20 -070054
maxwen9dcd4d12019-12-07 01:23:56 +010055 SurfaceComposerClient::Transaction t;
maxwen95dceef2019-12-07 12:00:17 +010056 t.setDisplaySurface(mDisplayToken, mProducer);
57 t.setDisplayProjection(mDisplayToken, 0, mSourceRect, displayRect);
58 t.setDisplayLayerStack(mDisplayToken, 0); // default stack
maxwen9dcd4d12019-12-07 01:23:56 +010059 t.apply();
Steve Kondik6f9ab852017-07-09 21:30:20 -070060
61 ALOGV("Virtual display (%ux%u [viewport=%ux%u] created", width, height, displayRect.getWidth(),
62 displayRect.getHeight());
63}
64
65VirtualDisplay::~VirtualDisplay() {
66 mCpuConsumer.clear();
67 mProducer.clear();
maxwen95dceef2019-12-07 12:00:17 +010068 SurfaceComposerClient::destroyDisplay(mDisplayToken);
Steve Kondik6f9ab852017-07-09 21:30:20 -070069
70 ALOGV("Virtual display destroyed");
71}
72
73Rect 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}