blob: 7692dc463bfd09e122bea3b23884ab583ab61fc1 [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
18#define LOG_TAG "VirtualDisplay"
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
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
53 mDpy = SurfaceComposerClient::createDisplay(String8("VNC-VirtualDisplay"), false /*secure*/);
54
maxwen9dcd4d12019-12-07 01:23:56 +010055 const auto displayToken = SurfaceComposerClient::getInternalDisplayToken();
56 if (displayToken == nullptr) {
57 ALOGE("Failed to get display token\n");
58 return;
59 }
Steve Kondik6f9ab852017-07-09 21:30:20 -070060
maxwen9dcd4d12019-12-07 01:23:56 +010061 SurfaceComposerClient::Transaction t;
62 t.setDisplaySurface(displayToken, mProducer);
63 t.setDisplayProjection(displayToken, 0, mSourceRect, displayRect);
64 t.setDisplayLayerStack(displayToken, 0); // default stack
65 t.apply();
Steve Kondik6f9ab852017-07-09 21:30:20 -070066
67 ALOGV("Virtual display (%ux%u [viewport=%ux%u] created", width, height, displayRect.getWidth(),
68 displayRect.getHeight());
69}
70
71VirtualDisplay::~VirtualDisplay() {
72 mCpuConsumer.clear();
73 mProducer.clear();
74 SurfaceComposerClient::destroyDisplay(mDpy);
75
76 ALOGV("Virtual display destroyed");
77}
78
79Rect VirtualDisplay::getDisplayRect() {
80 uint32_t outWidth, outHeight;
81 if (mWidth > (uint32_t)((float)mWidth * aspectRatio())) {
82 // limited by narrow width; reduce height
83 outWidth = mWidth;
84 outHeight = (uint32_t)((float)mWidth * aspectRatio());
85 } else {
86 // limited by short height; restrict width
87 outHeight = mHeight;
88 outWidth = (uint32_t)((float)mHeight / aspectRatio());
89 }
90
91 // position the desktop in the viewport while preserving
92 // the source aspect ratio. we do this in case the client
93 // has resized the window and to deal with orientation
94 // changes set up by updateDisplayProjection
95 uint32_t offX, offY;
96 offX = (mWidth - outWidth) / 2;
97 offY = (mHeight - outHeight) / 2;
98 return Rect(offX, offY, offX + outWidth, offY + outHeight);
99}