1 /*
2  * Copyright (C) 2016 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 "guest/hals/hwcomposer/common/base_composer.h"
18 
19 #include <string.h>
20 
21 #include <cutils/properties.h>
22 #include <log/log.h>
23 
24 namespace cuttlefish {
25 
BaseComposer(std::unique_ptr<ScreenView> screen_view)26 BaseComposer::BaseComposer(std::unique_ptr<ScreenView> screen_view)
27     : screen_view_(std::move(screen_view)), gralloc_() {}
28 
Dump(char * buff __unused,int buff_len __unused)29 void BaseComposer::Dump(char* buff __unused, int buff_len __unused) {}
30 
PostFrameBufferTarget(buffer_handle_t buffer_handle)31 int BaseComposer::PostFrameBufferTarget(buffer_handle_t buffer_handle) {
32   auto buffer_id = screen_view_->NextBuffer();
33   void* frame_buffer = screen_view_->GetBuffer(buffer_id);
34 
35   auto imported_buffer_opt = gralloc_.Import(buffer_handle);
36   if (!imported_buffer_opt) {
37     ALOGE("Failed to Import() framebuffer for post.");
38     return -1;
39   }
40   GrallocBuffer& imported_buffer = *imported_buffer_opt;
41 
42   auto buffer_opt = imported_buffer.Lock();
43   if (!buffer_opt) {
44     ALOGE("Failed to Lock() framebuffer for post.");
45     return -1;
46   }
47 
48   void* buffer = *buffer_opt;
49   memcpy(frame_buffer, buffer, screen_view_->buffer_size());
50 
51   imported_buffer.Unlock();
52 
53   screen_view_->Broadcast(buffer_id);
54   return 0;
55 }  // namespace cuttlefish
56 
IsValidLayer(const hwc_layer_1_t & layer)57 bool BaseComposer::IsValidLayer(const hwc_layer_1_t& layer) {
58   auto buffer_opt = gralloc_.Import(layer.handle);
59   if (!buffer_opt) {
60     ALOGE("Failed to import and validate layer buffer handle.");
61     return false;
62   }
63   GrallocBuffer& buffer = *buffer_opt;
64 
65   auto buffer_width_opt = buffer.GetWidth();
66   if (!buffer_width_opt) {
67     ALOGE("Failed to get layer buffer width.");
68     return false;
69   }
70   uint32_t buffer_width = *buffer_width_opt;
71 
72   auto buffer_height_opt = buffer.GetHeight();
73   if (!buffer_height_opt) {
74     ALOGE("Failed to get layer buffer height.");
75     return false;
76   }
77   uint32_t buffer_height = *buffer_height_opt;
78 
79   if (layer.sourceCrop.left < 0 || layer.sourceCrop.top < 0 ||
80       layer.sourceCrop.right > buffer_width ||
81       layer.sourceCrop.bottom > buffer_height) {
82     ALOGE(
83         "%s: Invalid sourceCrop for buffer handle: sourceCrop = [left = %d, "
84         "right = %d, top = %d, bottom = %d], handle = [width = %d, height = "
85         "%d]",
86         __FUNCTION__, layer.sourceCrop.left, layer.sourceCrop.right,
87         layer.sourceCrop.top, layer.sourceCrop.bottom, buffer_width,
88         buffer_height);
89     return false;
90   }
91   return true;
92 }
93 
PrepareLayers(size_t num_layers,hwc_layer_1_t * layers)94 int BaseComposer::PrepareLayers(size_t num_layers, hwc_layer_1_t* layers) {
95   // find unsupported overlays
96   for (size_t i = 0; i < num_layers; i++) {
97     if (IS_TARGET_FRAMEBUFFER(layers[i].compositionType)) {
98       continue;
99     }
100     layers[i].compositionType = HWC_FRAMEBUFFER;
101   }
102   return 0;
103 }
104 
SetLayers(size_t num_layers,hwc_layer_1_t * layers)105 int BaseComposer::SetLayers(size_t num_layers, hwc_layer_1_t* layers) {
106   for (size_t idx = 0; idx < num_layers; idx++) {
107     if (IS_TARGET_FRAMEBUFFER(layers[idx].compositionType)) {
108       return PostFrameBufferTarget(layers[idx].handle);
109     }
110   }
111   return -1;
112 }
113 
114 }  // namespace cuttlefish
115