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
FlushFrame(std::unique_ptr<SurfaceFrame> & frame)56 bool SurfaceOhosGl::FlushFrame(std::unique_ptr<SurfaceFrame>& frame)
57 {
58 if (drawingProxy_ == nullptr) {
59 LOGE("drawingProxy_ is nullptr, can not FlushFrame");
60 return false;
61 }
62 // gpu render flush
63 drawingProxy_->RenderFrame();
64 drawingProxy_->SwapBuffers();
65 return true;
66 }
67
GetCanvas(std::unique_ptr<SurfaceFrame> & frame)68 SkCanvas* SurfaceOhosGl::GetCanvas(std::unique_ptr<SurfaceFrame>& frame)
69 {
70 if (drawingProxy_ == nullptr) {
71 LOGE("drawingProxy_ is nullptr, can not GetCanvas");
72 return nullptr;
73 }
74 return drawingProxy_->AcquireCanvas(frame);
75 }
76 } // namespace Rosen
77 } // namespace OHOS
78