upload android base code part4

This commit is contained in:
August 2018-08-08 17:00:29 +08:00
parent b9e30e05b1
commit 78ea2404cd
23455 changed files with 5250148 additions and 0 deletions

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2014 - 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 __CONSTANTS_H__
#define __CONSTANTS_H__
#include <stdlib.h>
#include <inttypes.h>
#ifndef PRIu64
#define PRIu64 "llu"
#endif
#define INT(exp) static_cast<int>(exp)
#define FLOAT(exp) static_cast<float>(exp)
#define UINT8(exp) static_cast<uint8_t>(exp)
#define UINT16(exp) static_cast<uint16_t>(exp)
#define UINT32(exp) static_cast<uint32_t>(exp)
#define INT32(exp) static_cast<int32_t>(exp)
#define UINT64(exp) static_cast<uint64_t>(exp)
#define ROUND_UP(number, step) ((((number) + ((step) - 1)) / (step)) * (step))
#define BITMAP(bit) (1 << (bit))
#define ROUND_UP_ALIGN_DOWN(value, a) FLOAT(FloorToMultipleOf(UINT32(value + 0.5f), UINT32(a)))
#define ROUND_UP_ALIGN_UP(value, a) FLOAT(CeilToMultipleOf(UINT32(value + 0.5f), UINT32(a)))
#define IDLE_TIMEOUT_DEFAULT_MS 70
#define IDLE_TIMEOUT_ACTIVE_MS IDLE_TIMEOUT_DEFAULT_MS
#define IDLE_TIMEOUT_INACTIVE_MS 520
#define IS_RGB_FORMAT(format) (((format) < kFormatYCbCr420Planar) ? true: false)
#define BITS_PER_BYTE 8
#define BITS_TO_BYTES(x) (((x) + (BITS_PER_BYTE - 1)) / (BITS_PER_BYTE))
// factor value should be in powers of 2(eg: 1, 2, 4, 8)
template <class T1, class T2>
inline T1 FloorToMultipleOf(const T1 &value, const T2 &factor) {
return (T1)(value & (~(factor - 1)));
}
template <class T1, class T2>
inline T1 CeilToMultipleOf(const T1 &value, const T2 &factor) {
return (T1)((value + (factor - 1)) & (~(factor - 1)));
}
namespace sdm {
const int kThreadPriorityUrgent = -9;
const int kMaxRotatePerLayer = 2;
const uint32_t kMaxBlitTargetLayers = 2;
const int kPageSize = 4096;
const uint32_t kGridSize = 129; // size used for non-linear transformation before Tone-mapping
const uint32_t kLutDim = 17; // Dim of the 3d LUT for tone-mapping.
const int kMaxThermalLevel = 3;
typedef void * Handle;
} // namespace sdm
#endif // __CONSTANTS_H__

View file

@ -0,0 +1,121 @@
/*
* Copyright (c) 2014 - 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 __DEBUG_H__
#define __DEBUG_H__
#include <stdint.h>
#include <core/sdm_types.h>
#include <core/debug_interface.h>
#include <core/display_interface.h>
#define DLOG(tag, method, format, ...) Debug::Get()->method(tag, __CLASS__ "::%s: " format, \
__FUNCTION__, ##__VA_ARGS__)
#define DLOGE_IF(tag, format, ...) DLOG(tag, Error, format, ##__VA_ARGS__)
#define DLOGW_IF(tag, format, ...) DLOG(tag, Warning, format, ##__VA_ARGS__)
#define DLOGI_IF(tag, format, ...) DLOG(tag, Info, format, ##__VA_ARGS__)
#define DLOGD_IF(tag, format, ...) DLOG(tag, Debug, format, ##__VA_ARGS__)
#define DLOGV_IF(tag, format, ...) DLOG(tag, Verbose, format, ##__VA_ARGS__)
#define DLOGE(format, ...) DLOGE_IF(kTagNone, format, ##__VA_ARGS__)
#define DLOGD(format, ...) DLOGD_IF(kTagNone, format, ##__VA_ARGS__)
#define DLOGW(format, ...) DLOGW_IF(kTagNone, format, ##__VA_ARGS__)
#define DLOGI(format, ...) DLOGI_IF(kTagNone, format, ##__VA_ARGS__)
#define DLOGV(format, ...) DLOGV_IF(kTagNone, format, ##__VA_ARGS__)
#define DTRACE_BEGIN(custom_string) Debug::Get()->BeginTrace(__CLASS__, __FUNCTION__, custom_string)
#define DTRACE_END() Debug::Get()->EndTrace()
#define DTRACE_SCOPED() ScopeTracer <Debug> scope_tracer(__CLASS__, __FUNCTION__)
namespace sdm {
class Debug {
public:
static inline void SetDebugHandler(DebugHandler *debug_handler) {
debug_.debug_handler_ = debug_handler;
}
static inline DebugHandler* Get() { return debug_.debug_handler_; }
static int GetSimulationFlag();
static int GetHDMIResolution();
static void GetIdleTimeoutMs(uint32_t *active_ms, uint32_t *inactive_ms);
static int GetBootAnimLayerCount();
static bool IsRotatorDownScaleDisabled();
static bool IsDecimationDisabled();
static int GetMaxPipesPerMixer(DisplayType display_type);
static int GetMaxUpscale();
static bool IsVideoModeEnabled();
static bool IsRotatorUbwcDisabled();
static bool IsRotatorSplitDisabled();
static bool IsScalarDisabled();
static bool IsUbwcTiledFrameBuffer();
static bool IsAVRDisabled();
static bool IsExtAnimDisabled();
static bool IsPartialSplitDisabled();
static bool IsSkipValidateDisabled();
static DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
static int GetExtMaxlayers();
static bool GetProperty(const char *property_name, char *value);
static bool SetProperty(const char *property_name, const char *value);
private:
Debug();
// By default, drop any log messages/traces coming from Display manager. It will be overriden by
// Display manager client when core is successfully initialized.
class DefaultDebugHandler : public DebugHandler {
public:
virtual void Error(DebugTag /*tag*/, const char */*format*/, ...) { }
virtual void Warning(DebugTag /*tag*/, const char */*format*/, ...) { }
virtual void Info(DebugTag /*tag*/, const char */*format*/, ...) { }
virtual void Debug(DebugTag /*tag*/, const char */*format*/, ...) { }
virtual void Verbose(DebugTag /*tag*/, const char */*format*/, ...) { }
virtual void BeginTrace(const char */*class_name*/, const char */*function_name*/,
const char */*custom_string*/) { }
virtual void EndTrace() { }
virtual DisplayError GetProperty(const char */*property_name*/, int */*value*/) {
return kErrorNotSupported;
}
virtual DisplayError GetProperty(const char */*property_name*/, char */*value*/) {
return kErrorNotSupported;
}
virtual DisplayError SetProperty(const char */*property_name*/, const char */*value*/) {
return kErrorNotSupported;
}
};
DefaultDebugHandler default_debug_handler_;
DebugHandler *debug_handler_;
static Debug debug_;
};
} // namespace sdm
#endif // __DEBUG_H__

View file

@ -0,0 +1,63 @@
/*
* 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 __FACTORY_H__
#define __FACTORY_H__
#include <utility>
#include <map>
#include <string>
namespace sdm {
template <class Creator>
class Factory {
public:
int Add(const std::string &name, const Creator &creator) {
map_.insert(std::pair<std::string, Creator>(name, creator));
return 0;
}
Creator Get(const std::string &name) {
typename std::map<std::string, Creator>::iterator it = map_.find(name);
if (it != map_.end()) {
return it->second;
}
return nullptr;
}
private:
std::map<std::string, Creator> map_;
};
} // namespace sdm
#endif // __FACTORY_H__

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, 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 __FORMATS_H__
#define __FORMATS_H__
#include <core/layer_stack.h>
namespace sdm {
bool IsUBWCFormat(LayerBufferFormat format);
bool Is10BitFormat(LayerBufferFormat format);
const char *GetFormatString(const LayerBufferFormat &format);
BufferLayout GetBufferLayout(LayerBufferFormat format);
} // namespace sdm
#endif // __FORMATS_H__

View file

@ -0,0 +1,166 @@
/*
* Copyright (c) 2014 - 2016, 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 __LOCKER_H__
#define __LOCKER_H__
#include <stdint.h>
#include <pthread.h>
#include <sys/time.h>
#define SCOPE_LOCK(locker) Locker::ScopeLock lock(locker)
#define SEQUENCE_ENTRY_SCOPE_LOCK(locker) Locker::SequenceEntryScopeLock lock(locker)
#define SEQUENCE_EXIT_SCOPE_LOCK(locker) Locker::SequenceExitScopeLock lock(locker)
#define SEQUENCE_WAIT_SCOPE_LOCK(locker) Locker::SequenceWaitScopeLock lock(locker)
#define SEQUENCE_CANCEL_SCOPE_LOCK(locker) Locker::SequenceCancelScopeLock lock(locker)
namespace sdm {
class Locker {
public:
class ScopeLock {
public:
explicit ScopeLock(Locker& locker) : locker_(locker) {
locker_.Lock();
}
~ScopeLock() {
locker_.Unlock();
}
private:
Locker &locker_;
};
class SequenceEntryScopeLock {
public:
explicit SequenceEntryScopeLock(Locker& locker) : locker_(locker) {
locker_.Lock();
locker_.sequence_wait_ = 1;
}
~SequenceEntryScopeLock() {
locker_.Unlock();
}
private:
Locker &locker_;
};
class SequenceExitScopeLock {
public:
explicit SequenceExitScopeLock(Locker& locker) : locker_(locker) {
locker_.Lock();
locker_.sequence_wait_ = 0;
}
~SequenceExitScopeLock() {
locker_.Broadcast();
locker_.Unlock();
}
private:
Locker &locker_;
};
class SequenceWaitScopeLock {
public:
explicit SequenceWaitScopeLock(Locker& locker) : locker_(locker), error_(false) {
locker_.Lock();
while (locker_.sequence_wait_ == 1) {
locker_.Wait();
error_ = (locker_.sequence_wait_ == -1);
}
}
~SequenceWaitScopeLock() {
locker_.Unlock();
}
bool IsError() {
return error_;
}
private:
Locker &locker_;
bool error_;
};
class SequenceCancelScopeLock {
public:
explicit SequenceCancelScopeLock(Locker& locker) : locker_(locker) {
locker_.Lock();
locker_.sequence_wait_ = -1;
}
~SequenceCancelScopeLock() {
locker_.Broadcast();
locker_.Unlock();
}
private:
Locker &locker_;
};
Locker() : sequence_wait_(0) {
pthread_mutex_init(&mutex_, 0);
pthread_cond_init(&condition_, 0);
}
~Locker() {
pthread_mutex_destroy(&mutex_);
pthread_cond_destroy(&condition_);
}
void Lock() { pthread_mutex_lock(&mutex_); }
void Unlock() { pthread_mutex_unlock(&mutex_); }
void Signal() { pthread_cond_signal(&condition_); }
void Broadcast() { pthread_cond_broadcast(&condition_); }
void Wait() { pthread_cond_wait(&condition_, &mutex_); }
int WaitFinite(int ms) {
struct timespec ts;
struct timeval tv;
gettimeofday(&tv, NULL);
ts.tv_sec = tv.tv_sec + ms/1000;
ts.tv_nsec = tv.tv_usec*1000 + (ms%1000)*1000000;
ts.tv_sec += ts.tv_nsec/1000000000L;
ts.tv_nsec %= 1000000000L;
return pthread_cond_timedwait(&condition_, &mutex_, &ts);
}
private:
pthread_mutex_t mutex_;
pthread_cond_t condition_;
int sequence_wait_; // This flag is set to 1 on sequence entry, 0 on exit, and -1 on cancel.
// Some routines will wait for sequence of function calls to finish
// so that capturing a transitionary snapshot of context is prevented.
// If flag is set to -1, these routines will exit without doing any
// further processing.
};
} // namespace sdm
#endif // __LOCKER_H__

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2015-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 __RECT_H__
#define __RECT_H__
#include <stdint.h>
#include <core/sdm_types.h>
#include <core/layer_stack.h>
#include <utils/debug.h>
namespace sdm {
enum RectOrientation {
kOrientationPortrait,
kOrientationLandscape,
kOrientationUnknown,
};
bool IsValid(const LayerRect &rect);
bool IsCongruent(const LayerRect &rect1, const LayerRect &rect2);
void Log(DebugTag debug_tag, const char *prefix, const LayerRect &roi);
void Normalize(const uint32_t &align_x, const uint32_t &align_y, LayerRect *rect);
LayerRect Union(const LayerRect &rect1, const LayerRect &rect2);
LayerRect Intersection(const LayerRect &rect1, const LayerRect &rect2);
LayerRect Subtract(const LayerRect &rect1, const LayerRect &rect2);
LayerRect Reposition(const LayerRect &rect1, const int &x_offset, const int &y_offset);
void SplitLeftRight(const LayerRect &in_rect, uint32_t split_count, uint32_t align_x,
bool flip_horizontal, LayerRect *out_rects);
void SplitTopBottom(const LayerRect &in_rect, uint32_t split_count, uint32_t align_y,
bool flip_horizontal, LayerRect *out_rects);
void MapRect(const LayerRect &src_domain, const LayerRect &dst_domain, const LayerRect &in_rect,
LayerRect *out_rect);
void TransformHV(const LayerRect &src_domain, const LayerRect &in_rect,
const LayerTransform &transform, LayerRect *out_rect);
RectOrientation GetOrientation(const LayerRect &in_rect);
} // namespace sdm
#endif // __RECT_H__

View file

@ -0,0 +1,143 @@
/*
* 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 __SYNC_TASK_H__
#define __SYNC_TASK_H__
#include <thread>
#include <mutex>
#include <condition_variable> // NOLINT
namespace sdm {
template <class TaskCode>
class SyncTask {
public:
// This class need to be overridden by caller to pass on a task context.
class TaskContext {
public:
virtual ~TaskContext() { }
};
// Methods to callback into caller for command codes executions in worker thread.
class TaskHandler {
public:
virtual ~TaskHandler() { }
virtual void OnTask(const TaskCode &task_code, TaskContext *task_context) = 0;
};
explicit SyncTask(TaskHandler &task_handler) : task_handler_(task_handler) {
// Block caller thread until worker thread has started and ready to listen to task commands.
// Worker thread will signal as soon as callback is received in the new thread.
std::unique_lock<std::mutex> caller_lock(caller_mutex_);
std::thread worker_thread(SyncTaskThread, this);
worker_thread_.swap(worker_thread);
caller_cv_.wait(caller_lock);
}
~SyncTask() {
// Task code does not matter here.
PerformTask(task_code_, nullptr, true);
worker_thread_.join();
}
void PerformTask(const TaskCode &task_code, TaskContext *task_context) {
PerformTask(task_code, task_context, false);
}
private:
void PerformTask(const TaskCode &task_code, TaskContext *task_context, bool terminate) {
std::unique_lock<std::mutex> caller_lock(caller_mutex_);
// New scope to limit scope of worker lock to this block.
{
// Set task command code and notify worker thread.
std::unique_lock<std::mutex> worker_lock(worker_mutex_);
task_code_ = task_code;
task_context_ = task_context;
worker_thread_exit_ = terminate;
pending_code_ = true;
worker_cv_.notify_one();
}
// Wait for worker thread to finish and signal.
caller_cv_.wait(caller_lock);
}
static void SyncTaskThread(SyncTask *sync_task) {
if (sync_task) {
sync_task->OnThreadCallback();
}
}
void OnThreadCallback() {
// Acquire worker lock and start waiting for events.
// Wait must start before caller thread can post events, otherwise posted events will be lost.
// Caller thread will be blocked until worker thread signals readiness.
std::unique_lock<std::mutex> worker_lock(worker_mutex_);
// New scope to limit scope of caller lock to this block.
{
// Signal caller thread that worker thread is ready to listen to events.
std::unique_lock<std::mutex> caller_lock(caller_mutex_);
caller_cv_.notify_one();
}
while (!worker_thread_exit_) {
// Add predicate to handle spurious interrupts.
// Wait for caller thread to signal new command codes.
worker_cv_.wait(worker_lock, [this] { return pending_code_; });
// Call task handler which is implemented by the caller.
if (!worker_thread_exit_) {
task_handler_.OnTask(task_code_, task_context_);
}
pending_code_ = false;
// Notify completion of current task to the caller thread which is blocked.
std::unique_lock<std::mutex> caller_lock(caller_mutex_);
caller_cv_.notify_one();
}
}
TaskHandler &task_handler_;
TaskCode task_code_;
TaskContext *task_context_ = nullptr;
std::thread worker_thread_;
std::mutex caller_mutex_;
std::mutex worker_mutex_;
std::condition_variable caller_cv_;
std::condition_variable worker_cv_;
bool worker_thread_exit_ = false;
bool pending_code_ = false;
};
} // namespace sdm
#endif // __SYNC_TASK_H__

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2015, 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 __SYS_H__
#define __SYS_H__
#include <sys/eventfd.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <pthread.h>
#include <fstream>
#ifdef SDM_VIRTUAL_DRIVER
#include <virtual_driver.h>
#endif
namespace sdm {
class Sys {
public:
#ifndef SDM_VIRTUAL_DRIVER
typedef std::fstream fstream;
#else
typedef VirtualFStream fstream;
#endif
// Pointers to system calls which are either mapped to actual system call or virtual driver.
#ifdef TARGET_HEADLESS
typedef int (*ioctl)(int, unsigned long int, ...); // NOLINT
#else
typedef int (*ioctl)(int, int, ...);
#endif
typedef int (*access)(const char *, int);
typedef int (*open)(const char *, int, ...);
typedef int (*close)(int);
typedef int (*poll)(struct pollfd *, nfds_t, int);
typedef ssize_t (*pread)(int, void *, size_t, off_t);
typedef ssize_t (*pwrite)(int, const void *, size_t, off_t);
typedef int (*pthread_cancel)(pthread_t thread);
typedef int (*dup)(int fd);
typedef ssize_t (*read)(int, void *, size_t);
typedef ssize_t (*write)(int, const void *, size_t);
typedef int (*eventfd)(unsigned int, int);
static bool getline_(fstream &fs, std::string &line); // NOLINT
static ioctl ioctl_;
static access access_;
static open open_;
static close close_;
static poll poll_;
static pread pread_;
static pwrite pwrite_;
static pthread_cancel pthread_cancel_;
static dup dup_;
static read read_;
static write write_;
static eventfd eventfd_;
};
class DynLib {
public:
~DynLib();
bool Open(const char *lib_name);
bool Sym(const char *func_name, void **func_ptr);
const char * Error() { return ::dlerror(); }
operator bool() const { return lib_ != NULL; }
private:
void Close();
void *lib_ = NULL;
};
} // namespace sdm
#endif // __SYS_H__

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2016 - 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 __UTILS_H__
#define __UTILS_H__
namespace sdm {
float gcd(float a, float b);
float lcm(float a, float b);
void CloseFd(int *fd);
enum class DriverType {
FB = 0,
DRM,
};
DriverType GetDriverType();
} // namespace sdm
#endif // __UTILS_H__