1 /*
2  * Copyright (c) 2022 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 #include "surface_ohos_gl.h"
17 
18 #include <hilog/log.h>
19 #include "window.h"
20 
21 namespace OHOS {
22 namespace Rosen {
SurfaceOhosGl(const sptr<Surface> & producer)23 SurfaceOhosGl::SurfaceOhosGl(const sptr<Surface>& producer)
24     : SurfaceOhos(producer), frame_(nullptr)
25 {
26 }
27 
~SurfaceOhosGl()28 SurfaceOhosGl::~SurfaceOhosGl()
29 {
30     frame_ = nullptr;
31 }
32 
RequestFrame(int32_t width,int32_t height)33 std::unique_ptr<SurfaceFrame> SurfaceOhosGl::RequestFrame(int32_t width, int32_t height)
34 {
35     if (drawingProxy_ == nullptr) {
36         LOGE("drawingProxy_ is nullptr, can not RequestFrame");
37         return nullptr;
38     }
39     struct NativeWindow* nativeWindow = CreateNativeWindowFromSurface(&producer_);
40     if (nativeWindow == nullptr) {
41         return nullptr;
42     }
43 
44     frame_ = std::make_unique<SurfaceFrameOhosGl>(width, height);
45     frame_->SetColorSpace(GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB);
46     frame_->SetSurface(static_cast<EGLSurface>(drawingProxy_->CreateSurface((EGLNativeWindowType)nativeWindow)));
47     NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, frame_->GetWidth(), frame_->GetHeight());
48     NativeWindowHandleOpt(nativeWindow, SET_COLOR_GAMUT, frame_->GetColorSpace());
49     drawingProxy_->MakeCurrent();
50 
51     std::unique_ptr<SurfaceFrame> ret(std::move(frame_));
52     DestoryNativeWindow(nativeWindow);
53     return ret;
54 }
55 
NativeRequestFrame(int32_t width,int32_t height)56 std::unique_ptr<SurfaceFrame> SurfaceOhosGl::NativeRequestFrame(int32_t width, int32_t height)
57 {
58     return RequestFrame(width, height);
59 }
60 
NativeFlushFrame(std::unique_ptr<SurfaceFrame> & frame)61 bool SurfaceOhosGl::NativeFlushFrame(std::unique_ptr<SurfaceFrame>& frame)
62 {
63     return FlushFrame(frame);
64 }
65 
FlushFrame(std::unique_ptr<SurfaceFrame> & frame)66 bool SurfaceOhosGl::FlushFrame(std::unique_ptr<SurfaceFrame>& frame)
67 {
68     if (drawingProxy_ == nullptr) {
69         LOGE("drawingProxy_ is nullptr, can not FlushFrame");
70         return false;
71     }
72     // gpu render flush
73     drawingProxy_->RenderFrame();
74     drawingProxy_->SwapBuffers();
75     return true;
76 }
77 
GetSkCanvas(std::unique_ptr<SurfaceFrame> & frame)78 SkCanvas* SurfaceOhosGl::GetSkCanvas(std::unique_ptr<SurfaceFrame>& frame)
79 {
80     if (drawingProxy_ == nullptr) {
81         LOGE("drawingProxy_ is nullptr, can not GetSkCanvas");
82         return nullptr;
83     }
84     return drawingProxy_->AcquireSkCanvas(frame);
85 }
86 
GetCanvas(std::unique_ptr<SurfaceFrame> & frame)87 Drawing::Canvas* SurfaceOhosGl::GetCanvas(std::unique_ptr<SurfaceFrame>& frame)
88 {
89     if (drawingProxy_ == nullptr) {
90         LOGE("drawingProxy_ is nullptr, can not GetSkCanvas");
91         return nullptr;
92     }
93     return drawingProxy_->AcquireDrCanvas(frame);
94 }
95 } // namespace Rosen
96 } // namespace OHOS
97