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 "software_render_backend.h"
17 
18 #include "drawing_utils.h"
19 #include "surface_ohos_raster.h"
20 
21 namespace OHOS {
22 namespace Rosen {
AcquireSkCanvas(std::unique_ptr<SurfaceFrame> & frame)23 SkCanvas* SoftwareRenderBackend::AcquireSkCanvas(std::unique_ptr<SurfaceFrame>& frame)
24 {
25     SurfaceFrameOhosRaster* framePtr = static_cast<SurfaceFrameOhosRaster*>(frame.get());
26     sptr<SurfaceBuffer> buffer = framePtr->GetBuffer();
27     if ((buffer == nullptr) || (buffer->GetWidth() <= 0) || (buffer->GetHeight() <= 0)) {
28         LOGE("Buffer is invalide");
29         return nullptr;
30     }
31 
32     auto addr = static_cast<uint32_t*>(buffer->GetVirAddr());
33     if (addr == nullptr) {
34         LOGE("buffer addr is invalid");
35         return nullptr;
36     }
37     SkImageInfo info =
38         SkImageInfo::Make(buffer->GetWidth(), buffer->GetHeight(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
39     auto uniqueCanvasPtr = SkCanvas::MakeRasterDirect(info, addr, buffer->GetSize() / buffer->GetHeight());
40     skCanvas_ = std::move(uniqueCanvasPtr);
41 
42     LOGD("SoftwareRenderBackend::AcquireCanvas canvas");
43     return skCanvas_.get();
44 }
45 }
46 }