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 | |
| 21 | #include <ui/DisplayInfo.h> |
| 22 | |
| 23 | #include "AndroidPixelBuffer.h" |
| 24 | |
| 25 | using namespace vncflinger; |
| 26 | using namespace android; |
| 27 | |
| 28 | const rfb::PixelFormat AndroidPixelBuffer::sRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16); |
| 29 | |
| 30 | AndroidPixelBuffer::AndroidPixelBuffer() |
| 31 | : ManagedPixelBuffer(), mRotated(false), mScaleX(1.0f), mScaleY(1.0f) { |
| 32 | setPF(sRGBX); |
| 33 | setSize(0, 0); |
| 34 | } |
| 35 | |
| 36 | AndroidPixelBuffer::~AndroidPixelBuffer() { |
| 37 | mListener = nullptr; |
| 38 | } |
| 39 | |
maxwen | ad0a922 | 2020-09-20 14:40:41 +0200 | [diff] [blame^] | 40 | bool AndroidPixelBuffer::isDisplayRotated(ui::Rotation orientation) { |
| 41 | return orientation != ui::ROTATION_0 && orientation != ui::ROTATION_180; |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void 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 | |
| 58 | void 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 | |
| 91 | void 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 | |
maxwen | ad0a922 | 2020-09-20 14:40:41 +0200 | [diff] [blame^] | 101 | void AndroidPixelBuffer::setDisplayInfo(DisplayConfig* config, ui::DisplayState* state) { |
| 102 | bool rotated = isDisplayRotated(state->orientation); |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 103 | setBufferRotation(rotated); |
| 104 | |
maxwen | ad0a922 | 2020-09-20 14:40:41 +0200 | [diff] [blame^] | 105 | uint32_t w = rotated ? config->resolution.height : config->resolution.width; |
| 106 | uint32_t h = rotated ? config->resolution.width : config->resolution.height; |
Steve Kondik | 6f9ab85 | 2017-07-09 21:30:20 -0700 | [diff] [blame] | 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 | |
| 117 | Rect AndroidPixelBuffer::getSourceRect() { |
| 118 | return Rect(mSourceWidth, mSourceHeight); |
| 119 | } |
maxwen | 95dceef | 2019-12-07 12:00:17 +0100 | [diff] [blame] | 120 | |
| 121 | void AndroidPixelBuffer::reset() { |
| 122 | mSourceWidth = 0; |
| 123 | mSourceHeight = 0; |
| 124 | } |