Home | History | Annotate | Download | only in renderthread
      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 #pragma once
     18 
     19 #include <gui/Surface.h>
     20 #include <utils/Macros.h>
     21 #include <utils/StrongPointer.h>
     22 
     23 #include <memory>
     24 
     25 namespace android::uirenderer::renderthread {
     26 
     27 class ReliableSurface : public ANativeObjectBase<ANativeWindow, ReliableSurface, RefBase> {
     28     PREVENT_COPY_AND_ASSIGN(ReliableSurface);
     29 
     30 public:
     31     ReliableSurface(sp<Surface>&& surface);
     32     ~ReliableSurface();
     33 
     34     void setDequeueTimeout(nsecs_t timeout) { mSurface->setDequeueTimeout(timeout); }
     35 
     36     int reserveNext();
     37 
     38     void allocateBuffers() { mSurface->allocateBuffers(); }
     39 
     40     int query(int what, int* value) const { return mSurface->query(what, value); }
     41 
     42     nsecs_t getLastDequeueStartTime() const { return mSurface->getLastDequeueStartTime(); }
     43 
     44     uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); }
     45 
     46 private:
     47     const sp<Surface> mSurface;
     48 
     49     mutable std::mutex mMutex;
     50 
     51     uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
     52     PixelFormat mFormat = PIXEL_FORMAT_RGBA_8888;
     53     std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{
     54             nullptr, AHardwareBuffer_release};
     55     ANativeWindowBuffer* mReservedBuffer = nullptr;
     56     base::unique_fd mReservedFenceFd;
     57     bool mHasDequeuedBuffer = false;
     58     bool mInErrorState = false;
     59 
     60     bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
     61     ANativeWindowBuffer* acquireFallbackBuffer();
     62     void clearReservedBuffer();
     63 
     64     void perform(int operation, va_list args);
     65     int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
     66     int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
     67     int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
     68 
     69     static Surface* getWrapped(const ANativeWindow*);
     70 
     71     // ANativeWindow hooks
     72     static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
     73     static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer,
     74                                   int* fenceFd);
     75     static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
     76 
     77     static int hook_perform(ANativeWindow* window, int operation, ...);
     78     static int hook_query(const ANativeWindow* window, int what, int* value);
     79     static int hook_setSwapInterval(ANativeWindow* window, int interval);
     80 
     81     static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
     82     static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer** buffer);
     83     static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
     84     static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
     85 };
     86 
     87 };  // namespace android::uirenderer::renderthread