• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ::munmap(data_, size_);
39         data_ = nullptr;
40         size_ = 0;
41     }
42 }
43 
NeedReportScene()44 bool FlowbufferAdapterImpl::NeedReportScene()
45 {
46     auto currentTime = std::chrono::system_clock::now().time_since_epoch();
47     timeStamp_ = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime).count();
48     if (timeStamp_ - preTimeStamp_ > PERFORMANCE_PERIOD_MS) {
49         preTimeStamp_ = timeStamp_;
50         return true;
51     }
52     return false;
53 }
54 
StartPerformanceBoost()55 void FlowbufferAdapterImpl::StartPerformanceBoost()
56 {
57     if (!NeedReportScene()) {
58         return;
59     }
60     OHOS::NWeb::ResSchedClientAdapter::ReportScene(
61         OHOS::NWeb::ResSchedStatusAdapter::WEB_SCENE_ENTER, OHOS::NWeb::ResSchedSceneAdapter::KEY_TASK);
62 }
63 
CreateAshmem(size_t size,int mapType,int & fd)64 void* FlowbufferAdapterImpl::CreateAshmem(size_t size, int mapType, int& fd)
65 {
66     fd = AshmemCreate(nullptr, size);
67     if (fd < 0) {
68         WVLOG_E("Create ashmem failed, fd: %{public}d", fd);
69         return nullptr;
70     }
71 
72     int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE);
73     if (result < 0) {
74         close(fd);
75         WVLOG_E("Ashmem set port failed, result: %{public}d", result);
76         return nullptr;
77     }
78 
79     void *startAddr = ::mmap(nullptr, size, mapType, MAP_SHARED, fd, 0);
80     if (startAddr == MAP_FAILED) {
81         close(fd);
82         WVLOG_E("Map ashmem failed");
83         return nullptr;
84     }
85     data_ = startAddr;
86     size_ = size;
87     return startAddr;
88 }
89 
CreateAshmemWithFd(const int fd,size_t size,int mapType)90 void* FlowbufferAdapterImpl::CreateAshmemWithFd(const int fd, size_t size, int mapType)
91 {
92     if (fd < 0) {
93         WVLOG_E("CreateAshmemWithFd failed, fd: %{public}d", fd);
94         return nullptr;
95     }
96 
97     int ashmemSize = AshmemGetSize(fd);
98     if (ashmemSize < 0 || size_t(ashmemSize) < size) {
99         WVLOG_E("CreateAshmemWithFd failed, ashmemSize: %{public}d, size: %{public}zu", ashmemSize, size);
100         return nullptr;
101     }
102 
103     void *startAddr = ::mmap(nullptr, size, mapType, MAP_SHARED, fd, 0);
104     if (startAddr == MAP_FAILED) {
105         close(fd);
106         WVLOG_E("Map ashmem failed");
107         return nullptr;
108     }
109     data_ = startAddr;
110     size_ = size;
111     return startAddr;
112 }
113 } // namespace OHOS::NWeb
114