John Reck | 013127b | 2020-10-29 20:53:51 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 28 | namespace android::uirenderer { |
| 29 | |
| 30 | template <CanvasOpType T> |
| 31 | struct CanvasOp; |
| 32 | |
| 33 | struct 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 | |
| 50 | template <> |
| 51 | struct CanvasOp<CanvasOpType::Save> { |
| 52 | void draw(SkCanvas* canvas) const { canvas->save(); } |
| 53 | ASSERT_DRAWABLE() |
| 54 | }; |
| 55 | |
| 56 | template <> |
| 57 | struct CanvasOp<CanvasOpType::SaveLayer> { |
| 58 | SkCanvas::SaveLayerRec saveLayerRec; |
| 59 | void draw(SkCanvas* canvas) const { canvas->saveLayer(saveLayerRec); } |
| 60 | ASSERT_DRAWABLE() |
| 61 | }; |
| 62 | |
| 63 | template <> |
| 64 | struct CanvasOp<CanvasOpType::SaveBehind> { |
| 65 | SkRect bounds; |
| 66 | void draw(SkCanvas* canvas) const { SkAndroidFrameworkUtils::SaveBehind(canvas, &bounds); } |
| 67 | ASSERT_DRAWABLE() |
| 68 | }; |
| 69 | |
| 70 | template <> |
| 71 | struct CanvasOp<CanvasOpType::Restore> { |
| 72 | void draw(SkCanvas* canvas) const { canvas->restore(); } |
| 73 | ASSERT_DRAWABLE() |
| 74 | }; |
| 75 | |
| 76 | template <> |
| 77 | struct CanvasOp<CanvasOpType::BeginZ> { |
| 78 | }; |
| 79 | template <> |
| 80 | struct CanvasOp<CanvasOpType::EndZ> {}; |
| 81 | |
| 82 | // ---------------------------------------------- |
| 83 | // Clip Ops |
| 84 | // --------------------------------------------- |
| 85 | |
| 86 | template <> |
| 87 | struct CanvasOp<CanvasOpType::ClipRect> { |
| 88 | SkRect rect; |
| 89 | SkClipOp clipOp; |
| 90 | void draw(SkCanvas* canvas) const { canvas->clipRect(rect, clipOp); } |
| 91 | ASSERT_DRAWABLE() |
| 92 | }; |
| 93 | |
| 94 | template <> |
| 95 | struct 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 | |
| 106 | template <> |
| 107 | struct CanvasOp<CanvasOpType::DrawColor> { |
| 108 | SkColor4f color; |
| 109 | SkBlendMode mode; |
| 110 | void draw(SkCanvas* canvas) const { canvas->drawColor(color, mode); } |
| 111 | ASSERT_DRAWABLE() |
| 112 | }; |
| 113 | |
| 114 | template <> |
| 115 | struct 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 |