blob: 7cbb9a78a5671d200562bfebe6b313a6acd0bd3c [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 "AndroidPixelBuffer"
19#include <utils/Log.h>
20
21#include <ui/DisplayInfo.h>
22
23#include "AndroidPixelBuffer.h"
24
25using namespace vncflinger;
26using namespace android;
27
28const rfb::PixelFormat AndroidPixelBuffer::sRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16);
29
30AndroidPixelBuffer::AndroidPixelBuffer()
31 : ManagedPixelBuffer(), mRotated(false), mScaleX(1.0f), mScaleY(1.0f) {
32 setPF(sRGBX);
33 setSize(0, 0);
34}
35
36AndroidPixelBuffer::~AndroidPixelBuffer() {
37 mListener = nullptr;
38}
39
40bool AndroidPixelBuffer::isDisplayRotated(uint8_t orientation) {
41 return orientation != DISPLAY_ORIENTATION_0 && orientation != DISPLAY_ORIENTATION_180;
42}
43
44void AndroidPixelBuffer::setBufferRotation(bool rotated) {
45 if (rotated != mRotated) {
46 ALOGV("Orientation changed, swap width/height");
47 mRotated = rotated;
48 setSize(height_, width_);
49 std::swap(mScaleX, mScaleY);
50 stride = width_;
51
52 if (mListener != nullptr) {
53 mListener->onBufferDimensionsChanged(width_, height_);
54 }
55 }
56}
57
58void AndroidPixelBuffer::updateBufferSize(bool fromDisplay) {
59 uint32_t w = 0, h = 0;
60
61 // if this was caused by the source size changing (doesn't really
62 // happen on most Android hardware), then we need to consider
63 // a previous window size set by the client
64 if (fromDisplay) {
65 w = (uint32_t)((float)mSourceWidth * mScaleX);
66 h = (uint32_t)((float)mSourceHeight * mScaleY);
67 mClientWidth = w;
68 mClientHeight = h;
69 } else {
70 w = mClientWidth;
71 h = mClientHeight;
72 }
73
74 mScaleX = (float)mClientWidth / (float)mSourceWidth;
75 mScaleY = (float)mClientHeight / (float)mSourceHeight;
76
77 if (w == (uint32_t)width_ && h == (uint32_t)height_) {
78 return;
79 }
80
81 ALOGV("Buffer dimensions changed: old=(%dx%d) new=(%dx%d) scaleX=%f scaleY=%f", width_, height_,
82 w, h, mScaleX, mScaleY);
83
84 setSize(w, h);
85
86 if (mListener != nullptr) {
87 mListener->onBufferDimensionsChanged(width_, height_);
88 }
89}
90
91void AndroidPixelBuffer::setWindowSize(uint32_t width, uint32_t height) {
92 if (mClientWidth != width || mClientHeight != height) {
93 ALOGV("Client window size changed: old=(%dx%d) new=(%dx%d)", mClientWidth, mClientHeight,
94 width, height);
95 mClientWidth = width;
96 mClientHeight = height;
97 updateBufferSize();
98 }
99}
100
101void AndroidPixelBuffer::setDisplayInfo(DisplayInfo* info) {
102 bool rotated = isDisplayRotated(info->orientation);
103 setBufferRotation(rotated);
104
105 uint32_t w = rotated ? info->h : info->w;
106 uint32_t h = rotated ? info->w : info->h;
107
108 if (w != mSourceWidth || h != mSourceHeight) {
109 ALOGV("Display dimensions changed: old=(%dx%d) new=(%dx%d)", mSourceWidth, mSourceHeight, w,
110 h);
111 mSourceWidth = w;
112 mSourceHeight = h;
113 updateBufferSize(true);
114 }
115}
116
117Rect AndroidPixelBuffer::getSourceRect() {
118 return Rect(mSourceWidth, mSourceHeight);
119}