1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_FLUTTER_RENDER_CONTEXT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_FLUTTER_RENDER_CONTEXT_H 18 19 #include "flutter/lib/ui/compositing/scene.h" 20 #include "flutter/lib/ui/compositing/scene_builder.h" 21 #include "flutter/lib/ui/painting/canvas.h" 22 #include "flutter/lib/ui/painting/picture_recorder.h" 23 24 #include "base/geometry/rect.h" 25 #include "core/pipeline/base/render_context.h" 26 #include "core/pipeline/base/render_layer.h" 27 #include "core/pipeline/layers/container_layer.h" 28 #include "core/pipeline/layers/offset_layer.h" 29 #include "core/pipeline/layers/picture_layer.h" 30 31 namespace OHOS::Ace { 32 33 class FlutterRenderContext : public RenderContext { 34 DECLARE_ACE_TYPE(FlutterRenderContext, RenderContext) 35 36 public: 37 FlutterRenderContext() = default; 38 ~FlutterRenderContext() override; 39 40 void Repaint(const RefPtr<RenderNode>& node) override; 41 void PaintChild(const RefPtr<RenderNode>& child, const Offset& offset) override; 42 bool IsIntersectWith(const RefPtr<RenderNode>& child, Offset& offset) override; 43 void Restore() override; 44 45 void InitContext(RenderLayer layer, const Rect& rect); 46 flutter::Canvas* GetCanvas(); 47 void StopRecordingIfNeeded(); 48 GetContainerLayer()49 Flutter::ContainerLayer* GetContainerLayer() const 50 { 51 return containerLayer_; 52 } 53 IsRecording()54 bool IsRecording() 55 { 56 return !!canvas_; 57 } 58 FinishRecordingAsPicture()59 sk_sp<SkPicture> FinishRecordingAsPicture() 60 { 61 if (recorder_) { 62 auto picture = recorder_->endRecording(); 63 if (picture) { 64 return picture->picture(); 65 } 66 } 67 return nullptr; 68 } 69 FinishRecordingAsImage()70 sk_sp<SkImage> FinishRecordingAsImage() 71 { 72 if (recorder_) { 73 auto picture = recorder_->endRecording(); 74 if (picture) { 75 auto image = picture->toImage(estimatedRect_.Width(), estimatedRect_.Height()); 76 if (image) { 77 return image->image(); 78 } 79 } 80 } 81 return nullptr; 82 } 83 84 private: 85 void StartRecording(); 86 void SetOffSet( 87 const RefPtr<RenderNode>& child, Flutter::OffsetLayer* layer, const Offset& pos, const std::string& name); 88 89 fml::RefPtr<flutter::PictureRecorder> recorder_; 90 fml::RefPtr<flutter::Canvas> canvas_; 91 Flutter::ContainerLayer* containerLayer_ = nullptr; 92 RefPtr<Flutter::PictureLayer> currentLayer_; 93 Rect estimatedRect_; 94 }; 95 96 } // namespace OHOS::Ace 97 98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_FLUTTER_RENDER_CONTEXT_H 99