1/*
2 * Copyright (C) 2018 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 "mock/system/window/MockNativeWindow.h"
18
19namespace android {
20namespace mock {
21namespace {
22
23int dispatch_setSwapInterval(struct ANativeWindow* window, int interval) {
24    return static_cast<NativeWindow*>(window)->setSwapInterval(interval);
25}
26
27int dispatch_dequeueBuffer_DEPRECATED(struct ANativeWindow* window,
28                                      struct ANativeWindowBuffer** buffer) {
29    return static_cast<NativeWindow*>(window)->dequeueBuffer_DEPRECATED(buffer);
30}
31
32int dispatch_lockBuffer_DEPRECATED(struct ANativeWindow* window,
33                                   struct ANativeWindowBuffer* buffer) {
34    return static_cast<NativeWindow*>(window)->lockBuffer_DEPRECATED(buffer);
35}
36
37int dispatch_queueBuffer_DEPRECATED(struct ANativeWindow* window,
38                                    struct ANativeWindowBuffer* buffer) {
39    return static_cast<NativeWindow*>(window)->queueBuffer_DEPRECATED(buffer);
40}
41
42int dispatch_query(const struct ANativeWindow* window, int what, int* value) {
43    return static_cast<const NativeWindow*>(window)->query(what, value);
44}
45
46int dispatch_perform(struct ANativeWindow* window, int operation, ...) {
47    // TODO: Handle the various operations and their varargs better.
48    return static_cast<NativeWindow*>(window)->perform(operation);
49}
50
51int dispatch_cancelBuffer_DEPRECATED(struct ANativeWindow* window,
52                                     struct ANativeWindowBuffer* buffer) {
53    return static_cast<NativeWindow*>(window)->cancelBuffer_DEPRECATED(buffer);
54}
55
56int dispatch_dequeueBuffer(struct ANativeWindow* window, struct ANativeWindowBuffer** buffer,
57                           int* fenceFd) {
58    return static_cast<NativeWindow*>(window)->dequeueBuffer(buffer, fenceFd);
59}
60
61int dispatch_queueBuffer(struct ANativeWindow* window, struct ANativeWindowBuffer* buffer,
62                         int fenceFd) {
63    return static_cast<NativeWindow*>(window)->queueBuffer(buffer, fenceFd);
64}
65
66int dispatch_cancelBuffer(struct ANativeWindow* window, struct ANativeWindowBuffer* buffer,
67                          int fenceFd) {
68    return static_cast<NativeWindow*>(window)->cancelBuffer(buffer, fenceFd);
69}
70
71} // namespace
72
73NativeWindow::NativeWindow() {
74    // ANativeWindow is a structure with function pointers and not a C++ class.
75    // Set all the pointers to dispatch functions, which will invoke the mock
76    // interface functions.
77    ANativeWindow::setSwapInterval = &dispatch_setSwapInterval;
78    ANativeWindow::dequeueBuffer_DEPRECATED = &dispatch_dequeueBuffer_DEPRECATED;
79    ANativeWindow::lockBuffer_DEPRECATED = &dispatch_lockBuffer_DEPRECATED;
80    ANativeWindow::queueBuffer_DEPRECATED = &dispatch_queueBuffer_DEPRECATED;
81    ANativeWindow::query = &dispatch_query;
82    ANativeWindow::perform = &dispatch_perform;
83    ANativeWindow::cancelBuffer_DEPRECATED = &dispatch_cancelBuffer_DEPRECATED;
84    ANativeWindow::dequeueBuffer = &dispatch_dequeueBuffer;
85    ANativeWindow::queueBuffer = &dispatch_queueBuffer;
86    ANativeWindow::cancelBuffer = &dispatch_cancelBuffer;
87}
88
89// Explicit default instantiation is recommended.
90NativeWindow::~NativeWindow() = default;
91
92} // namespace mock
93} // namespace android
94