1// LAF OS Library
2// Copyright (C) 2019-2022 Igara Studio S.A.
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifndef OS_SKIA_SKIA_HELPERS_INCLUDED
8#define OS_SKIA_SKIA_HELPERS_INCLUDED
9#pragma once
10
11#include "gfx/color.h"
12#include "gfx/rect.h"
13#include "os/paint.h"
14#include "os/sampling.h"
15
16#include "include/core/SkColor.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkSamplingOptions.h"
20
21#include <algorithm>
22
23namespace os {
24
25inline SkColor to_skia(gfx::Color c) {
26 return SkColorSetARGB(gfx::geta(c), gfx::getr(c), gfx::getg(c), gfx::getb(c));
27}
28
29inline SkColor4f to_skia4f(gfx::Color c) {
30 return SkColor4f::FromColor(to_skia(c));
31}
32
33inline SkIRect to_skia(const gfx::Rect& rc) {
34 return SkIRect::MakeXYWH(rc.x, rc.y, rc.w, rc.h);
35}
36
37inline SkRect to_skia(const gfx::RectF& rc) {
38 return SkRect::MakeXYWH(SkScalar(rc.x), SkScalar(rc.y), SkScalar(rc.w), SkScalar(rc.h));
39}
40
41inline SkRect to_skia_fix(const gfx::RectF& rc) {
42 return SkRect::MakeXYWH(SkScalar(rc.x), SkScalar(rc.y),
43 SkScalar(std::max(0.0, rc.w-1)),
44 SkScalar(std::max(0.0, rc.h-1)));
45}
46
47inline void to_skia(const Sampling& sampling, SkSamplingOptions& skSampling) {
48 static_assert((int)SkFilterMode::kNearest == (int)Sampling::Filter::Nearest &&
49 (int)SkFilterMode::kLinear == (int)Sampling::Filter::Linear,
50 "Sampling filter modes don't match with Skia");
51
52 static_assert((int)SkMipmapMode::kNone == (int)Sampling::Mipmap::None &&
53 (int)SkMipmapMode::kNearest == (int)Sampling::Mipmap::Nearest &&
54 (int)SkMipmapMode::kLinear == (int)Sampling::Mipmap::Linear,
55 "Sampling mipmap modes don't match with Skia");
56
57 if (sampling.useCubic) {
58 skSampling = SkSamplingOptions({ sampling.cubic.B,
59 sampling.cubic.C });
60 }
61 else {
62 skSampling = SkSamplingOptions((SkFilterMode)sampling.filter,
63 (SkMipmapMode)sampling.mipmap);
64 }
65}
66
67} // namespace os
68
69#endif
70