blob: 8000f8feaf23880e04cf82e119e29c488dd862a1 [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
55 SurfaceComposerClient::openGlobalTransaction();
56 SurfaceComposerClient::setDisplaySurface(mDpy, mProducer);
57
58 SurfaceComposerClient::setDisplayProjection(mDpy, 0, mSourceRect, displayRect);
59 SurfaceComposerClient::setDisplayLayerStack(mDpy, 0); // default stack
60 SurfaceComposerClient::closeGlobalTransaction();
61
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();
69 SurfaceComposerClient::destroyDisplay(mDpy);
70
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}