Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 1 | // |
| 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 | |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 18 | #define LOG_TAG "VNCFlinger:AndroidPixelBuffer" |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 21 | #include "AndroidPixelBuffer.h" |
| 22 | |
| 23 | using namespace vncflinger; |
| 24 | using namespace android; |
| 25 | |
| 26 | const rfb::PixelFormat AndroidPixelBuffer::sRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 27 | |
| 28 | AndroidPixelBuffer::AndroidPixelBuffer() |
| 29 | : ManagedPixelBuffer(), mRotated(false), mScaleX(1.0f), mScaleY(1.0f) { |
| 30 | setPF(sRGBX); |
| 31 | setSize(0, 0); |
| 32 | } |
| 33 | |
| 34 | AndroidPixelBuffer::~AndroidPixelBuffer() { |
| 35 | mListener = nullptr; |
| 36 | } |
| 37 | |
maxwen | ad0a922 | 2020-09-20 14:40:41 +0200 | [diff] [blame] | 38 | bool AndroidPixelBuffer::isDisplayRotated(ui::Rotation orientation) { |
| 39 | return orientation != ui::ROTATION_0 && orientation != ui::ROTATION_180; |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void 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 | |
| 56 | void 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 | |
| 89 | void 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 | |
maxwen | 80ee966 | 2021-10-05 21:08:06 +0200 | [diff] [blame] | 99 | void AndroidPixelBuffer::setDisplayInfo(ui::DisplayMode* mode, ui::DisplayState* state) { |
maxwen | ad0a922 | 2020-09-20 14:40:41 +0200 | [diff] [blame] | 100 | bool rotated = isDisplayRotated(state->orientation); |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 101 | setBufferRotation(rotated); |
| 102 | |
maxwen | 80ee966 | 2021-10-05 21:08:06 +0200 | [diff] [blame] | 103 | uint32_t w = rotated ? mode->resolution.height : mode->resolution.width; |
| 104 | uint32_t h = rotated ? mode->resolution.width : mode->resolution.height; |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 105 | |
| 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 | |
| 115 | Rect AndroidPixelBuffer::getSourceRect() { |
| 116 | return Rect(mSourceWidth, mSourceHeight); |
| 117 | } |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 118 | |
| 119 | void AndroidPixelBuffer::reset() { |
| 120 | mSourceWidth = 0; |
| 121 | mSourceHeight = 0; |
| 122 | } |