add future in AbstractDisplayController

Signed-off-by: xiaojianfeng <xiaojianfeng3@huawei.com>
Change-Id: Ife15eaf8916b5c2af4c2edc51cbece369d5393ae
This commit is contained in:
xiaojianfeng
2022-01-26 11:30:55 +08:00
parent be6ff04389
commit cffd1079c9
4 changed files with 72 additions and 20 deletions
+11 -4
View File
@@ -26,6 +26,7 @@
#include "screen.h"
#include "abstract_display.h"
#include "transaction/rs_interfaces.h"
#include "future.h"
namespace OHOS::Rosen {
class AbstractDisplayController : public RefBase {
@@ -55,22 +56,28 @@ private:
sptr<AbstractScreenController::AbstractScreenCallback> abstractScreenCallback_;
OHOS::Rosen::RSInterfaces *rsInterface_;
class ScreenshotCallback : public SurfaceCaptureCallback {
class ScreenshotCallback : public SurfaceCaptureCallback, public Future<std::shared_ptr<Media::PixelMap>> {
public:
ScreenshotCallback() = default;
~ScreenshotCallback() {};
void OnSurfaceCapture(std::shared_ptr<Media::PixelMap> pixelmap) override
{
if (flag_ == false) {
FutureCall(pixelmap);
}
protected:
void Call(std::shared_ptr<Media::PixelMap> pixelmap) override
{
if (!flag_) {
flag_ = true;
pixelMap_ = pixelmap;
}
}
bool IsPixelMapOk()
bool IsReady() override
{
return flag_;
}
std::shared_ptr<Media::PixelMap> GetPixelMap()
std::shared_ptr<Media::PixelMap> FetchResult() override
{
return pixelMap_;
}
+1 -12
View File
@@ -90,18 +90,7 @@ std::shared_ptr<Media::PixelMap> AbstractDisplayController::GetScreenSnapshot(Di
std::shared_ptr<ScreenshotCallback> callback = std::make_shared<ScreenshotCallback>();
rsInterface_->TakeSurfaceCapture(displayNode, callback);
int counter = 0;
while (!callback->IsPixelMapOk()) {
usleep(10000); // 10000us equals to 10ms
counter++;
if (counter >= 200) { // wait for 200 * 10ms = 2s
WLOGFE("Failed to get pixelmap, timeout");
return nullptr;
}
}
std::shared_ptr<Media::PixelMap> screenshot = callback->GetPixelMap();
std::shared_ptr<Media::PixelMap> screenshot = callback->GetResult(2000); // wait for 2000ms
if (screenshot == nullptr) {
WLOGFE("Failed to get pixelmap from RS, return nullptr!");
}
+6 -4
View File
@@ -51,8 +51,11 @@ void AbstractScreenController::Init()
std::vector<ScreenId> AbstractScreenController::GetAllScreenIds()
{
std::vector<ScreenId> tmp;
return tmp;
std::vector<ScreenId> res;
for (auto iter = dmsScreenMap_.begin(); iter != dmsScreenMap_.end(); iter++) {
res.push_back(iter->first);
}
return res;
}
sptr<AbstractScreen> AbstractScreenController::GetAbstractScreen(ScreenId dmsScreenId)
@@ -136,13 +139,12 @@ void AbstractScreenController::OnRsScreenChange(ScreenId rsScreenId, ScreenEvent
void AbstractScreenController::ProcessScreenDisconnected(ScreenId rsScreenId)
{
WLOGI("disconnect screen, screenId=%{public}" PRIu64"", rsScreenId);
ScreenId dmsScreenId = INVALID_SCREEN_ID;
auto iter = rs2DmsScreenIdMap_.find(rsScreenId);
if (iter == rs2DmsScreenIdMap_.end()) {
WLOGE("disconnect screen, screenId=%{public}" PRIu64" is not in rs2DmsScreenIdMap_", rsScreenId);
return;
}
dmsScreenId = iter->second;
ScreenId dmsScreenId = iter->second;
auto dmsScreenMapIter = dmsScreenMap_.find(dmsScreenId);
if (dmsScreenMapIter != dmsScreenMap_.end()) {
if (abstractScreenCallback_ != nullptr && CheckScreenInScreenGroup(dmsScreenMapIter->second)) {
+54
View File
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* 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 p ermissions and
* limitations under the License.
*/
#ifndef OHOS_WM_INCLUDE_FUTURE_H
#define OHOS_WM_INCLUDE_FUTURE_H
#include "hilog/log.h"
namespace OHOS::Rosen {
template<class T>
class Future {
constexpr static HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "Future"};
public:
T GetResult(long timeOut)
{
std::unique_lock <std::mutex> lock(mutex_);
if (!conditionVariable_.wait_for(lock, std::chrono::milliseconds(timeOut), [this] { return IsReady(); })) {
OHOS::HiviewDFX::HiLog::Error(LABEL, "wait for %{public}ld, timeout.", timeOut);
}
return FetchResult();
}
protected:
virtual bool IsReady() = 0;
virtual T FetchResult() = 0;
virtual void Call(T) = 0;
void FutureCall(T t)
{
std::unique_lock <std::mutex> lock(mutex_);
Call(t);
conditionVariable_.notify_one();
}
private:
std::condition_variable conditionVariable_;
std::mutex mutex_;
};
} // namespace OHOS::Rosen
#endif // OHOS_WM_INCLUDE_FUTURE_H