1 /*
2 * Copyright (c) 2023 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_vulkan.h"
17
18 #include <vulkan_native_surface_ohos.h>
19 #include <vulkan_window.h>
20 #include <vulkan_proc_table.h>
21 #include <hilog/log.h>
22 #include <display_type.h>
23 #include "window.h"
24
25 namespace OHOS {
26 namespace Rosen {
SurfaceOhosVulkan(const sptr<Surface> & producer)27 SurfaceOhosVulkan::SurfaceOhosVulkan(const sptr<Surface>& producer)
28 : SurfaceOhos(producer), frame_(nullptr)
29 {
30 }
31
~SurfaceOhosVulkan()32 SurfaceOhosVulkan::~SurfaceOhosVulkan()
33 {
34 frame_ = nullptr;
35 if (mNativeWindow_ != nullptr) {
36 DestoryNativeWindow(mNativeWindow_);
37 mNativeWindow_ = nullptr;
38 }
39 if (mVulkanWindow_ != nullptr) {
40 delete mVulkanWindow_;
41 }
42 }
43
RequestFrame(int32_t width,int32_t height)44 std::unique_ptr<SurfaceFrame> SurfaceOhosVulkan::RequestFrame(int32_t width, int32_t height)
45 {
46 if (mNativeWindow_ == nullptr) {
47 mNativeWindow_ = CreateNativeWindowFromSurface(&producer_);
48 if (mNativeWindow_ == nullptr) {
49 LOGE("SurfaceOhosVulkan nativewindow is null");
50 return nullptr;
51 }
52 }
53
54 if (mVulkanWIndow_ == nullptr) {
55 auto vulkan_surface_ohos = std::make_unique<vulkan::VulkanNativeSurfaceOHOS>(mNativeWindow_);
56 mVulkanWIndow_ = new vulkan::VulkanWindow(std::move(vulkan_surface_ohos));
57 }
58
59 sk_sp<SkSurface> skSurface = mVulkanWIndow_->AcquireSurface();
60
61 frame_ = std::make_unique<SurfaceFrameOhosVulkan>(skSurface, width, height);
62 frame_->SetColorSpace(GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB);
63
64 NativeWindowHandleOpt(mNativeWindow_, SET_BUFFER_GEOMETRY, width, height);
65 NativeWindowHandleOpt(mNativeWindow_, SET_COLOR_GAMUT, frame_->GetColorSpace());
66
67 std::unique_ptr<SurfaceFrame> ret(std::move(frame_));
68 return ret;
69 }
70
FlushFrame(std::unique_ptr<SurfaceFrame> & frame)71 bool SurfaceOhosVulkan::FlushFrame(std::unique_ptr<SurfaceFrame>& frame)
72 {
73 if (drawingProxy_ == nullptr) {
74 LOGE("drawingProxy_ is nullptr, can not FlushFrame");
75 return false;
76 }
77 // gpu render flush
78 drawingProxy_->RenderFrame();
79 if (mVulkanWindow_ != nullptr) {
80 mVulkanWindow_->SwapBuffers();
81 } else {
82 LOGE("mVulkanWIndow_ is null");
83 }
84 return true;
85 }
86
GetCanvas(std::unique_ptr<SurfaceFrame> & frame)87 SkCanvas* SurfaceOhosVulkan::GetCanvas(std::unique_ptr<SurfaceFrame>& frame)
88 {
89 if (drawingProxy_ == nullptr) {
90 LOGE("drawingProxy_ is nullptr, can not GetCanvas");
91 return nullptr;
92 }
93 return drawingProxy_->AcquireCanvas(frame);
94 }
95 } // namespace Rosen
96 } // namespace OHOS
97