• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 "host/libs/wayland/wayland_surfaces.h"
18 
19 #include <android-base/logging.h>
20 #include <wayland-server-protocol.h>
21 
22 #include "host/libs/wayland/wayland_surface.h"
23 
24 namespace wayland {
25 
GetOrCreateSurface(std::uint32_t id)26 Surface* Surfaces::GetOrCreateSurface(std::uint32_t id) {
27   std::unique_lock<std::mutex> lock(surfaces_mutex_);
28 
29   auto [it, inserted] = surfaces_.try_emplace(id, nullptr);
30 
31   std::unique_ptr<Surface>& surface_ptr = it->second;
32   if (inserted) {
33     surface_ptr.reset(new Surface(id, *this));
34   }
35   return surface_ptr.get();
36 }
37 
OnNextFrame(const FrameCallback & frame_callback)38 void Surfaces::OnNextFrame(const FrameCallback& frame_callback) {
39   // Wraps the given callback in a std::package_task that can be waited upon
40   // for completion.
41   Surfaces::FrameCallbackPackaged frame_callback_packaged(
42       [&frame_callback](std::uint32_t display_number,
43                         std::uint8_t* frame_pixels) {
44         frame_callback(display_number, frame_pixels);
45       });
46 
47   {
48     std::unique_lock<std::mutex> lock(callback_mutex_);
49     callback_.emplace(&frame_callback_packaged);
50   }
51 
52   // Blocks until the frame_callback_packaged was called.
53   frame_callback_packaged.get_future().get();
54 }
55 
HandleSurfaceFrame(std::uint32_t display_number,std::uint8_t * frame_bytes)56 void Surfaces::HandleSurfaceFrame(std::uint32_t display_number,
57                                   std::uint8_t* frame_bytes) {
58   std::unique_lock<std::mutex> lock(callback_mutex_);
59   if (callback_) {
60     (*callback_.value())(display_number, frame_bytes);
61     callback_.reset();
62   }
63 }
64 
65 }  // namespace wayland