blob: 9db0ef1d6e9afa99098b1479aa0aa2b46a2ad356 [file] [log] [blame]
John Reck34781b22017-07-05 16:39:36 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ProfileDataContainer.h"
18
19#include <log/log.h>
20#include <cutils/ashmem.h>
21
Dan Albert9b6a62f2017-12-12 16:08:11 -080022#include <errno.h>
John Reck34781b22017-07-05 16:39:36 -070023#include <sys/mman.h>
24
25namespace android {
26namespace uirenderer {
27
28void ProfileDataContainer::freeData() {
29 if (mIsMapped) {
30 munmap(mData, sizeof(ProfileData));
31 } else {
32 delete mData;
33 }
34 mIsMapped = false;
35 mData = nullptr;
36}
37
38void ProfileDataContainer::rotateStorage() {
39 // If we are mapped we want to stop using the ashmem backend and switch to malloc
40 // We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed
41 // If we aren't sitting on top of ashmem then just do a reset() as it's functionally
42 // equivalent do a free, malloc, reset.
43 if (mIsMapped) {
44 freeData();
45 mData = new ProfileData;
46 }
47 mData->reset();
48}
49
50void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) {
51 int regionSize = ashmem_get_size_region(ashmemfd);
52 if (regionSize < 0) {
53 int err = errno;
54 ALOGW("Failed to get ashmem region size from fd %d, err %d %s", ashmemfd, err, strerror(err));
55 return;
56 }
57 if (regionSize < static_cast<int>(sizeof(ProfileData))) {
58 ALOGW("Ashmem region is too small! Received %d, required %u",
59 regionSize, static_cast<unsigned int>(sizeof(ProfileData)));
60 return;
61 }
62 ProfileData* newData = reinterpret_cast<ProfileData*>(
63 mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE,
64 MAP_SHARED, ashmemfd, 0));
65 if (newData == MAP_FAILED) {
66 int err = errno;
67 ALOGW("Failed to move profile data to ashmem fd %d, error = %d",
68 ashmemfd, err);
69 return;
70 }
71
72 newData->mergeWith(*mData);
73 freeData();
74 mData = newData;
75 mIsMapped = true;
76}
77
78} /* namespace uirenderer */
Dan Albert9b6a62f2017-12-12 16:08:11 -080079} /* namespace android */