Home | History | Annotate | Download | only in test
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/ozone/platform/dri/test/mock_dri_wrapper.h"
      6 
      7 #include <xf86drm.h>
      8 #include <xf86drmMode.h>
      9 
     10 #include "third_party/skia/include/core/SkCanvas.h"
     11 #include "ui/ozone/platform/dri/hardware_display_controller.h"
     12 
     13 namespace ui {
     14 
     15 namespace {
     16 
     17 template<class Object> Object* DrmAllocator() {
     18   return static_cast<Object*>(drmMalloc(sizeof(Object)));
     19 }
     20 
     21 }  // namespace
     22 
     23 MockDriWrapper::MockDriWrapper(int fd)
     24     : DriWrapper(""),
     25       get_crtc_call_count_(0),
     26       set_crtc_call_count_(0),
     27       restore_crtc_call_count_(0),
     28       add_framebuffer_call_count_(0),
     29       remove_framebuffer_call_count_(0),
     30       page_flip_call_count_(0),
     31       overlay_flip_call_count_(0),
     32       set_crtc_expectation_(true),
     33       add_framebuffer_expectation_(true),
     34       page_flip_expectation_(true),
     35       create_dumb_buffer_expectation_(true),
     36       current_framebuffer_(0) {
     37   fd_ = fd;
     38 }
     39 
     40 MockDriWrapper::~MockDriWrapper() {
     41   fd_ = -1;
     42 }
     43 
     44 ScopedDrmCrtcPtr MockDriWrapper::GetCrtc(uint32_t crtc_id) {
     45   get_crtc_call_count_++;
     46   return ScopedDrmCrtcPtr(DrmAllocator<drmModeCrtc>());
     47 }
     48 
     49 bool MockDriWrapper::SetCrtc(uint32_t crtc_id,
     50                              uint32_t framebuffer,
     51                              std::vector<uint32_t> connectors,
     52                              drmModeModeInfo* mode) {
     53   current_framebuffer_ = framebuffer;
     54   set_crtc_call_count_++;
     55   return set_crtc_expectation_;
     56 }
     57 
     58 bool MockDriWrapper::SetCrtc(drmModeCrtc* crtc,
     59                              std::vector<uint32_t> connectors) {
     60   restore_crtc_call_count_++;
     61   return true;
     62 }
     63 
     64 ScopedDrmConnectorPtr MockDriWrapper::GetConnector(uint32_t connector_id) {
     65   return ScopedDrmConnectorPtr(DrmAllocator<drmModeConnector>());
     66 }
     67 
     68 bool MockDriWrapper::AddFramebuffer(uint32_t width,
     69                                     uint32_t height,
     70                                     uint8_t depth,
     71                                     uint8_t bpp,
     72                                     uint32_t stride,
     73                                     uint32_t handle,
     74                                     uint32_t* framebuffer) {
     75   add_framebuffer_call_count_++;
     76   *framebuffer = add_framebuffer_call_count_;
     77   return add_framebuffer_expectation_;
     78 }
     79 
     80 bool MockDriWrapper::RemoveFramebuffer(uint32_t framebuffer) {
     81   remove_framebuffer_call_count_++;
     82   return true;
     83 }
     84 
     85 bool MockDriWrapper::PageFlip(uint32_t crtc_id,
     86                               uint32_t framebuffer,
     87                               void* data) {
     88   page_flip_call_count_++;
     89   current_framebuffer_ = framebuffer;
     90   controllers_.push(static_cast<ui::HardwareDisplayController*>(data));
     91   return page_flip_expectation_;
     92 }
     93 
     94 bool MockDriWrapper::PageFlipOverlay(uint32_t crtc_id,
     95                                      uint32_t framebuffer,
     96                                      const gfx::Rect& location,
     97                                      const gfx::RectF& source,
     98                                      int overlay_plane) {
     99   overlay_flip_call_count_++;
    100   return true;
    101 }
    102 
    103 ScopedDrmPropertyPtr MockDriWrapper::GetProperty(drmModeConnector* connector,
    104                                                  const char* name) {
    105   return ScopedDrmPropertyPtr(DrmAllocator<drmModePropertyRes>());
    106 }
    107 
    108 bool MockDriWrapper::SetProperty(uint32_t connector_id,
    109                                  uint32_t property_id,
    110                                  uint64_t value) {
    111   return true;
    112 }
    113 
    114 ScopedDrmPropertyBlobPtr MockDriWrapper::GetPropertyBlob(
    115     drmModeConnector* connector,
    116     const char* name) {
    117   return ScopedDrmPropertyBlobPtr(DrmAllocator<drmModePropertyBlobRes>());
    118 }
    119 
    120 bool MockDriWrapper::SetCursor(uint32_t crtc_id,
    121                                uint32_t handle,
    122                                const gfx::Size& size) {
    123   return true;
    124 }
    125 
    126 bool MockDriWrapper::MoveCursor(uint32_t crtc_id, const gfx::Point& point) {
    127   return true;
    128 }
    129 
    130 void MockDriWrapper::HandleEvent(drmEventContext& event) {
    131   CHECK(!controllers_.empty());
    132   controllers_.front()->OnPageFlipEvent(0, 0, 0);
    133   controllers_.pop();
    134 }
    135 
    136 bool MockDriWrapper::CreateDumbBuffer(const SkImageInfo& info,
    137                                       uint32_t* handle,
    138                                       uint32_t* stride,
    139                                       void** pixels) {
    140   if (!create_dumb_buffer_expectation_)
    141     return false;
    142 
    143   *handle = 0;
    144   *stride = info.minRowBytes();
    145   *pixels = new char[info.getSafeSize(*stride)];
    146   buffers_.push_back(
    147       skia::AdoptRef(SkSurface::NewRasterDirect(info, *pixels, *stride)));
    148   buffers_.back()->getCanvas()->clear(SK_ColorBLACK);
    149 
    150   return true;
    151 }
    152 
    153 void MockDriWrapper::DestroyDumbBuffer(const SkImageInfo& info,
    154                                        uint32_t handle,
    155                                        uint32_t stride,
    156                                        void* pixels) {
    157   delete[] static_cast<char*>(pixels);
    158 }
    159 
    160 }  // namespace ui
    161