1
2 /*
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "compositionengine/mock/NativeWindow.h"
19 #include <log/log.h>
20
21 namespace android::compositionengine::mock {
22
forwardSetSwapInterval(ANativeWindow * window,int interval)23 static int forwardSetSwapInterval(ANativeWindow* window, int interval) {
24 return static_cast<NativeWindow*>(window)->setSwapInterval(interval);
25 }
26
forwardDequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)27 static int forwardDequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, int* fenceFd) {
28 return static_cast<NativeWindow*>(window)->dequeueBuffer(buffer, fenceFd);
29 }
30
forwardCancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)31 static int forwardCancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
32 return static_cast<NativeWindow*>(window)->cancelBuffer(buffer, fenceFd);
33 }
34
forwardQueueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)35 static int forwardQueueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd) {
36 return static_cast<NativeWindow*>(window)->queueBuffer(buffer, fenceFd);
37 }
38
forwardQuery(const ANativeWindow * window,int what,int * value)39 static int forwardQuery(const ANativeWindow* window, int what, int* value) {
40 return static_cast<const NativeWindow*>(window)->query(what, value);
41 }
42
forwardPerform(ANativeWindow * window,int operation,...)43 static int forwardPerform(ANativeWindow* window, int operation, ...) {
44 va_list args;
45 va_start(args, operation);
46 int result = NO_ERROR;
47 switch (operation) {
48 case NATIVE_WINDOW_API_CONNECT: {
49 int api = va_arg(args, int);
50 result = static_cast<NativeWindow*>(window)->connect(api);
51 break;
52 }
53 case NATIVE_WINDOW_SET_BUFFERS_FORMAT: {
54 PixelFormat format = va_arg(args, PixelFormat);
55 result = static_cast<NativeWindow*>(window)->setBuffersFormat(format);
56 break;
57 }
58 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: {
59 ui::Dataspace dataspace = static_cast<ui::Dataspace>(va_arg(args, int));
60 result = static_cast<NativeWindow*>(window)->setBuffersDataSpace(dataspace);
61 break;
62 }
63 case NATIVE_WINDOW_SET_USAGE: {
64 // Note: Intentionally widens usage from 32 to 64 bits so we
65 // just have one implementation.
66 uint64_t usage = va_arg(args, uint32_t);
67 result = static_cast<NativeWindow*>(window)->setUsage(usage);
68 break;
69 }
70 case NATIVE_WINDOW_SET_USAGE64: {
71 uint64_t usage = va_arg(args, uint64_t);
72 result = static_cast<NativeWindow*>(window)->setUsage(usage);
73 break;
74 }
75 case NATIVE_WINDOW_API_DISCONNECT: {
76 int api = va_arg(args, int);
77 result = static_cast<NativeWindow*>(window)->disconnect(api);
78 break;
79 }
80 default:
81 LOG_ALWAYS_FATAL("Unexpected operation %d", operation);
82 break;
83 }
84
85 va_end(args);
86 return result;
87 }
88
forwardDequeueBufferDeprecated(ANativeWindow * window,ANativeWindowBuffer ** buffer)89 static int forwardDequeueBufferDeprecated(ANativeWindow* window, ANativeWindowBuffer** buffer) {
90 int ignoredFenceFd = -1;
91 return static_cast<NativeWindow*>(window)->dequeueBuffer(buffer, &ignoredFenceFd);
92 }
93
forwardCancelBufferDeprecated(ANativeWindow * window,ANativeWindowBuffer * buffer)94 static int forwardCancelBufferDeprecated(ANativeWindow* window, ANativeWindowBuffer* buffer) {
95 return static_cast<NativeWindow*>(window)->cancelBuffer(buffer, -1);
96 }
97
forwardLockBufferDeprecated(ANativeWindow * window,ANativeWindowBuffer * buffer)98 static int forwardLockBufferDeprecated(ANativeWindow* window, ANativeWindowBuffer* buffer) {
99 return static_cast<NativeWindow*>(window)->lockBuffer_DEPRECATED(buffer);
100 }
101
forwardQueueBufferDeprecated(ANativeWindow * window,ANativeWindowBuffer * buffer)102 static int forwardQueueBufferDeprecated(ANativeWindow* window, ANativeWindowBuffer* buffer) {
103 return static_cast<NativeWindow*>(window)->queueBuffer(buffer, -1);
104 }
105
NativeWindow()106 NativeWindow::NativeWindow() {
107 ANativeWindow::setSwapInterval = &forwardSetSwapInterval;
108 ANativeWindow::dequeueBuffer = &forwardDequeueBuffer;
109 ANativeWindow::cancelBuffer = &forwardCancelBuffer;
110 ANativeWindow::queueBuffer = &forwardQueueBuffer;
111 ANativeWindow::query = &forwardQuery;
112 ANativeWindow::perform = &forwardPerform;
113
114 ANativeWindow::dequeueBuffer_DEPRECATED = &forwardDequeueBufferDeprecated;
115 ANativeWindow::cancelBuffer_DEPRECATED = &forwardCancelBufferDeprecated;
116 ANativeWindow::lockBuffer_DEPRECATED = &forwardLockBufferDeprecated;
117 ANativeWindow::queueBuffer_DEPRECATED = &forwardQueueBufferDeprecated;
118 }
119 NativeWindow::~NativeWindow() = default;
120
121 } // namespace android::compositionengine::mock
122