• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <surface_utils.h>
17 #include <cinttypes>
18 #include "buffer_log.h"
19 
20 namespace OHOS {
21 using namespace HiviewDFX;
22 
GetInstance()23 SurfaceUtils* SurfaceUtils::GetInstance()
24 {
25     static SurfaceUtils instance;
26     return &instance;
27 }
28 
~SurfaceUtils()29 SurfaceUtils::~SurfaceUtils()
30 {
31     surfaceCache_.clear();
32 }
33 
GetSurface(uint64_t uniqueId)34 sptr<Surface> SurfaceUtils::GetSurface(uint64_t uniqueId)
35 {
36     std::lock_guard<std::mutex> lockGuard(mutex_);
37     if (surfaceCache_.count(uniqueId) == 0) {
38         BLOGE("Cannot find surface by uniqueId %" PRIu64 ".", uniqueId);
39         return nullptr;
40     }
41     return surfaceCache_[uniqueId];
42 }
43 
Add(uint64_t uniqueId,const sptr<Surface> & surface)44 SurfaceError SurfaceUtils::Add(uint64_t uniqueId, const sptr<Surface> &surface)
45 {
46     std::lock_guard<std::mutex> lockGuard(mutex_);
47     if (surface == nullptr) {
48         BLOGE(" surface is nullptr.");
49         return GSERROR_INVALID_ARGUMENTS;
50     }
51     if (surfaceCache_.count(uniqueId) == 0) {
52         surfaceCache_[uniqueId] = surface;
53         return GSERROR_OK;
54     }
55     BLOGW("the surface by uniqueId %" PRIu64 " already existed", uniqueId);
56     return GSERROR_OK;
57 }
58 
Remove(uint64_t uniqueId)59 SurfaceError SurfaceUtils::Remove(uint64_t uniqueId)
60 {
61     std::lock_guard<std::mutex> lockGuard(mutex_);
62     if (surfaceCache_.count(uniqueId) == 0) {
63         BLOGE("Delete failed without surface by uniqueId %" PRIu64, uniqueId);
64         return GSERROR_INVALID_OPERATING;
65     }
66     surfaceCache_.erase(uniqueId);
67     return GSERROR_OK;
68 }
69 } // namespace OHOS
70