1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef HISTOGRAM_HISTOGRAM_COLLECTOR_H_
18 #define HISTOGRAM_HISTOGRAM_COLLECTOR_H_
19 #include <android-base/thread_annotations.h>
20 #include <string>
21 #include <thread>
22 #include <mutex>
23 #include <condition_variable>
24 #define HWC2_INCLUDE_STRINGIFICATION
25 #define HWC2_USE_CPP11
26 #include <hardware/hwcomposer2.h>
27 #undef HWC2_INCLUDE_STRINGIFICATION
28 #undef HWC2_USE_CPP11
29 
30 //number of enums in hwc2_format_color_component_t;
31 #define NUM_HISTOGRAM_COLOR_COMPONENTS 4
32 
33 namespace histogram {
34 typedef uint32_t BlobId;
35 
36 class Ringbuffer;
37 class HistogramCollector
38 {
39 public:
40     HistogramCollector();
41     ~HistogramCollector();
42 
43     void start();
44     void start(uint64_t max_frames);
45     void stop();
46 
47     void notify_histogram_event(int blob_source_fd, BlobId id);
48 
49     std::string Dump() const;
50 
51     HWC2::Error collect(uint64_t max_frames,
52                         uint64_t timestamp,
53                         int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
54                         uint64_t* samples[NUM_HISTOGRAM_COLOR_COMPONENTS],
55                         uint64_t* numFrames) const;
56     HWC2::Error getAttributes(int32_t* format,
57                               int32_t* dataspace,
58                               uint8_t* supported_components) const;
59 
60 private:
61     HistogramCollector(HistogramCollector const&) = delete;
62     HistogramCollector& operator=(HistogramCollector const&) = delete;
63     void blob_processing_thread();
64 
65     std::condition_variable cv;
66     std::mutex mutable mutex;
67     bool started /* GUARDED_BY(mutex) */ = false;
68 
69     struct BlobWork {
70         int fd; /* non-owning! */
71         BlobId id;
72     } blobwork /* GUARDED_BY(mutex) */;
73     //no optional in c++14.
74     bool work_available = false; /* GUARDED_BY(mutex) */;
75 
76     std::thread monitoring_thread;
77 
78     std::unique_ptr<histogram::Ringbuffer> histogram;
79 };
80 
81 }  // namespace histogram
82 
83 #endif  // HISTOGRAM_HISTOGRAM_COLLECTOR_H_
84