aedfea1eb08ea0e3dd28dd2c25f27cd5faafdbfa
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / OpenglCodecCommon / ChecksumCalculatorThreadInfo.cpp
1 /*
2 * Copyright (C) 2016 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 "ChecksumCalculatorThreadInfo.h"
18
19 #include "emugl/common/crash_reporter.h"
20 #include "emugl/common/lazy_instance.h"
21 #include "emugl/common/thread_store.h"
22
23 #include <stdio.h>
24 #include <atomic>
25 #include <string>
26
27 namespace {
28
29 class ChecksumCalculatorThreadStore : public ::emugl::ThreadStore {
30 public:
31     ChecksumCalculatorThreadStore() : ::emugl::ThreadStore(NULL) {}
32 };
33
34 #ifdef TRACE_CHECKSUMHELPER
35 std::atomic<size_t> sNumInstances(0);
36 #endif  // TRACE_CHECKSUMHELPER
37
38 }
39
40 static ::emugl::LazyInstance<ChecksumCalculatorThreadStore> s_tls =
41         LAZY_INSTANCE_INIT;
42
43 static ChecksumCalculatorThreadInfo* getChecksumCalculatorThreadInfo() {
44     return static_cast<ChecksumCalculatorThreadInfo*>(s_tls->get());
45 }
46
47 ChecksumCalculatorThreadInfo::ChecksumCalculatorThreadInfo() {
48     LOG_CHECKSUMHELPER(
49         "%s: Checksum thread created (%u instances)\n", __FUNCTION__,
50         (size_t)sNumInstances);
51     s_tls->set(this);
52 }
53
54 ChecksumCalculatorThreadInfo::~ChecksumCalculatorThreadInfo() {
55     LOG_CHECKSUMHELPER(
56         "%s: GLprotocol destroyed (%u instances)\n", __FUNCTION__,
57         (size_t)sNumInstances);
58     s_tls->set(NULL);
59 }
60
61 uint32_t ChecksumCalculatorThreadInfo::getVersion() {
62     return getChecksumCalculatorThreadInfo()->m_protocol.getVersion();
63 }
64
65 bool ChecksumCalculatorThreadInfo::setVersion(uint32_t version) {
66     return getChecksumCalculatorThreadInfo()->m_protocol.setVersion(version);
67 }
68
69 size_t ChecksumCalculatorThreadInfo::checksumByteSize() {
70     return getChecksumCalculatorThreadInfo()->m_protocol.checksumByteSize();
71 }
72
73 bool ChecksumCalculatorThreadInfo::writeChecksum(void* buf,
74                                                  size_t bufLen,
75                                                  void* outputChecksum,
76                                                  size_t outputChecksumLen) {
77     ChecksumCalculator& protocol =
78             getChecksumCalculatorThreadInfo()->m_protocol;
79     protocol.addBuffer(buf, bufLen);
80     return protocol.writeChecksum(outputChecksum, outputChecksumLen);
81 }
82
83 bool ChecksumCalculatorThreadInfo::validate(void* buf,
84                                             size_t bufLen,
85                                             void* checksum,
86                                             size_t checksumLen) {
87     ChecksumCalculator& protocol =
88             getChecksumCalculatorThreadInfo()->m_protocol;
89     protocol.addBuffer(buf, bufLen);
90     return protocol.validate(checksum, checksumLen);
91 }
92
93 void ChecksumCalculatorThreadInfo::validOrDie(void* buf,
94                                               size_t bufLen,
95                                               void* checksum,
96                                               size_t checksumLen,
97                                               const char* message) {
98     // We should actually call crashhandler_die(message), but I don't think we
99     // can link to that library from here
100     if (!validate(buf, bufLen, checksum, checksumLen)) {
101         emugl_crash_reporter(emugl::LogLevel::FATAL, message);
102     }
103 }