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 #ifndef SK_IMAGE_CHAIN_H
17 #define SK_IMAGE_CHAIN_H
18 
19 #include <memory>
20 #include <vector>
21 #include <unistd.h>
22 #include "ec_log.h"
23 #include "image_source.h"
24 #include "include/core/SkRect.h"
25 #include "include/core/SkPath.h"
26 #include "include/core/SkRRect.h"
27 #include "include/core/SkColor.h"
28 #include "include/core/SkCanvas.h"
29 #include "include/core/SkPixmap.h"
30 #include "include/core/SkImageFilter.h"
31 #include "include/core/SkImageInfo.h"
32 #include "include/core/SkBitmap.h"
33 #include "include/core/SkPaint.h"
34 #include "include/core/SkSurface.h"
35 #include "include/effects/SkImageFilters.h"
36 
37 
38 namespace OHOS {
39 namespace Rosen {
40 
41 enum class DrawError {
42     ERR_OK = 0,
43     ERR_CPU_CANVAS,
44     ERR_GPU_CANVAS,
45     ERR_CANVAS_NULL,
46     ERR_IMAGE_NULL,
47     ERR_PIXEL_READ
48 };
49 
50 class SKImageChain {
51 public:
52     SKImageChain(SkCanvas* canvas, sk_sp<SkImage> image);
53     SKImageChain(std::shared_ptr<Media::PixelMap> srcPxielMap);
54     virtual ~SKImageChain();
55     DrawError Render(const std::vector<sk_sp<SkImageFilter>> &skFilters, const bool &forceCPU,
56         std::shared_ptr<Media::PixelMap> &dstPixelMap);
57 public:
58     DrawError Draw();
59     void ForceCPU(bool forceCPU);
60     void SetFilters(sk_sp<SkImageFilter> filter);
61     void SetClipRect(SkRect* rect);
62     void SetClipPath(SkPath* path);
63     void SetClipRRect(SkRRect* rRect);
64     std::shared_ptr<Media::PixelMap> GetPixelMap();
65 
66 private:
67     bool InitializeCanvas();
68     DrawError CheckForErrors();
69     void SetupPaint(SkPaint& paint);
70     void ApplyClipping();
71     bool DrawImage(SkPaint& paint);
72     DrawError InitWithoutCanvas();
73     bool CreateCPUCanvas();
74     bool CreateGPUCanvas();
75     SkColorType PixelFormatConvert(const Media::PixelFormat& pixelFormat);
76     SkImageInfo imageInfo_ = {};
77     bool forceCPU_ = true;
78     SkCanvas* canvas_ = nullptr;
79     sk_sp<SkImage> image_ = nullptr;
80     SkRect* rect_ = nullptr;
81     SkPath* path_ = nullptr;
82     SkRRect* rRect_ = nullptr;
83     std::shared_ptr<SkPixmap> dstPixmap_ = nullptr;
84     std::shared_ptr<Media::PixelMap> srcPixelMap_ = nullptr;
85     std::shared_ptr<Media::PixelMap> dstPixelMap_ = nullptr;
86     sk_sp<SkImageFilter> filters_ = nullptr;
87     sk_sp<SkSurface> gpuSurface_ = nullptr;
88     sk_sp<SkSurface> cpuSurface_ = nullptr;
89 };
90 } // namespace Rosen
91 } // namespace OHOS
92 #endif // IMAGE_CHAIN_H
93