Files

211 lines
5.6 KiB
C++

/*
* Copyright (C) 2024 Amlogic Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include <map>
#include <DrmTypes.h>
#include "MesonTypes.h"
#include "display_interface.h"
#include "MesonDisplay.h"
#include "DispAdptLog.h"
std::map<int, std::shared_ptr<MesonDisplay>> mDisplays;
std::mutex mMutex;
static std::shared_ptr<MesonDisplay> getDisplay(int displayId)
{
std::shared_ptr<MesonDisplay> display;
std::unique_lock<std::mutex> lock(mMutex);
auto disp = mDisplays.find(displayId);
if (disp != mDisplays.end())
return disp->second;
DispAdptLogInit();
display = std::make_shared<MesonDisplay>(displayId);
display->initialize();
mDisplays.emplace(displayId, display);
return display;
}
int32_t dispAdptCreateLayer(int32_t displayId, int32_t *layerId)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
int32_t id = 0;
int ret;
if (display.get()) {
ret = display->createLayer(id);
*layerId = id;
return ret;
}
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptDestroyLayer(int32_t displayId, int32_t layerId)
{
DTRACE_ALL();
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
bool destroy = false;
int ret = DISPLAY_RETURN_FAIL;
if (display.get()) {
ret = display->destroyLayer(layerId);
if (display->getLayerTotal() == 0)
destroy = true;
}
if (destroy && ret != MESON_ERROR_HAS_CHANGES) {
dispAdptDestroyDisplay(displayId);
}
return ret;
}
int32_t dispAdptSetTunnelId(int32_t displayId, int32_t layerId, int32_t tunnelId)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setTunnelId(layerId, tunnelId);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptSetLayerIndex(int32_t displayId, int32_t layerId, int32_t index)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setLayerIndex(layerId, index);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptSetLayerZorder(int32_t displayId, int32_t layerId, int32_t zorder)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setLayerZorder(layerId, zorder);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptSetLayerSourceCrop(int32_t displayId, int32_t layerId, struct rect crop)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setLayerSourceCrop(layerId, crop);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptSetLayerDispFrame(int32_t displayId, int32_t layerId, struct rect frame)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
drm_rect_t rect = { 0 };
rect.left = frame.x;
rect.top = frame.y;
rect.right = frame.x + frame.w;
rect.bottom = frame.y + frame.h;
if (display.get())
return display->setLayerDispFrame(layerId, rect);
return DISPLAY_RETURN_FAIL;
}
// get drm vsync latency
int32_t dispAdptGetVsyncDelayLatency(int32_t displayId, int32_t layerId)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->getVsyncDelayLatency(layerId);
return DISPLAY_RETURN_FAIL;
}
// switch videoplane to main
int32_t dispAdptSetVideoDisplaytoMainplane(int32_t displayId)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setVideoDisplaytoMainplane();
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptSetLayerTransform(int32_t displayId, int32_t layerId, int32_t transform)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->setLayerTransform(layerId, transform);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptRegisterCallback(int32_t displayId, int32_t layerId,
void *context, meson_callback_t *cb)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
display->registerCallback(layerId, context, cb);
return 0;
}
int32_t dispAdptRegisterPageFlipCallback(int32_t displayId,
int32_t layerId, void *context, adpt_page_flip_callback *cb)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
display->registerPageFlipCallBack(layerId, context, cb);
return 0;
}
int32_t dispAdptGetVideoDelay(int32_t displayId, int32_t fps)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->getVideoDelay(fps);
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptWaitVBlank(int32_t displayId)
{
std::shared_ptr<MesonDisplay> display = getDisplay(displayId);
if (display.get())
return display->waitVBlank();
return DISPLAY_RETURN_FAIL;
}
int32_t dispAdptDestroyDisplay(int32_t displayId)
{
std::unique_lock<std::mutex> lock(mMutex);
auto disp = mDisplays.find(displayId);
if (disp != mDisplays.end())
mDisplays.erase(displayId);
if (mDisplays.size() == 0)
DispAdptLogDestroy();
return 0;
}