Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #ifndef ANDROID_GUI_SURFACE_H |
| 18 | #define ANDROID_GUI_SURFACE_H |
| 19 | |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 20 | #include <system/window.h> |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 21 | #include <ui/ANativeObjectBase.h> |
| 22 | #include <utils/RefBase.h> |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 23 | |
| 24 | #include "gui/IGraphicBufferProducer.h" |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class Surface : public ANativeObjectBase<ANativeWindow, Surface, RefBase> { |
| 29 | public: |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 30 | explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false) |
| 31 | : mBufferProducer(bufferProducer) { |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 32 | ANativeWindow::perform = hook_perform; |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 33 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 34 | ANativeWindow::query = hook_query; |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 35 | } |
| 36 | static bool isValid(const sp<Surface>& surface) { return surface != nullptr; } |
| 37 | void allocateBuffers() {} |
| 38 | |
| 39 | uint64_t getNextFrameNumber() const { return 0; } |
| 40 | |
| 41 | int setScalingMode(int mode) { return 0; } |
| 42 | |
| 43 | virtual int disconnect(int api, |
| 44 | IGraphicBufferProducer::DisconnectMode mode = |
| 45 | IGraphicBufferProducer::DisconnectMode::Api) { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) { |
| 50 | // TODO: implement this |
| 51 | return 0; |
| 52 | } |
| 53 | virtual int unlockAndPost() { return 0; } |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 54 | virtual int query(int what, int* value) const { return mBufferProducer->query(what, value); } |
| 55 | |
| 56 | status_t setDequeueTimeout(nsecs_t timeout) { return OK; } |
| 57 | |
| 58 | nsecs_t getLastDequeueStartTime() const { return 0; } |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 59 | |
Jerome Gaillard | 8db3da5 | 2021-09-10 10:46:28 +0100 | [diff] [blame] | 60 | virtual void destroy() {} |
| 61 | |
Jerome Gaillard | da745d2 | 2024-04-16 14:06:37 +0100 | [diff] [blame] | 62 | int getBuffersDataSpace() { return 0; } |
| 63 | |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 64 | protected: |
| 65 | virtual ~Surface() {} |
| 66 | |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 67 | static int hook_perform(ANativeWindow* window, int operation, ...) { |
| 68 | va_list args; |
| 69 | va_start(args, operation); |
| 70 | Surface* c = getSelf(window); |
| 71 | int result = c->perform(operation, args); |
| 72 | va_end(args); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | static int hook_query(const ANativeWindow* window, int what, int* value) { |
| 77 | const Surface* c = getSelf(window); |
| 78 | return c->query(what, value); |
| 79 | } |
| 80 | |
| 81 | static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, |
| 82 | int* fenceFd) { |
| 83 | Surface* c = getSelf(window); |
| 84 | return c->dequeueBuffer(buffer, fenceFd); |
| 85 | } |
| 86 | |
| 87 | virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd) { |
| 88 | mBufferProducer->requestBuffer(0, &mBuffer); |
| 89 | *buffer = mBuffer.get(); |
| 90 | return OK; |
| 91 | } |
| 92 | virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd) { return 0; } |
| 93 | virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd) { return 0; } |
| 94 | virtual int perform(int operation, va_list args) { return 0; } |
| 95 | virtual int setSwapInterval(int interval) { return 0; } |
| 96 | virtual int setBufferCount(int bufferCount) { return 0; } |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 97 | |
| 98 | private: |
| 99 | // can't be copied |
| 100 | Surface& operator=(const Surface& rhs); |
| 101 | Surface(const Surface& rhs); |
Jerome Gaillard | 52f95ff | 2024-04-23 12:58:59 +0100 | [diff] [blame] | 102 | |
| 103 | const sp<IGraphicBufferProducer> mBufferProducer; |
| 104 | sp<GraphicBuffer> mBuffer; |
Fedor Kudasov | a887116 | 2019-07-04 12:54:28 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace android |
| 108 | |
| 109 | #endif // ANDROID_GUI_SURFACE_H |