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 <condition_variable>
21 #include <mutex>
22 #include <string>
23 #include <thread>
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  public:
39   HistogramCollector();
40   ~HistogramCollector();
41 
42   void start();
43   void start(uint64_t max_frames);
44   void stop();
45 
46   void notify_histogram_event(int blob_source_fd, BlobId id);
47 
48   std::string Dump() const;
49 
50   HWC2::Error collect(uint64_t max_frames, uint64_t timestamp,
51                       int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
52                       uint64_t *samples[NUM_HISTOGRAM_COLOR_COMPONENTS], uint64_t *numFrames) const;
53   HWC2::Error getAttributes(int32_t *format, int32_t *dataspace,
54                             uint8_t *supported_components) const;
55 
56  private:
57   HistogramCollector(HistogramCollector const &) = delete;
58   HistogramCollector &operator=(HistogramCollector const &) = delete;
59   void blob_processing_thread();
60 
61   std::condition_variable cv;
62   std::mutex mutable mutex;
63   bool started /* GUARDED_BY(mutex) */ = false;
64 
65   struct BlobWork {
66     int fd; /* non-owning! */
67     BlobId id;
68   } blobwork /* GUARDED_BY(mutex) */;
69   // no optional in c++14.
70   bool work_available = false; /* GUARDED_BY(mutex) */
71   ;
72 
73   std::thread monitoring_thread;
74 
75   std::unique_ptr<histogram::Ringbuffer> histogram;
76 };
77 
78 }  // namespace histogram
79 
80 #endif  // HISTOGRAM_HISTOGRAM_COLLECTOR_H_
81