diff --git a/wmserver/include/window_snapshot/snapshot_controller.h b/wmserver/include/window_snapshot/snapshot_controller.h index 6516b014..e4c37fb8 100644 --- a/wmserver/include/window_snapshot/snapshot_controller.h +++ b/wmserver/include/window_snapshot/snapshot_controller.h @@ -18,6 +18,7 @@ #include #include +#include "future.h" #include "window_root.h" #include "snapshot_stub.h" #include "window_manager_hilog.h" @@ -41,22 +42,27 @@ private: WMError TakeSnapshot(const std::shared_ptr& surfaceNode, AAFwk::Snapshot& snapshot); - class GetSurfaceCapture : public SurfaceCaptureCallback { + class GetSurfaceCapture : public SurfaceCaptureCallback, public Future> { public: GetSurfaceCapture() = default; ~GetSurfaceCapture() {}; void OnSurfaceCapture(std::shared_ptr pixelmap) override { - if (flag_ == false) { + FutureCall(pixelmap); + } + protected: + void Call(std::shared_ptr pixelmap) override + { + if (!flag_) { pixelMap_ = pixelmap; flag_ = true; } } - bool IsPixelMapOk() + bool IsReady() override { return flag_; } - std::shared_ptr GetPixelMap() + std::shared_ptr FetchResult() override { return pixelMap_; } diff --git a/wmserver/src/window_snapshot/snapshot_controller.cpp b/wmserver/src/window_snapshot/snapshot_controller.cpp index b948533e..82977585 100644 --- a/wmserver/src/window_snapshot/snapshot_controller.cpp +++ b/wmserver/src/window_snapshot/snapshot_controller.cpp @@ -34,16 +34,7 @@ WMError SnapshotController::TakeSnapshot(const std::shared_ptr& s std::shared_ptr callback = std::make_shared(); rsInterface_.TakeSurfaceCapture(surfaceNode, callback, scaleW, scaleH); - 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 WMError::WM_ERROR_NULLPTR; - } - } - std::shared_ptr pixelMap = callback->GetPixelMap(); + std::shared_ptr pixelMap = callback->GetResult(2000); // wait for 2000ms if (pixelMap == nullptr) { WLOGFE("Failed to get pixelmap, return nullptr!");