Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 1 | #pragma once |
| 2 | /* |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <memory> |
| 19 | |
| 20 | #include "common/vsoc/lib/typed_region_view.h" |
| 21 | #include "common/vsoc/shm/graphics.h" |
| 22 | #include "common/vsoc/shm/screen_layout.h" |
| 23 | #include "uapi/vsoc_shm.h" |
| 24 | |
| 25 | namespace vsoc { |
| 26 | namespace screen { |
| 27 | |
| 28 | // Provides information related to the device's screen. Allows to query screen |
| 29 | // properties such as resolution and dpi, as well as subscribe/notify to/of |
| 30 | // changes on the screen contents. It also holds the contents of the display. |
| 31 | class ScreenRegionView |
| 32 | : public vsoc::TypedRegionView<ScreenRegionView, |
| 33 | vsoc::layout::screen::ScreenLayout> { |
| 34 | public: |
| 35 | static int align(int input) { |
| 36 | auto constexpr alignment = 16; |
| 37 | return (input + alignment - 1) & -alignment; |
| 38 | } |
| 39 | |
| 40 | // Screen width in pixels |
| 41 | int x_res() const { return data().x_res; } |
| 42 | |
| 43 | // Screen height in pixels |
| 44 | int y_res() const { return data().y_res; } |
| 45 | |
| 46 | // Dots per inch |
| 47 | int dpi() const { return data().dpi; } |
| 48 | |
| 49 | // Refresh rate in Hertz |
| 50 | int refresh_rate_hz() const { return data().refresh_rate_hz; } |
| 51 | |
| 52 | uint32_t pixel_format() const { return kFbPixelFormat; } |
| 53 | |
| 54 | uint32_t bytes_per_pixel() const { |
| 55 | return vsoc::PixelFormatProperties<kFbPixelFormat>::bytes_per_pixel; |
| 56 | } |
| 57 | |
| 58 | int line_length() const { return align(x_res() * bytes_per_pixel()); } |
| 59 | |
| 60 | size_t buffer_size() const { |
| 61 | return (align(x_res() * bytes_per_pixel()) * y_res()) + kSwiftShaderPadding; |
| 62 | } |
| 63 | |
| 64 | int number_of_buffers() const; |
| 65 | |
| 66 | // Gets a pointer to the beginning of a buffer. Does not perform any bound |
| 67 | // checks on the index. |
| 68 | void* GetBuffer(int buffer_idx); |
| 69 | |
| 70 | // Broadcasts a new frame. |
| 71 | // buffer_idx is the index of the buffer containing the composed screen, it's |
| 72 | // a number in the range [0, number_of_buffers() - 1]. |
| 73 | // Stats holds performance information of the last composition, can be null. |
| 74 | void BroadcastNewFrame( |
| 75 | int buffer_idx, |
| 76 | const vsoc::layout::screen::CompositionStats* stats = nullptr); |
| 77 | |
| 78 | // Waits for a new frame (one with a different seq_num than last one we saw). |
| 79 | // Returns the index of the buffer containing the new frame or a negative |
| 80 | // number if there was an error, stores the new sequential number in |
| 81 | // *last_seq_num. The frame sequential numbers will be provided by the |
| 82 | // hwcomposer and expected to increase monotonically over time (though it's |
| 83 | // not a hard requirement), this numbers are guaranteed to be non-zero when a |
| 84 | // valid frame is available. Performance statistics are returned through the |
| 85 | // stats parameter when it's not null. |
| 86 | int WaitForNewFrameSince( |
| 87 | uint32_t* last_seq_num, |
| 88 | vsoc::layout::screen::CompositionStats* stats = nullptr); |
| 89 | |
| 90 | using Pixel = uint32_t; |
| 91 | static constexpr int kSwiftShaderPadding = 4; |
| 92 | static constexpr int kRedShift = 0; |
| 93 | static constexpr int kGreenShift = 8; |
| 94 | static constexpr int kBlueShift = 16; |
| 95 | static constexpr int kRedBits = 8; |
| 96 | static constexpr int kGreenBits = 8; |
| 97 | static constexpr int kBlueBits = 8; |
| 98 | static constexpr uint32_t kFbPixelFormat = vsoc::VSOC_PIXEL_FORMAT_RGBA_8888; |
| 99 | |
| 100 | protected: |
| 101 | const uint8_t* first_buffer() const; |
| 102 | }; |
| 103 | } // namespace screen |
| 104 | } // namespace vsoc |