Home | History | Annotate | Download | only in legacy
      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 "base_composer.h"
     18 
     19 #include <string.h>
     20 
     21 #include <cutils/log.h>
     22 #include <hardware/gralloc.h>
     23 
     24 #include "common/vsoc/lib/screen_region_view.h"
     25 #include "guest/hals/gralloc/legacy/gralloc_vsoc_priv.h"
     26 
     27 using vsoc::screen::ScreenRegionView;
     28 
     29 namespace cvd {
     30 
     31 namespace {
     32 
     33 void BroadcastFrameBufferChanged(int index) {
     34   ScreenRegionView::GetInstance()->BroadcastNewFrame(
     35       static_cast<uint32_t>(index));
     36 }
     37 
     38 }  // namespace
     39 
     40 BaseComposer::BaseComposer(int64_t vsync_base_timestamp,
     41                            int32_t vsync_period_ns)
     42     : vsync_base_timestamp_(vsync_base_timestamp),
     43       vsync_period_ns_(vsync_period_ns),
     44       fb_broadcaster_(BroadcastFrameBufferChanged) {
     45   hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
     46                 reinterpret_cast<const hw_module_t**>(&gralloc_module_));
     47 }
     48 
     49 BaseComposer::~BaseComposer() {}
     50 
     51 FbBroadcaster BaseComposer::ReplaceFbBroadcaster(FbBroadcaster fb_broadcaster) {
     52   FbBroadcaster tmp = fb_broadcaster_;
     53   fb_broadcaster_ = fb_broadcaster;
     54   return tmp;
     55 }
     56 
     57 void BaseComposer::Dump(char* buff __unused, int buff_len __unused) {}
     58 
     59 void BaseComposer::Broadcast(int fb_index) {
     60   fb_broadcaster_(fb_index);
     61 }
     62 
     63 int BaseComposer::PostFrameBufferTarget(buffer_handle_t buffer_handle) {
     64   int fb_index = NextScreenBuffer();
     65   if (fb_index < 0) {
     66     ALOGE("Could not get the next buffer. Is the screen region large enough?");
     67     return -1;
     68   }
     69   auto screen_view = ScreenRegionView::GetInstance();
     70   void* frame_buffer = screen_view->GetBuffer(fb_index);
     71   const private_handle_t* p_handle =
     72       reinterpret_cast<const private_handle_t*>(buffer_handle);
     73   void* buffer;
     74   int retval = gralloc_module_->lock(gralloc_module_, buffer_handle,
     75                                      GRALLOC_USAGE_SW_READ_OFTEN, 0, 0,
     76                                      p_handle->x_res, p_handle->y_res, &buffer);
     77   if (retval != 0) {
     78     ALOGE("Got error code %d from lock function", retval);
     79     return -1;
     80   }
     81   memcpy(frame_buffer, buffer, screen_view->buffer_size());
     82   Broadcast(fb_index);
     83   return 0;
     84 }  // namespace cvd
     85 
     86 int BaseComposer::PrepareLayers(size_t num_layers, vsoc_hwc_layer* layers) {
     87   // find unsupported overlays
     88   for (size_t i = 0; i < num_layers; i++) {
     89     if (IS_TARGET_FRAMEBUFFER(layers[i].compositionType)) {
     90       continue;
     91     }
     92     layers[i].compositionType = HWC_FRAMEBUFFER;
     93   }
     94   return 0;
     95 }
     96 
     97 int BaseComposer::SetLayers(size_t num_layers, vsoc_hwc_layer* layers) {
     98   for (size_t idx = 0; idx < num_layers; idx++) {
     99     if (IS_TARGET_FRAMEBUFFER(layers[idx].compositionType)) {
    100       return PostFrameBufferTarget(layers[idx].handle);
    101     }
    102   }
    103   return -1;
    104 }
    105 
    106 int BaseComposer::NextScreenBuffer() {
    107   int num_buffers = ScreenRegionView::GetInstance()->number_of_buffers();
    108   last_frame_buffer_ =
    109       num_buffers > 0 ? (last_frame_buffer_ + 1) % num_buffers : -1;
    110   return last_frame_buffer_;
    111 }
    112 
    113 }  // namespace cvd
    114