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 "draw/surface.h"
17
18 #include "impl_factory.h"
19 #include "static_factory.h"
20 #include "utils/log.h"
21 #include "utils/system_properties.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
Surface()26 Surface::Surface() : impl_(ImplFactory::CreateSurfaceImpl()), cachedCanvas_(nullptr) {}
27
Bind(const Bitmap & bitmap)28 bool Surface::Bind(const Bitmap& bitmap)
29 {
30 return impl_->Bind(bitmap);
31 }
32
33 #ifdef RS_ENABLE_GPU
Bind(const Image & image)34 bool Surface::Bind(const Image& image)
35 {
36 return impl_->Bind(image);
37 }
38
Bind(const FrameBuffer & frameBuffer)39 bool Surface::Bind(const FrameBuffer& frameBuffer)
40 {
41 return impl_->Bind(frameBuffer);
42 }
43
44 #ifdef RS_ENABLE_VK
MakeFromBackendRenderTarget(GPUContext * gpuContext,const TextureInfo & info,TextureOrigin origin,ColorType colorType,std::shared_ptr<ColorSpace> colorSpace,void (* deleteFunc)(void *),void * cleanupHelper)45 std::shared_ptr<Surface> Surface::MakeFromBackendRenderTarget(GPUContext* gpuContext, const TextureInfo& info,
46 TextureOrigin origin, ColorType colorType, std::shared_ptr<ColorSpace> colorSpace,
47 void (*deleteFunc)(void*), void* cleanupHelper)
48 {
49 if (!SystemProperties::IsUseVulkan()) {
50 return nullptr;
51 }
52 return StaticFactory::MakeFromBackendRenderTarget(gpuContext, info, origin,
53 colorType, colorSpace, deleteFunc, cleanupHelper);
54 }
55 #endif
56
MakeFromBackendTexture(GPUContext * gpuContext,const TextureInfo & info,TextureOrigin origin,int sampleCnt,ColorType colorType,std::shared_ptr<ColorSpace> colorSpace,void (* deleteVkImage)(void *),void * cleanHelper)57 std::shared_ptr<Surface> Surface::MakeFromBackendTexture(GPUContext* gpuContext, const TextureInfo& info,
58 TextureOrigin origin, int sampleCnt, ColorType colorType,
59 std::shared_ptr<ColorSpace> colorSpace, void (*deleteVkImage)(void *), void* cleanHelper)
60 {
61 return StaticFactory::MakeFromBackendTexture(gpuContext, info, origin, sampleCnt, colorType,
62 colorSpace, deleteVkImage, cleanHelper);
63 }
64
MakeRenderTarget(GPUContext * gpuContext,bool budgeted,const ImageInfo & imageInfo)65 std::shared_ptr<Surface> Surface::MakeRenderTarget(GPUContext* gpuContext, bool budgeted, const ImageInfo& imageInfo)
66 {
67 return StaticFactory::MakeRenderTarget(gpuContext, budgeted, imageInfo);
68 }
69 #endif
70
MakeRaster(const ImageInfo & imageInfo)71 std::shared_ptr<Surface> Surface::MakeRaster(const ImageInfo& imageInfo)
72 {
73 return StaticFactory::MakeRaster(imageInfo);
74 }
75
MakeRasterDirect(const ImageInfo & imageInfo,void * pixels,size_t rowBytes)76 std::shared_ptr<Surface> Surface::MakeRasterDirect(const ImageInfo& imageInfo, void* pixels, size_t rowBytes)
77 {
78 return StaticFactory::MakeRasterDirect(imageInfo, pixels, rowBytes);
79 }
80
MakeRasterN32Premul(int32_t width,int32_t height)81 std::shared_ptr<Surface> Surface::MakeRasterN32Premul(int32_t width, int32_t height)
82 {
83 return StaticFactory::MakeRasterN32Premul(width, height);
84 }
85
GetCanvas()86 std::shared_ptr<Canvas> Surface::GetCanvas()
87 {
88 if (cachedCanvas_ == nullptr) {
89 cachedCanvas_ = impl_->GetCanvas();
90 }
91 return cachedCanvas_;
92 }
93
GetImageSnapshot() const94 std::shared_ptr<Image> Surface::GetImageSnapshot() const
95 {
96 return impl_->GetImageSnapshot();
97 }
98
GetBackendTexture(BackendAccess access) const99 BackendTexture Surface::GetBackendTexture(BackendAccess access) const
100 {
101 return impl_->GetBackendTexture(access);
102 }
103
GetImageSnapshot(const RectI & bounds,bool allowRefCache) const104 std::shared_ptr<Image> Surface::GetImageSnapshot(const RectI& bounds, bool allowRefCache) const
105 {
106 return impl_->GetImageSnapshot(bounds, allowRefCache);
107 }
108
MakeSurface(int width,int height) const109 std::shared_ptr<Surface> Surface::MakeSurface(int width, int height) const
110 {
111 return impl_->MakeSurface(width, height);
112 }
113
MakeSurface(const ImageInfo & imageinfo) const114 std::shared_ptr<Surface> Surface::MakeSurface(const ImageInfo& imageinfo) const
115 {
116 return impl_->MakeSurface(imageinfo);
117 }
118
GetImageInfo()119 ImageInfo Surface::GetImageInfo()
120 {
121 std::shared_ptr<Canvas> canvas = GetCanvas();
122 if (!canvas) {
123 LOGD("canvas nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
124 return ImageInfo{};
125 }
126 return canvas->GetImageInfo();
127 }
128
FlushAndSubmit(bool syncCpu)129 void Surface::FlushAndSubmit(bool syncCpu)
130 {
131 impl_->FlushAndSubmit(syncCpu);
132 }
133
Flush(FlushInfo * drawingflushInfo)134 void Surface::Flush(FlushInfo *drawingflushInfo)
135 {
136 if (!impl_) {
137 LOGD("surfaceImpl Flush failed impl nullptr");
138 return;
139 }
140 impl_->Flush(drawingflushInfo);
141 }
142
143 #ifdef RS_ENABLE_GL
Wait(const std::vector<GrGLsync> & syncs)144 void Surface::Wait(const std::vector<GrGLsync>& syncs)
145 {
146 if (!SystemProperties::IsUseGl()) {
147 return;
148 }
149 if (!impl_) {
150 return;
151 }
152 impl_->Wait(syncs);
153 }
154 #endif
155
156 #ifdef RS_ENABLE_VK
Wait(int32_t time,const VkSemaphore & semaphore)157 void Surface::Wait(int32_t time, const VkSemaphore& semaphore)
158 {
159 if (!SystemProperties::IsUseVulkan()) {
160 return;
161 }
162 if (!impl_) {
163 return;
164 }
165 impl_->Wait(time, semaphore);
166 }
167
SetDrawingArea(const std::vector<RectI> & rects)168 void Surface::SetDrawingArea(const std::vector<RectI>& rects)
169 {
170 if (!SystemProperties::IsUseVulkan()) {
171 return;
172 }
173 if (!impl_) {
174 LOGD("surfaceImpl SetDrawingArea failed impl nullptr");
175 return;
176 }
177 impl_->SetDrawingArea(rects);
178 }
179
ClearDrawingArea()180 void Surface::ClearDrawingArea()
181 {
182 if (!SystemProperties::IsUseVulkan()) {
183 return;
184 }
185 if (!impl_) {
186 LOGD("surfaceImpl ClearDrawingArea failed impl nullptr");
187 return;
188 }
189 impl_->ClearDrawingArea();
190 }
191 #endif
192
Width() const193 int Surface::Width() const
194 {
195 return impl_->Width();
196 }
197
Height() const198 int Surface::Height() const
199 {
200 return impl_->Height();
201 }
202
203 } // namespace Drawing
204 } // namespace Rosen
205 } // namespace OHOS
206