• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "skia_picture.h"
17 
18 #ifdef USE_M133_SKIA
19 #include "include/core/SkTypeface.h"
20 #endif
21 #include "skia_data.h"
22 #include "skia_serial_procs.h"
23 #include "utils/data.h"
24 #include "utils/log.h"
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
SkiaPicture()28 SkiaPicture::SkiaPicture() noexcept : skiaPicture_(nullptr) {}
29 
GetPicture() const30 const sk_sp<SkPicture> SkiaPicture::GetPicture() const
31 {
32     return skiaPicture_;
33 }
34 
Serialize() const35 std::shared_ptr<Data> SkiaPicture::Serialize() const
36 {
37     if (skiaPicture_ == nullptr) {
38         LOGD("SkiaPicture::Serialize, SkPicture is nullptr!");
39         return nullptr;
40     }
41 
42     auto skData = skiaPicture_->serialize();
43     auto data = std::make_shared<Data>();
44     data->GetImpl<SkiaData>()->SetSkData(skData);
45 
46     return data;
47 }
48 
Deserialize(std::shared_ptr<Data> data)49 bool SkiaPicture::Deserialize(std::shared_ptr<Data> data)
50 {
51     if (data == nullptr) {
52         return false;
53     }
54 
55     skiaPicture_ = SkPicture::MakeFromData(data->GetData(), data->GetSize());
56     return skiaPicture_ != nullptr;
57 }
58 
SetSkPicture(const sk_sp<SkPicture> & skPicture)59 void SkiaPicture::SetSkPicture(const sk_sp<SkPicture>& skPicture)
60 {
61     skiaPicture_ = skPicture;
62 }
63 
ApproximateOpCount(bool nested)64 int SkiaPicture::ApproximateOpCount(bool nested)
65 {
66     if (skiaPicture_ == nullptr) {
67         return -1;
68     }
69     return skiaPicture_->approximateOpCount(nested);
70 }
71 
Serialize(SerialProcs * proc)72 std::shared_ptr<Data> SkiaPicture::Serialize(SerialProcs* proc)
73 {
74     if (proc == nullptr) {
75         return nullptr;
76     }
77     auto skiaSerialProcs = proc->GetImpl<SkiaSerialProcs>();
78     if (skiaSerialProcs == nullptr) {
79         return nullptr;
80     }
81     auto skProc = skiaSerialProcs->GetSkSerialProcs();
82     if (proc->HasTypefaceProc()) {
83         skProc->fTypefaceProc = [](SkTypeface* tf, void* ctx) {
84             return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
85         };
86     }
87     auto data = std::make_shared<Data>();
88     auto skData = skiaPicture_->serialize(skProc);
89     auto skiaData = data->GetImpl<SkiaData>();
90     if (skiaData == nullptr) {
91         return nullptr;
92     }
93     skiaData->SetSkData(skData);
94     return data;
95 }
96 } // namespace Drawing
97 } // namespace Rosen
98 } // namespace OHOS
99