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 #ifndef ROSEN_SAMPLES_TEXGINE_SKIA_FRAMEWORK_H 17 #define ROSEN_SAMPLES_TEXGINE_SKIA_FRAMEWORK_H 18 19 #include <atomic> 20 #include <functional> 21 #include <memory> 22 #include <mutex> 23 #include <thread> 24 #include <vector> 25 26 #include <include/core/SkMatrix.h> 27 #include <include/core/SkPoint3.h> 28 #include <include/core/SkTypeface.h> 29 30 #include <ui/rs_surface_node.h> 31 32 class SkBitmap; 33 class SkCanvas; 34 class SkFont; 35 class SkPaint; 36 class PointerFilter; 37 38 class SkiaFramework { 39 public: 40 using DrawFunc = std::function<void(SkCanvas &canvas)>; 41 using ResizeFunc = std::function<void(int width, int height)>; 42 43 SkiaFramework(); 44 virtual ~SkiaFramework(); 45 46 // Draw function is nullptr by default. 47 void SetDrawFunc(const DrawFunc &onDraw); 48 49 // Resize function is nullptr by default. 50 void SetResizeFunc(const ResizeFunc &onResize); 51 52 // Allow GPU by default. 53 void SetGPUAllowance(bool allow); 54 55 // Window width is 800 by default. 56 void SetWindowWidth(int width); 57 58 // Window height is 640 by default. 59 void SetWindowHeight(int height); 60 61 // Window scale is 1 by default. 62 void SetWindowScale(double scale); 63 64 // Window title is "skia framework" by default. 65 void SetWindowTitle(const std::string &title); 66 67 int GetWindowWidth() const; 68 int GetWindowHeight() const; 69 double GetWindowScale() const; 70 void Run(); 71 72 static void DrawString(SkCanvas &canvas, const std::string &str, double x, double y); 73 static SkPoint3 MeasureString(const std::string &str); 74 75 virtual void DrawBefore(SkCanvas &canvas); 76 virtual void DrawAfter(SkCanvas &canvas); 77 void DrawColorPicker(SkCanvas &canvas, SkBitmap &bitmap); 78 79 private: 80 friend class PointerFilter; 81 void UIThreadMain(); 82 void UpdateInvertMatrix(); 83 void ProcessBitmap(SkBitmap &bitmap, const OHOS::sptr<OHOS::SurfaceBuffer> buffer); 84 void PrepareVsyncFunc(); 85 void DrawPathAndString(SkCanvas &canvas, SkFont &font, SkPaint &paint1, SkPaint &paint2); 86 87 DrawFunc onDraw_ = nullptr; 88 ResizeFunc onResize_ = nullptr; 89 bool allowGPU_ = true; 90 int windowWidth_ = 800; 91 int windowHeight_ = 640; 92 double windowScale_ = 1; 93 std::string windowTitle_ = "skia framework"; 94 95 void *data_ = nullptr; 96 97 std::mutex propsMutex_; 98 std::atomic<int> x_ = 0; 99 std::atomic<int> y_ = 0; 100 std::atomic<bool> left_ = false; 101 std::atomic<bool> right_ = false; 102 std::atomic<bool> dirty_ = true; 103 std::atomic<int> downRX_ = 0; 104 std::atomic<int> downRY_ = 0; 105 std::atomic<int> diffx_ = 0; 106 std::atomic<int> diffy_ = 0; 107 std::atomic<int> clickx_ = 0; 108 std::atomic<int> clicky_ = 0; 109 std::atomic<int> scalex_ = 0; 110 std::atomic<int> scaley_ = 0; 111 std::atomic<double> scalediff_ = 0; 112 SkMatrix scaleMat_; 113 SkMatrix mat_; 114 SkMatrix invmat_; 115 }; 116 117 #endif // ROSEN_SAMPLES_TEXGINE_SKIA_FRAMEWORK_H 118