Home | History | Annotate | Download | only in hwc2
      1 /*
      2 * Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved.
      3 *
      4 * Redistribution and use in source and binary forms, with or without
      5 * modification, are permitted provided that the following conditions are
      6 * met:
      7 *     * Redistributions of source code must retain the above copyright
      8 *       notice, this list of conditions and the following disclaimer.
      9 *     * Redistributions in binary form must reproduce the above
     10 *       copyright notice, this list of conditions and the following
     11 *       disclaimer in the documentation and/or other materials provided
     12 *       with the distribution.
     13 *     * Neither the name of The Linux Foundation nor the names of its
     14 *       contributors may be used to endorse or promote products derived
     15 *       from this software without specific prior written permission.
     16 *
     17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 #include <utils/constants.h>
     31 #include <utils/debug.h>
     32 #include <sync/sync.h>
     33 #include <stdarg.h>
     34 #include <gr.h>
     35 
     36 #include "hwc_display_virtual.h"
     37 #include "hwc_debugger.h"
     38 
     39 #define __CLASS__ "HWCDisplayVirtual"
     40 
     41 namespace sdm {
     42 
     43 int HWCDisplayVirtual::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t width,
     44                               uint32_t height, int32_t *format, HWCDisplay **hwc_display) {
     45   int status = 0;
     46   HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, callbacks);
     47 
     48   // TODO(user): Populate format correctly
     49   DLOGI("Creating virtual display: w: %d h:%d format:0x%x", width, height, *format);
     50 
     51   status = hwc_display_virtual->Init();
     52   if (status) {
     53     DLOGW("Failed to initialize virtual display");
     54     delete hwc_display_virtual;
     55     return status;
     56   }
     57 
     58   status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On));
     59   if (status) {
     60     DLOGW("Failed to set power mode on virtual display");
     61     Destroy(hwc_display_virtual);
     62     return status;
     63   }
     64 
     65   status = hwc_display_virtual->SetConfig(width, height);
     66   if (status) {
     67     Destroy(hwc_display_virtual);
     68     return status;
     69   }
     70 
     71   // TODO(user): Validate that we support this width/height
     72   status = hwc_display_virtual->SetFrameBufferResolution(width, height);
     73 
     74   if (status) {
     75     DLOGW("Failed to set virtual display FB resolution");
     76     Destroy(hwc_display_virtual);
     77     return status;
     78   }
     79 
     80   *hwc_display = static_cast<HWCDisplay *>(hwc_display_virtual);
     81 
     82   return 0;
     83 }
     84 
     85 void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) {
     86   hwc_display->Deinit();
     87   delete hwc_display;
     88 }
     89 
     90 HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCCallbacks *callbacks)
     91     : HWCDisplay(core_intf, callbacks, kVirtual, HWC_DISPLAY_VIRTUAL, false, NULL,
     92                  DISPLAY_CLASS_VIRTUAL) {
     93 }
     94 
     95 int HWCDisplayVirtual::Init() {
     96   output_buffer_ = new LayerBuffer();
     97   return HWCDisplay::Init();
     98 }
     99 
    100 int HWCDisplayVirtual::Deinit() {
    101   int status = 0;
    102   if (output_buffer_) {
    103     delete output_buffer_;
    104     output_buffer_ = nullptr;
    105   }
    106   status = HWCDisplay::Deinit();
    107 
    108   return status;
    109 }
    110 
    111 HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
    112   auto status = HWC2::Error::None;
    113 
    114   if (display_paused_) {
    115     MarkLayersForGPUBypass();
    116     return status;
    117   }
    118 
    119   BuildLayerStack();
    120   layer_stack_.output_buffer = output_buffer_;
    121   status = PrepareLayerStack(out_num_types, out_num_requests);
    122   return status;
    123 }
    124 
    125 HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) {
    126   auto status = HWC2::Error::None;
    127   if (display_paused_) {
    128     DisplayError error = display_intf_->Flush();
    129     if (error != kErrorNone) {
    130       DLOGE("Flush failed. Error = %d", error);
    131     }
    132   } else {
    133     status = HWCDisplay::CommitLayerStack();
    134     if (status == HWC2::Error::None) {
    135       if (dump_frame_count_ && !flush_ && dump_output_layer_) {
    136         if (output_handle_ && output_handle_->base) {
    137           BufferInfo buffer_info;
    138           const private_handle_t *output_handle =
    139               reinterpret_cast<const private_handle_t *>(output_buffer_->buffer_id);
    140           buffer_info.buffer_config.width = static_cast<uint32_t>(output_handle->width);
    141           buffer_info.buffer_config.height = static_cast<uint32_t>(output_handle->height);
    142           buffer_info.buffer_config.format =
    143               GetSDMFormat(output_handle->format, output_handle->flags);
    144           buffer_info.alloc_buffer_info.size = static_cast<uint32_t>(output_handle->size);
    145           DumpOutputBuffer(buffer_info, reinterpret_cast<void *>(output_handle->base),
    146                            layer_stack_.retire_fence_fd);
    147         }
    148       }
    149 
    150       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
    151     }
    152   }
    153   CloseAcquireFds();
    154   close(output_buffer_->acquire_fence_fd);
    155   return status;
    156 }
    157 
    158 int HWCDisplayVirtual::SetConfig(uint32_t width, uint32_t height) {
    159   DisplayConfigVariableInfo variable_info;
    160   variable_info.x_pixels = width;
    161   variable_info.y_pixels = height;
    162   // TODO(user): Need to get the framerate of primary display and update it.
    163   variable_info.fps = 60;
    164   return display_intf_->SetActiveConfig(&variable_info);
    165 }
    166 
    167 HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) {
    168   if (buf == nullptr || release_fence == 0) {
    169     return HWC2::Error::BadParameter;
    170   }
    171   const private_handle_t *output_handle = static_cast<const private_handle_t *>(buf);
    172 
    173   // Fill output buffer parameters (width, height, format, plane information, fence)
    174   output_buffer_->acquire_fence_fd = dup(release_fence);
    175 
    176   if (output_handle) {
    177     output_buffer_->buffer_id = reinterpret_cast<uint64_t>(output_handle);
    178     int output_handle_format = output_handle->format;
    179 
    180     if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) {
    181       output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888;
    182     }
    183 
    184     output_buffer_->format = GetSDMFormat(output_handle_format, output_handle->flags);
    185 
    186     if (output_buffer_->format == kFormatInvalid) {
    187       return HWC2::Error::BadParameter;
    188     }
    189 
    190     int output_buffer_width, output_buffer_height;
    191     AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(output_handle, output_buffer_width,
    192                                                           output_buffer_height);
    193 
    194     output_buffer_->width = UINT32(output_buffer_width);
    195     output_buffer_->height = UINT32(output_buffer_height);
    196     // TODO(mkavm): Handle DRC and metadata changes
    197     output_buffer_->flags.secure = 0;
    198     output_buffer_->flags.video = 0;
    199 
    200     // TZ Protected Buffer - L1
    201     if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
    202       output_buffer_->flags.secure = 1;
    203     }
    204 
    205     // ToDo: Need to extend for non-RGB formats
    206     output_buffer_->planes[0].fd = output_handle->fd;
    207     output_buffer_->planes[0].offset = output_handle->offset;
    208     output_buffer_->planes[0].stride = UINT32(output_handle->width);
    209   }
    210 
    211   return HWC2::Error::None;
    212 }
    213 
    214 void HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
    215   HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
    216   dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0);
    217 
    218   DLOGI("output_layer_dump_enable %d", dump_output_layer_);
    219 }
    220 
    221 }  // namespace sdm
    222