• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 /*
3  * Copyright (C) 2016 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 <functional>
19 #include <mutex>
20 #include <thread>
21 #include <vector>
22 
23 #include <hardware/gralloc.h>
24 #include <common/libs/fs/shared_fd.h>
25 #include "hwcomposer.h"
26 
27 namespace cvd {
28 
29 class FrameBuffer{
30  public:
31   FrameBuffer();
32   ~FrameBuffer();
33 
34   void Broadcast(int32_t offset);
35   int NextScreenBuffer();
36   void* GetBuffer(int fb_index);
37   size_t buffer_size();
38   int32_t x_res();
39   int32_t y_res();
40   int32_t line_length();
41   int32_t bytes_per_pixel();
42   int32_t dpi();
43   int32_t refresh_rate();
44  private:
45   void BroadcastLoop();
46 
47   std::vector<char> inner_buffer_;
48   int last_frame_buffer_ = 0;
49   cvd::SharedFD screen_server_;
50   std::thread broadcast_thread_;
51   int32_t current_offset_ = 0;
52   int32_t current_seq_ = 0;
53   std::mutex mutex_;
54   std::condition_variable cond_var_;
55   bool running_ = true;
56   int32_t x_res_{720};
57   int32_t y_res_{1280};
58   int32_t dpi_{160};
59   int32_t refresh_rate_{60};
60 };
61 
62 class BaseComposer {
63  public:
64   BaseComposer(int64_t vsync_base_timestamp);
65   ~BaseComposer();
66 
67   // Sets the composition type of each layer and returns the number of layers
68   // to be composited by the hwcomposer.
69   int PrepareLayers(size_t num_layers, vsoc_hwc_layer* layers);
70   // Returns 0 if successful.
71   int SetLayers(size_t num_layers, vsoc_hwc_layer* layers);
72   void Dump(char* buff, int buff_len);
73 
x_res()74   int32_t x_res() {
75     return frame_buffer_.x_res();
76   }
y_res()77   int32_t y_res() {
78     return frame_buffer_.y_res();
79   }
dpi()80   int32_t dpi() {
81     return frame_buffer_.dpi();
82   }
refresh_rate()83   int32_t refresh_rate() {
84     return frame_buffer_.refresh_rate();
85   }
86 
87  protected:
88   const gralloc_module_t* gralloc_module_;
89   int64_t vsync_base_timestamp_;
90   int32_t vsync_period_ns_;
91   FrameBuffer frame_buffer_;
92 
93  private:
94   // Returns buffer offset or negative on error.
95   int PostFrameBufferTarget(buffer_handle_t handle);
96 };
97 
98 }  // namespace cvd
99