1 /*
2  * Copyright (c) 2024 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 "flowbuffer_adapter_impl.h"
17 
18 #include <cstring>
19 #include <sys/mman.h>
20 
21 #include "ashmem.h"
22 #include "nweb_log.h"
23 #include "ohos_adapter_helper.h"
24 #include "res_sched_client_adapter.h"
25 #include "securec.h"
26 
27 namespace OHOS::NWeb {
28 namespace {
29     const int64_t PERFORMANCE_PERIOD_MS = 300;
30 }
31 
32 int64_t FlowbufferAdapterImpl::timeStamp_ = 0;
33 int64_t FlowbufferAdapterImpl::preTimeStamp_ = 0;
34 
~FlowbufferAdapterImpl()35 FlowbufferAdapterImpl::~FlowbufferAdapterImpl()
36 {
37     if (data_ != nullptr) {
38         if (::munmap(data_, size_) != 0) {
39             WVLOG_E("Failed to unmap ashmem region");
40         }
41         data_ = nullptr;
42         size_ = 0;
43     }
44 }
45 
NeedReportScene()46 bool FlowbufferAdapterImpl::NeedReportScene()
47 {
48     auto currentTime = std::chrono::system_clock::now().time_since_epoch();
49     timeStamp_ = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime).count();
50     if (timeStamp_ - preTimeStamp_ > PERFORMANCE_PERIOD_MS) {
51         preTimeStamp_ = timeStamp_;
52         return true;
53     }
54     return false;
55 }
56 
StartPerformanceBoost()57 void FlowbufferAdapterImpl::StartPerformanceBoost()
58 {
59     if (!NeedReportScene()) {
60         return;
61     }
62     OHOS::NWeb::ResSchedClientAdapter::ReportScene(
63         OHOS::NWeb::ResSchedStatusAdapter::WEB_SCENE_ENTER, OHOS::NWeb::ResSchedSceneAdapter::KEY_TASK);
64 }
65 
CreateAshmem(size_t size,int mapType,int & fd)66 void* FlowbufferAdapterImpl::CreateAshmem(size_t size, int mapType, int& fd)
67 {
68     fd = AshmemCreate(nullptr, size);
69     if (fd < 0) {
70         WVLOG_E("Create ashmem failed, fd: %{public}d", fd);
71         return nullptr;
72     }
73 
74     int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE);
75     if (result < 0) {
76         close(fd);
77         fd = -1;
78         WVLOG_E("Ashmem set port failed, result: %{public}d", result);
79         return nullptr;
80     }
81 
82     void *startAddr = ::mmap(nullptr, size, mapType, MAP_SHARED, fd, 0);
83     if (startAddr == MAP_FAILED) {
84         close(fd);
85         fd = -1;
86         WVLOG_E("Map ashmem failed");
87         return nullptr;
88     }
89     data_ = startAddr;
90     size_ = size;
91     return startAddr;
92 }
93 
CreateAshmemWithFd(const int fd,size_t size,int mapType)94 void* FlowbufferAdapterImpl::CreateAshmemWithFd(const int fd, size_t size, int mapType)
95 {
96     if (fd < 0) {
97         WVLOG_E("CreateAshmemWithFd failed, fd: %{public}d", fd);
98         return nullptr;
99     }
100 
101     int ashmemSize = AshmemGetSize(fd);
102     if (ashmemSize < 0 || size_t(ashmemSize) < size) {
103         WVLOG_E("CreateAshmemWithFd failed, ashmemSize: %{public}d, size: %{public}zu", ashmemSize, size);
104         return nullptr;
105     }
106 
107     void *startAddr = ::mmap(nullptr, size, mapType, MAP_SHARED, fd, 0);
108     if (startAddr == MAP_FAILED) {
109         close(fd);
110         WVLOG_E("Map ashmem failed");
111         return nullptr;
112     }
113     data_ = startAddr;
114     size_ = size;
115     return startAddr;
116 }
117 } // namespace OHOS::NWeb
118