* Copyright (c) 2022-2024 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 permissions and
* limitations under the License.
*/
#ifndef GPU_IPC_COMMON_GPU_SURFACE_ID_LOOKUP_H_
#define GPU_IPC_COMMON_GPU_SURFACE_ID_LOOKUP_H_
#include <string>
#include <map>
#include "gpu/gpu_export.h"
#include "base/memory/singleton.h"
#include "base/synchronization/lock.h"
#include "gpu/ipc/common/surface_handle.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "ui/gfx/native_widget_types.h"
namespace gpu {
class GPU_EXPORT GpuSurfaceIdTracker {
public:
struct SurfaceRecord {
SurfaceRecord(gfx::AcceleratedWidget widget,
std::string& native_image_surface_id);
SurfaceRecord(SurfaceRecord&&);
SurfaceRecord(const SurfaceRecord&) = delete;
gfx::AcceleratedWidget widget;
std::string native_image_surface_id;
};
GpuSurfaceIdTracker(const GpuSurfaceIdTracker&) = delete;
GpuSurfaceIdTracker& operator=(const GpuSurfaceIdTracker&) = delete;
GpuSurfaceIdTracker();
virtual ~GpuSurfaceIdTracker() {}
static GpuSurfaceIdTracker* Get() { return GetInstance(); }
std::string AcquireNativeImageSurfaceId(
gpu::SurfaceHandle surface_handle);
int AddSurfaceForNativeWidget(SurfaceRecord record);
bool IsValidSurfaceHandle(gpu::SurfaceHandle surface_handle) const;
void RemoveSurface(gpu::SurfaceHandle surface_handle);
std::size_t GetSurfaceCount();
static GpuSurfaceIdTracker* GetInstance();
private:
using SurfaceMap = std::map<gpu::SurfaceHandle, SurfaceRecord>;
friend struct base::DefaultSingletonTraits<GpuSurfaceIdTracker>;
mutable base::Lock surface_map_lock_;
SurfaceMap surface_map_;
int next_surface_handle_;
};
}
#endif