blob: 27eca210a03d2038d2b202666b3d2a02caf75c80 [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 <>
Nader Jawadd5c2b6d2020-11-05 19:12:29 -0800115struct CanvasOp<CanvasOpType::DrawPaint> {
116 SkPaint paint;
117 void draw(SkCanvas* canvas) const { canvas->drawPaint(paint); }
118 ASSERT_DRAWABLE()
119};
120
121template <>
John Reck013127b2020-10-29 20:53:51 -0400122struct CanvasOp<CanvasOpType::DrawRect> {
123 SkRect rect;
124 SkPaint paint;
125 void draw(SkCanvas* canvas) const { canvas->drawRect(rect, paint); }
126 ASSERT_DRAWABLE()
127};
128
Nader Jawadd5c2b6d2020-11-05 19:12:29 -0800129template<>
130struct CanvasOp<CanvasOpType::DrawRoundRect> {
131 SkRect rect;
132 SkScalar rx;
133 SkScalar ry;
134 SkPaint paint;
135 void draw(SkCanvas* canvas) const {
136 canvas->drawRoundRect(rect, rx, ry, paint);
137 }
138 ASSERT_DRAWABLE()
139};
140
141template<>
142struct CanvasOp<CanvasOpType::DrawCircle> {
143 SkScalar cx;
144 SkScalar cy;
145 SkScalar radius;
146 SkPaint paint;
147 void draw(SkCanvas* canvas) const {
148 canvas->drawCircle(cx, cy, radius, paint);
149 }
150 ASSERT_DRAWABLE()
151};
152
153template<>
154struct CanvasOp<CanvasOpType::DrawOval> {
155 SkRect oval;
156 SkPaint paint;
157 void draw(SkCanvas* canvas) const {
158 canvas->drawOval(oval, paint);
159 }
160 ASSERT_DRAWABLE()
161};
162
163template<>
164struct CanvasOp<CanvasOpType::DrawArc> {
165 SkRect oval;
166 SkScalar startAngle;
167 SkScalar sweepAngle;
168 bool useCenter;
169 SkPaint paint;
170
171 void draw(SkCanvas* canvas) const {
172 canvas->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
173 }
174 ASSERT_DRAWABLE()
175};
John Reck013127b2020-10-29 20:53:51 -0400176
177// cleanup our macros
178#undef ASSERT_DRAWABLE
179
180} // namespace android::uirenderer