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 
19 namespace android {
20 namespace mock {
21 namespace {
22 
dispatch_setSwapInterval(struct ANativeWindow * window,int interval)23 int dispatch_setSwapInterval(struct ANativeWindow* window, int interval) {
24     return static_cast<NativeWindow*>(window)->setSwapInterval(interval);
25 }
26 
dispatch_dequeueBuffer_DEPRECATED(struct ANativeWindow * window,struct ANativeWindowBuffer ** buffer)27 int dispatch_dequeueBuffer_DEPRECATED(struct ANativeWindow* window,
28                                       struct ANativeWindowBuffer** buffer) {
29     return static_cast<NativeWindow*>(window)->dequeueBuffer_DEPRECATED(buffer);
30 }
31 
dispatch_lockBuffer_DEPRECATED(struct ANativeWindow * window,struct ANativeWindowBuffer * buffer)32 int dispatch_lockBuffer_DEPRECATED(struct ANativeWindow* window,
33                                    struct ANativeWindowBuffer* buffer) {
34     return static_cast<NativeWindow*>(window)->lockBuffer_DEPRECATED(buffer);
35 }
36 
dispatch_queueBuffer_DEPRECATED(struct ANativeWindow * window,struct ANativeWindowBuffer * buffer)37 int dispatch_queueBuffer_DEPRECATED(struct ANativeWindow* window,
38                                     struct ANativeWindowBuffer* buffer) {
39     return static_cast<NativeWindow*>(window)->queueBuffer_DEPRECATED(buffer);
40 }
41 
dispatch_query(const struct ANativeWindow * window,int what,int * value)42 int dispatch_query(const struct ANativeWindow* window, int what, int* value) {
43     return static_cast<const NativeWindow*>(window)->query(what, value);
44 }
45 
dispatch_perform(struct ANativeWindow * window,int operation,...)46 int 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 
dispatch_cancelBuffer_DEPRECATED(struct ANativeWindow * window,struct ANativeWindowBuffer * buffer)51 int dispatch_cancelBuffer_DEPRECATED(struct ANativeWindow* window,
52                                      struct ANativeWindowBuffer* buffer) {
53     return static_cast<NativeWindow*>(window)->cancelBuffer_DEPRECATED(buffer);
54 }
55 
dispatch_dequeueBuffer(struct ANativeWindow * window,struct ANativeWindowBuffer ** buffer,int * fenceFd)56 int dispatch_dequeueBuffer(struct ANativeWindow* window, struct ANativeWindowBuffer** buffer,
57                            int* fenceFd) {
58     return static_cast<NativeWindow*>(window)->dequeueBuffer(buffer, fenceFd);
59 }
60 
dispatch_queueBuffer(struct ANativeWindow * window,struct ANativeWindowBuffer * buffer,int fenceFd)61 int dispatch_queueBuffer(struct ANativeWindow* window, struct ANativeWindowBuffer* buffer,
62                          int fenceFd) {
63     return static_cast<NativeWindow*>(window)->queueBuffer(buffer, fenceFd);
64 }
65 
dispatch_cancelBuffer(struct ANativeWindow * window,struct ANativeWindowBuffer * buffer,int fenceFd)66 int 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 
NativeWindow()73 NativeWindow::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.
90 NativeWindow::~NativeWindow() = default;
91 
92 } // namespace mock
93 } // namespace android
94