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