• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fcntl.h>
18 #include <log/log.h>
19 #include <pthread.h>
20 #include <sys/epoll.h>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <chrono>
26 #include <ctime>
27 #include <fstream>
28 #include <iomanip>
29 #include <memory>
30 #include <sstream>
31 #include <tuple>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include <drm/msm_drm.h>
36 #include <drm/msm_drm_pp.h>
37 #include <xf86drm.h>
38 #include <xf86drmMode.h>
39 
40 #include "histogram_collector.h"
41 #include "ringbuffer.h"
42 
43 constexpr static auto implementation_defined_max_frame_ringbuffer = 300;
44 
HistogramCollector()45 histogram::HistogramCollector::HistogramCollector()
46     : histogram(histogram::Ringbuffer::create(implementation_defined_max_frame_ringbuffer,
47                                               std::make_unique<histogram::DefaultTimeKeeper>())) {}
48 
~HistogramCollector()49 histogram::HistogramCollector::~HistogramCollector() {
50   stop();
51 }
52 
53 namespace {
54 static constexpr size_t numBuckets = 8;
55 static_assert((HIST_V_SIZE % numBuckets) == 0,
56               "histogram cannot be rebucketed to smaller number of buckets");
57 static constexpr int bucket_compression = HIST_V_SIZE / numBuckets;
58 
rebucketTo8Buckets(std::array<uint64_t,HIST_V_SIZE> const & frame)59 std::array<uint64_t, numBuckets> rebucketTo8Buckets(
60     std::array<uint64_t, HIST_V_SIZE> const &frame) {
61   std::array<uint64_t, numBuckets> bins;
62   bins.fill(0);
63   for (auto i = 0u; i < HIST_V_SIZE; i++)
64     bins[i / bucket_compression] += frame[i];
65   return bins;
66 }
67 }  // namespace
68 
Dump() const69 std::string histogram::HistogramCollector::Dump() const {
70   uint64_t num_frames = 0;
71   std::array<uint64_t, HIST_V_SIZE> all_sample_buckets;
72   std::tie(num_frames, all_sample_buckets) = histogram->collect_cumulative();
73   std::array<uint64_t, numBuckets> samples = rebucketTo8Buckets(all_sample_buckets);
74 
75   std::stringstream ss;
76   ss << "Color Sampling, dark (0.0) to light (1.0): sampled frames: " << num_frames << '\n';
77   if (num_frames == 0) {
78     ss << "\tno color statistics collected\n";
79     return ss.str();
80   }
81 
82   ss << std::fixed << std::setprecision(3);
83   ss << "\tbucket\t\t: # of displayed pixels at bucket value\n";
84   for (auto i = 0u; i < samples.size(); i++) {
85     ss << "\t" << i / static_cast<float>(samples.size()) << " to "
86        << (i + 1) / static_cast<float>(samples.size()) << "\t: " << samples[i] << '\n';
87   }
88 
89   return ss.str();
90 }
91 
collect(uint64_t max_frames,uint64_t timestamp,int32_t out_samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],uint64_t * out_samples[NUM_HISTOGRAM_COLOR_COMPONENTS],uint64_t * out_num_frames) const92 HWC2::Error histogram::HistogramCollector::collect(
93     uint64_t max_frames, uint64_t timestamp,
94     int32_t out_samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
95     uint64_t *out_samples[NUM_HISTOGRAM_COLOR_COMPONENTS], uint64_t *out_num_frames) const {
96   if (!out_samples_size || !out_num_frames)
97     return HWC2::Error::BadParameter;
98 
99   out_samples_size[0] = 0;
100   out_samples_size[1] = 0;
101   out_samples_size[2] = numBuckets;
102   out_samples_size[3] = 0;
103 
104   uint64_t num_frames = 0;
105   std::array<uint64_t, HIST_V_SIZE> samples;
106 
107   if (max_frames == 0 && timestamp == 0) {
108     std::tie(num_frames, samples) = histogram->collect_cumulative();
109   } else if (max_frames == 0) {
110     std::tie(num_frames, samples) = histogram->collect_after(timestamp);
111   } else if (timestamp == 0) {
112     std::tie(num_frames, samples) = histogram->collect_max(max_frames);
113   } else {
114     std::tie(num_frames, samples) = histogram->collect_max_after(timestamp, max_frames);
115   }
116 
117   auto samples_rebucketed = rebucketTo8Buckets(samples);
118   *out_num_frames = num_frames;
119   if (out_samples && out_samples[2])
120     memcpy(out_samples[2], samples_rebucketed.data(), sizeof(uint64_t) * samples_rebucketed.size());
121 
122   return HWC2::Error::None;
123 }
124 
getAttributes(int32_t * format,int32_t * dataspace,uint8_t * supported_components) const125 HWC2::Error histogram::HistogramCollector::getAttributes(int32_t *format, int32_t *dataspace,
126                                                          uint8_t *supported_components) const {
127   if (!format || !dataspace || !supported_components)
128     return HWC2::Error::BadParameter;
129 
130   *format = HAL_PIXEL_FORMAT_HSV_888;
131   *dataspace = HAL_DATASPACE_UNKNOWN;
132   *supported_components = HWC2_FORMAT_COMPONENT_2;
133   return HWC2::Error::None;
134 }
135 
start()136 void histogram::HistogramCollector::start() {
137   start(implementation_defined_max_frame_ringbuffer);
138 }
139 
start(uint64_t max_frames)140 void histogram::HistogramCollector::start(uint64_t max_frames) {
141   std::unique_lock<decltype(mutex)> lk(mutex);
142   if (started) {
143     return;
144   }
145 
146   started = true;
147   histogram =
148       histogram::Ringbuffer::create(max_frames, std::make_unique<histogram::DefaultTimeKeeper>());
149   monitoring_thread = std::thread(&HistogramCollector::blob_processing_thread, this);
150 }
151 
stop()152 void histogram::HistogramCollector::stop() {
153   std::unique_lock<decltype(mutex)> lk(mutex);
154   if (!started) {
155     return;
156   }
157 
158   started = false;
159   cv.notify_all();
160   lk.unlock();
161 
162   if (monitoring_thread.joinable())
163     monitoring_thread.join();
164 }
165 
notify_histogram_event(int blob_source_fd,BlobId id)166 void histogram::HistogramCollector::notify_histogram_event(int blob_source_fd, BlobId id) {
167   std::unique_lock<decltype(mutex)> lk(mutex);
168   if (!started) {
169     ALOGW("Discarding event blob-id: %X", id);
170     return;
171   }
172   if (work_available) {
173     ALOGI("notified of histogram event before consuming last one. prior event discarded");
174   }
175 
176   work_available = true;
177   blobwork = HistogramCollector::BlobWork{blob_source_fd, id};
178   cv.notify_all();
179 }
180 
blob_processing_thread()181 void histogram::HistogramCollector::blob_processing_thread() {
182   pthread_setname_np(pthread_self(), "histogram_blob");
183 
184   std::unique_lock<decltype(mutex)> lk(mutex);
185 
186   while (true) {
187     cv.wait(lk, [this] { return !started || work_available; });
188     if (!started) {
189       return;
190     }
191 
192     auto work = blobwork;
193     work_available = false;
194     lk.unlock();
195 
196     drmModePropertyBlobPtr blob = drmModeGetPropertyBlob(work.fd, work.id);
197     if (!blob || !blob->data) {
198       lk.lock();
199       continue;
200     }
201     histogram->insert(*static_cast<struct drm_msm_hist *>(blob->data));
202     drmModeFreePropertyBlob(blob);
203 
204     lk.lock();
205   }
206 }
207