blob: e70106c275717db1c3829b284a479a8547722efe [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "msd_img_connection.h"
extern "C"
{
#include "process_stats.h"
#include "pvrsrv.h"
#include "pvrsrv_error.h"
#include "srvcore.h"
#include "rgxdevice.h"
}
static thread_local MsdImgConnection *current_connection;
class ScopedSetConnection
{
public:
explicit ScopedSetConnection(MsdImgConnection *connection)
{
DASSERT(!current_connection);
current_connection = connection;
}
~ScopedSetConnection()
{
DASSERT(current_connection);
current_connection = nullptr;
}
};
MsdImgConnection::MsdImgConnection(Owner *owner, msd_client_id_t client_id) : owner_(owner), client_id_(client_id)
{
magic_ = kMagic;
snprintf(client_name_, sizeof(client_name_), "Client ID %d", client_id);
}
MsdImgConnection::~MsdImgConnection()
{
ScopedSetConnection set_connection(this);
if (connection_data_)
{
PVRSRVConnectionDisconnect(connection_data_);
}
}
MsdImgConnection *
MsdImgConnection::GetCurrentConnection()
{
return current_connection;
}
bool
MsdImgConnection::Init()
{
ScopedSetConnection set_connection(this);
void *data;
PVRSRV_ERROR srv_err = PVRSRVConnectionConnect(&data, this);
if (srv_err != PVRSRV_OK)
{
return DRETF(false, "Failed to create PVRSRV connection: %d", srv_err);
}
connection_data_ = reinterpret_cast<CONNECTION_DATA *>(data);
return true;
}
PVRSRV_DEVICE_NODE *
OSGetDevData(CONNECTION_DATA *psConnection)
{
MsdImgConnection *connection = MsdImgConnection::Cast(psConnection->hOsPrivateData);
return connection->owner()->device_node();
}
PVRSRV_ERROR
OSConnectionPrivateDataInit(IMG_HANDLE *phOsPrivateData, void *pvOSData)
{
*phOsPrivateData = pvOSData;
return PVRSRV_OK;
}
PVRSRV_ERROR
OSConnectionPrivateDataDeInit(IMG_HANDLE hOsPrivateData)
{
// Data should be freed by connection close.
return PVRSRV_OK;
}