blob: a31a91c352686bd437cb31675ab61fe774ca6353 [file] [log] [blame]
John Reck013127b2020-10-29 20:53:51 -04001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <SkAndroidFrameworkUtils.h>
20#include <SkCanvas.h>
21#include <SkPath.h>
22#include <log/log.h>
23
24#include "CanvasOpTypes.h"
25
26#include <experimental/type_traits>
27
28namespace android::uirenderer {
29
30template <CanvasOpType T>
31struct CanvasOp;
32
33struct CanvasOpTraits {
34 CanvasOpTraits() = delete;
35
36 template<class T>
37 using draw_t = decltype(std::integral_constant<void (T::*)(SkCanvas*) const, &T::draw>{});
38
39 template <class T>
40 static constexpr bool can_draw = std::experimental::is_detected_v<draw_t, T>;
41};
42
43#define ASSERT_DRAWABLE() private: constexpr void _check_drawable() \
44 { static_assert(CanvasOpTraits::can_draw<std::decay_t<decltype(*this)>>); }
45
46// ----------------------------------------------
47// State Ops
48// ---------------------------------------------
49
50template <>
51struct CanvasOp<CanvasOpType::Save> {
52 void draw(SkCanvas* canvas) const { canvas->save(); }
53 ASSERT_DRAWABLE()
54};
55
56template <>
57struct CanvasOp<CanvasOpType::SaveLayer> {
58 SkCanvas::SaveLayerRec saveLayerRec;
59 void draw(SkCanvas* canvas) const { canvas->saveLayer(saveLayerRec); }
60 ASSERT_DRAWABLE()
61};
62
63template <>
64struct CanvasOp<CanvasOpType::SaveBehind> {
65 SkRect bounds;
66 void draw(SkCanvas* canvas) const { SkAndroidFrameworkUtils::SaveBehind(canvas, &bounds); }
67 ASSERT_DRAWABLE()
68};
69
70template <>
71struct CanvasOp<CanvasOpType::Restore> {
72 void draw(SkCanvas* canvas) const { canvas->restore(); }
73 ASSERT_DRAWABLE()
74};
75
76template <>
77struct CanvasOp<CanvasOpType::BeginZ> {
78};
79template <>
80struct CanvasOp<CanvasOpType::EndZ> {};
81
82// ----------------------------------------------
83// Clip Ops
84// ---------------------------------------------
85
86template <>
87struct CanvasOp<CanvasOpType::ClipRect> {
88 SkRect rect;
89 SkClipOp clipOp;
90 void draw(SkCanvas* canvas) const { canvas->clipRect(rect, clipOp); }
91 ASSERT_DRAWABLE()
92};
93
94template <>
95struct CanvasOp<CanvasOpType::ClipPath> {
96 SkPath path;
97 SkClipOp op;
98 void draw(SkCanvas* canvas) const { canvas->clipPath(path, op, true); }
99 ASSERT_DRAWABLE()
100};
101
102// ----------------------------------------------
103// Drawing Ops
104// ---------------------------------------------
105
106template <>
107struct CanvasOp<CanvasOpType::DrawColor> {
108 SkColor4f color;
109 SkBlendMode mode;
110 void draw(SkCanvas* canvas) const { canvas->drawColor(color, mode); }
111 ASSERT_DRAWABLE()
112};
113
114template <>
115struct CanvasOp<CanvasOpType::DrawRect> {
116 SkRect rect;
117 SkPaint paint;
118 void draw(SkCanvas* canvas) const { canvas->drawRect(rect, paint); }
119 ASSERT_DRAWABLE()
120};
121
122
123// cleanup our macros
124#undef ASSERT_DRAWABLE
125
126} // namespace android::uirenderer