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 20 #include <system/window.h> 21 #include <ui/ANativeObjectBase.h> 22 #include <utils/RefBase.h> 23 24 #include "gui/IGraphicBufferProducer.h" 25 26 namespace android { 27 28 class Surface : public ANativeObjectBase<ANativeWindow, Surface, RefBase> { 29 public: 30 explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false) mBufferProducer(bufferProducer)31 : mBufferProducer(bufferProducer) { 32 ANativeWindow::perform = hook_perform; 33 ANativeWindow::dequeueBuffer = hook_dequeueBuffer; 34 ANativeWindow::query = hook_query; 35 } 36 isValid(const sp<Surface> & surface)37 static bool isValid(const sp<Surface>& surface) { 38 return surface != nullptr; 39 } 40 allocateBuffers()41 void allocateBuffers() {} 42 getNextFrameNumber()43 uint64_t getNextFrameNumber() const { 44 return 0; 45 } 46 setScalingMode(int mode)47 int setScalingMode(int mode) { 48 return 0; 49 } 50 51 virtual int disconnect(int api, 52 IGraphicBufferProducer::DisconnectMode mode = 53 IGraphicBufferProducer::DisconnectMode::Api) { 54 return 0; 55 } 56 lock(ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)57 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) { 58 // TODO: implement this 59 return 0; 60 } 61 unlockAndPost()62 virtual int unlockAndPost() { 63 return 0; 64 } 65 query(int what,int * value)66 virtual int query(int what, int* value) const { 67 return mBufferProducer->query(what, value); 68 } 69 setDequeueTimeout(nsecs_t timeout)70 status_t setDequeueTimeout(nsecs_t timeout) { 71 return OK; 72 } 73 getLastDequeueStartTime()74 nsecs_t getLastDequeueStartTime() const { 75 return 0; 76 } 77 destroy()78 virtual void destroy() {} 79 getBuffersDataSpace()80 int getBuffersDataSpace() { 81 return 0; 82 } 83 84 protected: ~Surface()85 virtual ~Surface() {} 86 hook_perform(ANativeWindow * window,int operation,...)87 static int hook_perform(ANativeWindow* window, int operation, ...) { 88 va_list args; 89 va_start(args, operation); 90 Surface* c = getSelf(window); 91 int result = c->perform(operation, args); 92 va_end(args); 93 return result; 94 } 95 hook_query(const ANativeWindow * window,int what,int * value)96 static int hook_query(const ANativeWindow* window, int what, int* value) { 97 const Surface* c = getSelf(window); 98 return c->query(what, value); 99 } 100 hook_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)101 static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, 102 int* fenceFd) { 103 Surface* c = getSelf(window); 104 return c->dequeueBuffer(buffer, fenceFd); 105 } 106 dequeueBuffer(ANativeWindowBuffer ** buffer,int * fenceFd)107 virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd) { 108 mBufferProducer->requestBuffer(0, &mBuffer); 109 *buffer = mBuffer.get(); 110 return OK; 111 } 112 cancelBuffer(ANativeWindowBuffer * buffer,int fenceFd)113 virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd) { 114 return 0; 115 } 116 queueBuffer(ANativeWindowBuffer * buffer,int fenceFd)117 virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd) { 118 return 0; 119 } 120 perform(int operation,va_list args)121 virtual int perform(int operation, va_list args) { 122 return 0; 123 } 124 setSwapInterval(int interval)125 virtual int setSwapInterval(int interval) { 126 return 0; 127 } 128 setBufferCount(int bufferCount)129 virtual int setBufferCount(int bufferCount) { 130 return 0; 131 } 132 133 private: 134 // can't be copied 135 Surface& operator=(const Surface& rhs); 136 137 Surface(const Surface& rhs); 138 139 const sp<IGraphicBufferProducer> mBufferProducer; 140 sp<GraphicBuffer> mBuffer; 141 }; 142 143 } // namespace android 144 145 #endif // ANDROID_GUI_SURFACE_H 146