Home | History | Annotate | Download | only in lib
      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, int alignment = kAlignment) {
     36     return (input + alignment - 1) & -alignment;
     37   }
     38 
     39   // Screen width in pixels
     40   int x_res() const { return data().x_res; }
     41 
     42   // Screen height in pixels
     43   int y_res() const { return data().y_res; }
     44 
     45   // Dots per inch
     46   int dpi() const { return data().dpi; }
     47 
     48   // Refresh rate in Hertz
     49   int refresh_rate_hz() const { return data().refresh_rate_hz; }
     50 
     51   uint32_t pixel_format() const { return kFbPixelFormat; }
     52 
     53   uint32_t bytes_per_pixel() const {
     54     return vsoc::PixelFormatProperties<kFbPixelFormat>::bytes_per_pixel;
     55   }
     56 
     57   int line_length() const { return align(x_res() * bytes_per_pixel()); }
     58 
     59   size_t buffer_size() const {
     60     return (align(x_res() * bytes_per_pixel()) * y_res()) + kSwiftShaderPadding;
     61   }
     62 
     63   int number_of_buffers() const;
     64 
     65   // Gets a pointer to the beginning of a buffer. Does not perform any bound
     66   // checks on the index.
     67   void* GetBuffer(int buffer_idx);
     68 
     69   // Broadcasts a new frame.
     70   // buffer_idx is the index of the buffer containing the composed screen, it's
     71   // a number in the range [0, number_of_buffers() - 1].
     72   // Stats holds performance information of the last composition, can be null.
     73   void BroadcastNewFrame(
     74       int buffer_idx,
     75       const vsoc::layout::screen::CompositionStats* stats = nullptr);
     76 
     77   // Waits for a new frame (one with a different seq_num than last one we saw).
     78   // Returns the index of the buffer containing the new frame or a negative
     79   // number if there was an error, stores the new sequential number in
     80   // *last_seq_num. The frame sequential numbers will be provided by the
     81   // hwcomposer and expected to increase monotonically over time (though it's
     82   // not a hard requirement), this numbers are guaranteed to be non-zero when a
     83   // valid frame is available. Performance statistics are returned through the
     84   // stats parameter when it's not null.
     85   int WaitForNewFrameSince(
     86       uint32_t* last_seq_num,
     87       vsoc::layout::screen::CompositionStats* stats = nullptr);
     88 
     89   using Pixel = uint32_t;
     90   static constexpr int kSwiftShaderPadding = 4;
     91   static constexpr int kRedShift = 0;
     92   static constexpr int kGreenShift = 8;
     93   static constexpr int kBlueShift = 16;
     94   static constexpr int kRedBits = 8;
     95   static constexpr int kGreenBits = 8;
     96   static constexpr int kBlueBits = 8;
     97   static constexpr uint32_t kFbPixelFormat = vsoc::VSOC_PIXEL_FORMAT_RGBA_8888;
     98   static constexpr int kAlignment = 8;
     99 
    100  protected:
    101   const uint8_t* first_buffer() const;
    102 };
    103 }  // namespace screen
    104 }  // namespace vsoc
    105