Skip to content

Commit

Permalink
sdm: drm: Add support for default non-atomic mode
Browse files Browse the repository at this point in the history
Add support for booting up with default non-atomic mode
Add libdrmutils that currently has:
DRMMaster:
    Creates a master DRM session
    Converts ION handles to DRM FB_ID
DRMResMgr:
    Enables a default display path by providing APIs for
    connector id, crtc id, mode etc

Change-Id: I1dc697d2cc5e3fa744c99e2c9ddd57bf06e78c4f
CRs-fixed: 1114808
  • Loading branch information
Saurabh Shah committed Feb 7, 2017
1 parent e37bdce commit 7d476ed
Show file tree
Hide file tree
Showing 47 changed files with 2,861 additions and 323 deletions.
2 changes: 1 addition & 1 deletion Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ display-hals := include libqservice libqdutils $(sdm-libs)/utils $(sdm-libs)/cor

ifneq ($(TARGET_IS_HEADLESS), true)
display-hals += libcopybit liblight libmemtrack hdmi_cec \
$(sdm-libs)/hwc $(sdm-libs)/hwc2 gpu_tonemapper
$(sdm-libs)/hwc $(sdm-libs)/hwc2 gpu_tonemapper libdrmutils
endif

ifneq ($(TARGET_USES_GRALLOC1), true)
Expand Down
4 changes: 4 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ display_top := $(call my-dir)
#Common C flags
common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
common_flags += -Wconversion -Wall -Werror -std=c++11
ifneq ($(TARGET_IS_HEADLESS), true)
common_flags += -DCOMPILE_DRM
endif

ifeq ($(TARGET_USES_COLOR_METADATA), true)
common_flags += -DUSE_COLOR_METADATA
Expand All @@ -28,6 +31,7 @@ common_includes += $(display_top)/libqservice
common_includes += $(display_top)/gpu_tonemapper
ifneq ($(TARGET_IS_HEADLESS), true)
common_includes += $(display_top)/libcopybit
common_includes += $(display_top)/libdrmutils
endif

common_includes += $(display_top)/include
Expand Down
15 changes: 15 additions & 0 deletions libdrmutils/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libdrmutils
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
external/libdrm
LOCAL_SHARED_LIBRARIES := libdrm
LOCAL_CFLAGS := -DLOG_TAG=\"DRMUTILS\" -Wall -std=c++11 -Werror
LOCAL_CLANG := true
LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
LOCAL_SRC_FILES := drm_master.cpp drm_res_mgr.cpp
LOCAL_COPY_HEADERS_TO := qcom/display
LOCAL_COPY_HEADERS := drm_master.h drm_res_mgr.h
include $(BUILD_SHARED_LIBRARY)
75 changes: 75 additions & 0 deletions libdrmutils/drm_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2017, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __DRM_LOGGER_H__
#define __DRM_LOGGER_H__

#include <utility>

namespace drm_utils {

class DRMLogger {
public:
virtual ~DRMLogger() {}
virtual void Error(const char *format, ...) = 0;
virtual void Info(const char *format, ...) = 0;
virtual void Debug(const char *format, ...) = 0;

static void Set(DRMLogger *logger) { s_instance = logger; }
static DRMLogger *Get() { return s_instance; }

private:
static DRMLogger *s_instance;
};

template <typename... T>
void DRM_LOGE(const char *format, T&&... args) {
if (DRMLogger::Get()) {
DRMLogger::Get()->Error(format, std::forward<T>(args)...);
}
}

template <typename... T>
void DRM_LOGI(const char *format, T&&... args) {
if (DRMLogger::Get()) {
DRMLogger::Get()->Info(format, std::forward<T>(args)...);
}
}

template <typename... T>
void DRM_LOGD_IF(bool pred, const char *format, T&&... args) {
if (pred && DRMLogger::Get()) {
DRMLogger::Get()->Debug(format, std::forward<T>(args)...);
}
}

} // namespace drm_utils

#endif // __DRM_LOGGER_H__

137 changes: 137 additions & 0 deletions libdrmutils/drm_master.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2017, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm/drm_fourcc.h>

#include <algorithm>
#include <iterator>

#include "drm_master.h"

#define __CLASS__ "DRMMaster"

using std::mutex;
using std::lock_guard;
using std::begin;
using std::copy;
using std::end;
using std::fill;

namespace drm_utils {

DRMLogger *DRMLogger::s_instance = nullptr;
DRMMaster *DRMMaster::s_instance = nullptr;
mutex DRMMaster::s_lock;

int DRMMaster::GetInstance(DRMMaster **master) {
lock_guard<mutex> obj(s_lock);

if (!s_instance) {
s_instance = new DRMMaster();
if (s_instance->Init() < 0) {
delete s_instance;
s_instance = nullptr;
return -ENODEV;
}
}

*master = s_instance;
return 0;
}

int DRMMaster::Init() {
dev_fd_ = drmOpen("msm_drm", nullptr);
if (dev_fd_ < 0) {
DRM_LOGE("%s::%s: drmOpen failed with error %d", __CLASS__, __FUNCTION__, dev_fd_);
return -ENODEV;
}

return 0;
}

DRMMaster::~DRMMaster() {
drmClose(dev_fd_);
dev_fd_ = -1;
}

int DRMMaster::CreateFbId(const DRMBuffer &drm_buffer, uint32_t *gem_handle, uint32_t *fb_id) {
int ret = drmPrimeFDToHandle(dev_fd_, drm_buffer.fd, gem_handle);
if (ret) {
DRM_LOGE("%s::%s: drmPrimeFDToHandle failed with error %d", __CLASS__, __FUNCTION__, ret);
return ret;
}

uint32_t gem_handles[4] = {0};
uint32_t pitches[4] = {0};
uint32_t offsets[4] = {0};
uint64_t modifier[4] = {0};

fill(begin(gem_handles), begin(gem_handles) + drm_buffer.num_planes, *gem_handle);
copy(begin(drm_buffer.stride), end(drm_buffer.stride), begin(pitches));
copy(begin(drm_buffer.offset), end(drm_buffer.offset), begin(offsets));
fill(begin(modifier), begin(modifier) + drm_buffer.num_planes, drm_buffer.drm_format_modifier);

ret = drmModeAddFB3(dev_fd_, drm_buffer.width, drm_buffer.height, drm_buffer.drm_format,
gem_handles, pitches, offsets, modifier, fb_id, DRM_MODE_FB_MODIFIERS);
if (ret) {
DRM_LOGE("%s::%s: drmModeAddFB3 failed with error %d", __CLASS__, __FUNCTION__, ret);
struct drm_gem_close gem_close = {};
gem_close.handle = *gem_handle;
int ret1 = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
if (ret1) {
DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret1);
}
}

return ret;
}

int DRMMaster::RemoveFbId(uint32_t gem_handle, uint32_t fb_id) {
int ret = drmModeRmFB(dev_fd_, fb_id);
if (ret) {
DRM_LOGE("%s::%s: drmModeRmFB failed with error %d", __CLASS__, __FUNCTION__, ret);
}

struct drm_gem_close gem_close = {};
gem_close.handle = gem_handle;
ret = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
if (ret) {
DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret);
}

return ret;
}

} // namespace drm_utils
94 changes: 94 additions & 0 deletions libdrmutils/drm_master.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2017, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __DRM_MASTER_H__
#define __DRM_MASTER_H__

#include <mutex>

#include "drm_logger.h"

namespace drm_utils {

struct DRMBuffer {
int fd = -1;
uint32_t width = 0;
uint32_t height = 0;
uint32_t drm_format = 0;
uint64_t drm_format_modifier = 0;
uint32_t stride[4] = {};
uint32_t offset[4] = {};
uint32_t num_planes = 1;
};

class DRMMaster {
public:
~DRMMaster();
/* Converts from ION fd --> Prime Handle --> FB_ID.
* Input:
* drm_buffer: A DRMBuffer obj that packages description of buffer
* Output:
* fb_id: Pointer to store DRM framebuffer id into
* Returns:
* ioctl error code
*/
int CreateFbId(const DRMBuffer &drm_buffer, uint32_t *gem_handle, uint32_t *fb_id);
/* Removes the fb_id from DRM
* Input:
* fb_id: DRM FB to be removed
* Returns:
* ioctl error code
*/
int RemoveFbId(uint32_t gem_handle, uint32_t fb_id);
/* Poplulates master DRM fd
* Input:
* fd: Pointer to store master fd into
*/
void GetHandle(int *fd) { *fd = dev_fd_; }

/* Creates an instance of DRMMaster if it doesn't exist and initializes it. Threadsafe.
* Input:
* master: Pointer to store a pointer to the instance
* Returns:
* -ENODEV if device cannot be opened or initilization fails
*/
static int GetInstance(DRMMaster **master);

private:
DRMMaster() {}
int Init();

int dev_fd_ = -1; // Master fd for DRM
static DRMMaster *s_instance; // Singleton instance
static std::mutex s_lock;
};

} // namespace drm_utils

#endif // __DRM_MASTER_H__
Loading

0 comments on commit 7d476ed

Please sign in to comment.