1e9297d28Sopenharmony_ci/* 2e9297d28Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3e9297d28Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e9297d28Sopenharmony_ci * you may not use this file except in compliance with the License. 5e9297d28Sopenharmony_ci * You may obtain a copy of the License at 6e9297d28Sopenharmony_ci * 7e9297d28Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e9297d28Sopenharmony_ci * 9e9297d28Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e9297d28Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e9297d28Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e9297d28Sopenharmony_ci * See the License for the specific language governing permissions and 13e9297d28Sopenharmony_ci * limitations under the License. 14e9297d28Sopenharmony_ci */ 15e9297d28Sopenharmony_ci 16e9297d28Sopenharmony_ci#ifndef FRAMEWORKS_SURFACE_IMAGE_H 17e9297d28Sopenharmony_ci#define FRAMEWORKS_SURFACE_IMAGE_H 18e9297d28Sopenharmony_ci 19e9297d28Sopenharmony_ci#include <atomic> 20e9297d28Sopenharmony_ci#include <mutex> 21e9297d28Sopenharmony_ci#include <array> 22e9297d28Sopenharmony_ci 23e9297d28Sopenharmony_ci#include <EGL/egl.h> 24e9297d28Sopenharmony_ci#include <EGL/eglext.h> 25e9297d28Sopenharmony_ci#include <GLES/gl.h> 26e9297d28Sopenharmony_ci#include <GLES/glext.h> 27e9297d28Sopenharmony_ci#include <GLES3/gl32.h> 28e9297d28Sopenharmony_ci#include "buffer_log.h" 29e9297d28Sopenharmony_ci#include "consumer_surface.h" 30e9297d28Sopenharmony_ci#include "native_window.h" 31e9297d28Sopenharmony_ci 32e9297d28Sopenharmony_cinamespace OHOS { 33e9297d28Sopenharmony_cistruct ImageCacheSeq { 34e9297d28Sopenharmony_ci ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {} 35e9297d28Sopenharmony_ci EGLImageKHR eglImage_; 36e9297d28Sopenharmony_ci EGLSyncKHR eglSync_; 37e9297d28Sopenharmony_ci}; 38e9297d28Sopenharmony_ci 39e9297d28Sopenharmony_cistatic constexpr int64_t TRANSFORM_MATRIX_ELE_COUNT = 16; 40e9297d28Sopenharmony_citypedef void (*OnBufferAvailableListener)(void *context); 41e9297d28Sopenharmony_ci 42e9297d28Sopenharmony_ciclass SurfaceImage : public ConsumerSurface { 43e9297d28Sopenharmony_cipublic: 44e9297d28Sopenharmony_ci SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES); 45e9297d28Sopenharmony_ci SurfaceImage(); 46e9297d28Sopenharmony_ci virtual ~SurfaceImage(); 47e9297d28Sopenharmony_ci 48e9297d28Sopenharmony_ci void InitSurfaceImage(); 49e9297d28Sopenharmony_ci 50e9297d28Sopenharmony_ci std::string GetSurfaceImageName() const 51e9297d28Sopenharmony_ci { 52e9297d28Sopenharmony_ci return surfaceImageName_; 53e9297d28Sopenharmony_ci } 54e9297d28Sopenharmony_ci 55e9297d28Sopenharmony_ci SurfaceError UpdateSurfaceImage(); 56e9297d28Sopenharmony_ci int64_t GetTimeStamp(); 57e9297d28Sopenharmony_ci 58e9297d28Sopenharmony_ci // update buffer available state, updateSurfaceImage_ and a private mutex 59e9297d28Sopenharmony_ci void OnUpdateBufferAvailableState(bool updated) 60e9297d28Sopenharmony_ci { 61e9297d28Sopenharmony_ci updateSurfaceImage_ = updated; 62e9297d28Sopenharmony_ci } 63e9297d28Sopenharmony_ci 64e9297d28Sopenharmony_ci bool GetBufferAvailableState() 65e9297d28Sopenharmony_ci { 66e9297d28Sopenharmony_ci return updateSurfaceImage_; 67e9297d28Sopenharmony_ci } 68e9297d28Sopenharmony_ci 69e9297d28Sopenharmony_ci SurfaceError AttachContext(uint32_t textureId); 70e9297d28Sopenharmony_ci SurfaceError DetachContext(); 71e9297d28Sopenharmony_ci 72e9297d28Sopenharmony_ci SurfaceError GetTransformMatrix(float matrix[16]); 73e9297d28Sopenharmony_ci SurfaceError GetTransformMatrixV2(float matrix[16]); 74e9297d28Sopenharmony_ci SurfaceError SetOnBufferAvailableListener(void *context, OnBufferAvailableListener listener); 75e9297d28Sopenharmony_ci SurfaceError UnsetOnBufferAvailableListener(); 76e9297d28Sopenharmony_ci OnBufferAvailableListener listener_ = nullptr; 77e9297d28Sopenharmony_ci void *context_ = nullptr; 78e9297d28Sopenharmony_ci 79e9297d28Sopenharmony_ci SurfaceError AcquireNativeWindowBuffer(OHNativeWindowBuffer** nativeWindowBuffer, int32_t* fenceFd); 80e9297d28Sopenharmony_ci SurfaceError ReleaseNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer, int32_t fenceFd); 81e9297d28Sopenharmony_ci 82e9297d28Sopenharmony_ci SurfaceError SetDefaultUsage(uint64_t usage); 83e9297d28Sopenharmony_ci SurfaceError SetDefaultSize(int32_t width, int32_t height); 84e9297d28Sopenharmony_ciprivate: 85e9297d28Sopenharmony_ci SurfaceError ValidateEglState(); 86e9297d28Sopenharmony_ci EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 87e9297d28Sopenharmony_ci SurfaceError UpdateEGLImageAndTexture(const sptr<SurfaceBuffer>& buffer); 88e9297d28Sopenharmony_ci void UpdateSurfaceInfo(uint32_t seqNum, sptr<SurfaceBuffer> buffer, const sptr<SyncFence> &acquireFence, 89e9297d28Sopenharmony_ci int64_t timestamp, Rect damage); 90e9297d28Sopenharmony_ci void CheckImageCacheNeedClean(uint32_t seqNum); 91e9297d28Sopenharmony_ci void DestroyEGLImage(EGLImageKHR &eglImage); 92e9297d28Sopenharmony_ci void DestroyEGLSync(EGLSyncKHR &eglSync); 93e9297d28Sopenharmony_ci void NewBufferDestroyEGLImage(bool isNewBuffer, uint32_t seqNum); 94e9297d28Sopenharmony_ci void DestroyEGLImageBySeq(uint32_t seqNum); 95e9297d28Sopenharmony_ci 96e9297d28Sopenharmony_ci uint32_t textureId_ = 0; 97e9297d28Sopenharmony_ci uint32_t textureTarget_ = GL_TEXTURE_EXTERNAL_OES; 98e9297d28Sopenharmony_ci std::string surfaceImageName_; 99e9297d28Sopenharmony_ci 100e9297d28Sopenharmony_ci std::mutex opMutex_; 101e9297d28Sopenharmony_ci std::atomic<bool> updateSurfaceImage_; 102e9297d28Sopenharmony_ci 103e9297d28Sopenharmony_ci EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 104e9297d28Sopenharmony_ci EGLContext eglContext_ = EGL_NO_CONTEXT; 105e9297d28Sopenharmony_ci std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_; 106e9297d28Sopenharmony_ci uint32_t currentSurfaceImage_ = UINT_MAX; 107e9297d28Sopenharmony_ci sptr<SurfaceBuffer> currentSurfaceBuffer_; 108e9297d28Sopenharmony_ci int64_t currentTimeStamp_; 109e9297d28Sopenharmony_ci Rect currentCrop_ = {}; 110e9297d28Sopenharmony_ci GraphicTransformType currentTransformType_ = GraphicTransformType::GRAPHIC_ROTATE_NONE; 111e9297d28Sopenharmony_ci float currentTransformMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 112e9297d28Sopenharmony_ci float currentTransformMatrixV2_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 113e9297d28Sopenharmony_ci uint64_t uniqueId_ = 0; 114e9297d28Sopenharmony_ci}; 115e9297d28Sopenharmony_ci 116e9297d28Sopenharmony_ciclass SurfaceImageListener : public IBufferConsumerListener { 117e9297d28Sopenharmony_cipublic: 118e9297d28Sopenharmony_ci explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 119e9297d28Sopenharmony_ci { 120e9297d28Sopenharmony_ci BLOGI("SurfaceImageListener"); 121e9297d28Sopenharmony_ci }; 122e9297d28Sopenharmony_ci virtual ~SurfaceImageListener(); 123e9297d28Sopenharmony_ci 124e9297d28Sopenharmony_ci virtual void OnBufferAvailable() override; 125e9297d28Sopenharmony_ci 126e9297d28Sopenharmony_ciprivate: 127e9297d28Sopenharmony_ci wptr<SurfaceImage> surfaceImage_; 128e9297d28Sopenharmony_ci}; 129e9297d28Sopenharmony_ci} // namespace OHOS 130e9297d28Sopenharmony_ci#endif // FRAMEWORKS_SURFACE_IMAGE_H 131