• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 namespace OHOS {
32 struct ImageCacheSeq {
ImageCacheSeqImageCacheSeq33     ImageCacheSeq() : eglImage_(EGL_NO_IMAGE_KHR), eglSync_(EGL_NO_SYNC_KHR) {}
34     EGLImageKHR eglImage_;
35     EGLSyncKHR eglSync_;
36 };
37 
38 static constexpr int64_t TRANSFORM_MATRIX_ELE_COUNT = 16;
39 typedef void (*OnBufferAvailableListener)(void *context);
40 
41 class SurfaceImage : public ConsumerSurface {
42 public:
43     SurfaceImage(uint32_t textureId, uint32_t textureTarget = GL_TEXTURE_EXTERNAL_OES);
44     virtual ~SurfaceImage();
45 
46     void InitSurfaceImage();
47 
GetSurfaceImageName()48     std::string GetSurfaceImageName() const
49     {
50         return surfaceImageName_;
51     }
52 
53     SurfaceError SetDefaultSize(int32_t width, int32_t height);
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 SetOnBufferAvailableListener(void *context, OnBufferAvailableListener listener);
74     SurfaceError UnsetOnBufferAvailableListener();
75     OnBufferAvailableListener listener_ = nullptr;
76     void *context_ = nullptr;
77 
78 private:
79     SurfaceError ValidateEglState();
80     EGLImageKHR CreateEGLImage(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer);
81     SurfaceError UpdateEGLImageAndTexture(EGLDisplay disp, const sptr<SurfaceBuffer>& buffer);
82     void UpdateSurfaceInfo(uint32_t seqNum, sptr<SurfaceBuffer> buffer, const sptr<SyncFence> &acquireFence,
83                            int64_t timestamp, Rect damage);
84 
85     uint32_t textureId_;
86     uint32_t textureTarget_;
87     std::string surfaceImageName_;
88 
89     std::mutex opMutex_;
90     std::atomic<bool> updateSurfaceImage_;
91 
92     EGLDisplay eglDisplay_;
93     EGLContext eglContext_;
94     std::map<uint32_t, ImageCacheSeq> imageCacheSeqs_;
95     uint32_t currentSurfaceImage_;
96     sptr<SurfaceBuffer> currentSurfaceBuffer_;
97     int64_t currentTimeStamp_;
98     Rect currentCrop_ = {};
99     GraphicTransformType currentTransformType_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
100     float currentTransformMatrix_[TRANSFORM_MATRIX_ELE_COUNT] = {0.0};
101 };
102 
103 class SurfaceImageListener : public IBufferConsumerListener {
104 public:
SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage)105     explicit SurfaceImageListener(const sptr<SurfaceImage> & surfaceImage) : surfaceImage_(surfaceImage)
106     {
107         BLOGI("SurfaceImageListener");
108     };
109     virtual ~SurfaceImageListener();
110 
111     virtual void OnBufferAvailable() override;
112 
113 private:
114     wptr<SurfaceImage> surfaceImage_;
115 };
116 } // namespace OHOS
117 #endif // FRAMEWORKS_SURFACE_IMAGE_H
118