1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FRAMEWORKS_SURFACE_IMAGE_H 17 #define FRAMEWORKS_SURFACE_IMAGE_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <array> 22 23 #include <EGL/egl.h> 24 #include <EGL/eglext.h> 25 #include <GLES/gl.h> 26 #include <GLES/glext.h> 27 #include <GLES3/gl32.h> 28 #include "buffer_log.h" 29 #include "consumer_surface.h" 30 #include "native_window.h" 31 32 namespace OHOS { 33 struct ImageCacheSeq { ImageCacheSeqImageCacheSeq34 ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {} 35 EGLImageKHR eglImage_; 36 EGLSyncKHR eglSync_; 37 }; 38 39 static constexpr uint32_t TRANSFORM_MATRIX_ELE_COUNT = 16; 40 typedef void (*OnBufferAvailableListener)(void *context); 41 42 class SurfaceImage : public ConsumerSurface { 43 public: 44 SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES); 45 SurfaceImage(); 46 virtual ~SurfaceImage(); 47 48 void InitSurfaceImage(); 49 GetSurfaceImageName()50 std::string GetSurfaceImageName() const 51 { 52 return surfaceImageName_; 53 } 54 55 SurfaceError UpdateSurfaceImage(); 56 int64_t GetTimeStamp(); 57 58 // update buffer available state, updateSurfaceImage_ and a private mutex OnUpdateBufferAvailableState(bool updated)59 void OnUpdateBufferAvailableState(bool updated) 60 { 61 updateSurfaceImage_ = updated; 62 } 63 GetBufferAvailableState()64 bool GetBufferAvailableState() 65 { 66 return updateSurfaceImage_; 67 } 68 69 SurfaceError AttachContext(uint32_t textureId); 70 SurfaceError DetachContext(); 71 72 SurfaceError GetTransformMatrix(float matrix[16]); 73 SurfaceError GetTransformMatrixV2(float matrix[16]); 74 SurfaceError GetBufferMatrix(float matrix[16]); 75 SurfaceError SetOnBufferAvailableListener(void *context, OnBufferAvailableListener listener); 76 SurfaceError UnsetOnBufferAvailableListener(); 77 OnBufferAvailableListener listener_ = nullptr; 78 void *context_ = nullptr; 79 80 SurfaceError AcquireNativeWindowBuffer(OHNativeWindowBuffer** nativeWindowBuffer, int32_t* fenceFd); 81 SurfaceError ReleaseNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer, int32_t fenceFd); 82 83 SurfaceError SetDefaultUsage(uint64_t usage); 84 SurfaceError SetDefaultSize(int32_t width, int32_t height); 85 SurfaceError SetDropBufferSwitch(bool isOpen); 86 private: 87 void UpdateBasicInfo(const sptr<SurfaceBuffer>& buffer, int64_t timestamp); 88 Rect GetBufferCropRegion(const sptr<OHOS::SurfaceBuffer>& buffer); 89 SurfaceError ValidateEglState(); 90 EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer); 91 SurfaceError UpdateEGLImageAndTexture(const sptr<SurfaceBuffer>& buffer); 92 void UpdateSurfaceInfo(sptr<SurfaceBuffer> buffer, const sptr<SyncFence> &acquireFence, 93 int64_t timestamp, const Rect& damage); 94 void CheckImageCacheNeedClean(uint32_t seqNum); 95 void DestroyEGLImage(EGLImageKHR &eglImage); 96 void DestroyEGLSync(EGLSyncKHR &eglSync); 97 void NewBufferDestroyEGLImage(bool isNewBuffer, uint32_t seqNum); 98 void DestroyEGLImageBySeq(uint32_t seqNum); 99 100 uint32_t textureId_ = 0; 101 uint32_t textureTarget_ = GL_TEXTURE_EXTERNAL_OES; 102 std::string surfaceImageName_; 103 104 std::mutex opMutex_; 105 std::atomic<bool> updateSurfaceImage_; 106 107 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 108 EGLContext eglContext_ = EGL_NO_CONTEXT; 109 std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_; 110 uint32_t currentSurfaceImage_ = UINT_MAX; 111 sptr<SurfaceBuffer> currentSurfaceBuffer_; 112 int64_t currentTimeStamp_; 113 float currentTransformMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 114 float currentTransformMatrixV2_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 115 float currentBufferMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0}; 116 uint64_t uniqueId_ = 0; 117 bool dropFrameMode_ = false; 118 119 /** 120 * @brief Represents the properties of a graphics buffer frame 121 * 122 * This structure encapsulates essential information about a graphics buffer frame, 123 * including its effective region, dimensions, and transformation settings. It is used 124 * to track and compare buffer states for efficient graphics rendering and processing. 125 */ 126 struct BufferProperties { 127 /** 128 * @brief Defines the valid/effective region within the buffer 129 * Represents the actual usable area of the buffer that contains valid content, 130 * which may be smaller than the full buffer dimensions 131 */ 132 Rect crop = {0, 0, 0, 0}; 133 134 /** 135 * @brief Rotation/transformation type applied to the buffer 136 * Specifies how the buffer content should be transformed during rendering 137 */ 138 GraphicTransformType transformType = GraphicTransformType::GRAPHIC_ROTATE_NONE; 139 140 /** 141 * @brief Width of the buffer in pixels 142 * Represents the horizontal dimension of the graphics buffer 143 */ 144 uint32_t bufferWidth = 0; 145 146 /** 147 * @brief Height of the buffer in pixels 148 * Represents the vertical dimension of the graphics buffer 149 */ 150 uint32_t bufferHeight = 0; 151 152 /** 153 * @brief Equality comparison operator 154 * @param other The BufferProperties object to compare with 155 * @return true if all properties match, false otherwise 156 */ 157 bool operator==(const BufferProperties& other) const 158 { 159 return crop == other.crop && 160 transformType == other.transformType && 161 bufferWidth == other.bufferWidth && 162 bufferHeight == other.bufferHeight; 163 } 164 165 /** 166 * @brief Inequality comparison operator 167 * @param other The BufferProperties object to compare with 168 * @return true if any property differs, false if all match 169 */ 170 bool operator!=(const BufferProperties& other) const 171 { 172 return !(*this == other); 173 } 174 }; 175 BufferProperties bufferProperties_; 176 BufferProperties preBufferProperties_; 177 }; 178 179 class SurfaceImageListener : public IBufferConsumerListener { 180 public: SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)181 explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage) 182 { 183 BLOGI("SurfaceImageListener"); 184 }; 185 virtual ~SurfaceImageListener(); 186 187 virtual void OnBufferAvailable() override; 188 189 private: 190 wptr<SurfaceImage> surfaceImage_; 191 }; 192 } // namespace OHOS 193 #endif // FRAMEWORKS_SURFACE_IMAGE_H 194