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 <hwui/Bitmap.h> 20 21 #include <SkBitmap.h> 22 #include <SkCanvas.h> 23 24 #include "CanvasOps.h" 25 26 #include <experimental/type_traits> 27 #include <variant> 28 29 namespace android::uirenderer { 30 31 class CanvasOpBuffer; 32 33 void rasterizeCanvasBuffer(const CanvasOpBuffer& source, SkCanvas* destination); 34 35 class ImmediateModeRasterizer { 36 public: ImmediateModeRasterizer(std::unique_ptr<SkCanvas> && canvas)37 explicit ImmediateModeRasterizer(std::unique_ptr<SkCanvas>&& canvas) { 38 mCanvas = canvas.get(); 39 mOwnership = std::move(canvas); 40 } 41 ImmediateModeRasterizer(std::shared_ptr<SkCanvas> canvas)42 explicit ImmediateModeRasterizer(std::shared_ptr<SkCanvas> canvas) { 43 mCanvas = canvas.get(); 44 mOwnership = std::move(canvas); 45 } 46 ImmediateModeRasterizer(Bitmap & bitmap)47 explicit ImmediateModeRasterizer(Bitmap& bitmap) { 48 mCanvas = &(mOwnership.emplace<SkCanvas>(bitmap.getSkBitmap())); 49 } 50 51 template <CanvasOpType T> draw(const CanvasOp<T> & op)52 void draw(const CanvasOp<T>& op) { 53 if constexpr (CanvasOpTraits::can_draw<CanvasOp<T>>) { 54 op.draw(mCanvas); 55 } 56 } 57 58 private: 59 SkCanvas* mCanvas; 60 // Just here to keep mCanvas alive. Thankfully we never need to actually look inside this... 61 std::variant<SkCanvas, std::shared_ptr<SkCanvas>, std::unique_ptr<SkCanvas>> mOwnership; 62 }; 63 64 } // namespace android::uirenderer 65