| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_COMMON_GRAPHICS_TEXTURE_H_ |
| 6 | #define FLUTTER_COMMON_GRAPHICS_TEXTURE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "flutter/display_list/dl_canvas.h" |
| 11 | #include "flutter/fml/macros.h" |
| 12 | #include "flutter/fml/synchronization/waitable_event.h" |
| 13 | #include "third_party/skia/include/core/SkCanvas.h" |
| 14 | #include "third_party/skia/include/core/SkSamplingOptions.h" |
| 15 | |
| 16 | class GrDirectContext; |
| 17 | |
| 18 | namespace impeller { |
| 19 | class AiksContext; |
| 20 | }; |
| 21 | |
| 22 | namespace flutter { |
| 23 | |
| 24 | class ContextListener { |
| 25 | public: |
| 26 | ContextListener(); |
| 27 | ~ContextListener(); |
| 28 | |
| 29 | // Called from raster thread. |
| 30 | virtual void OnGrContextCreated() = 0; |
| 31 | |
| 32 | // Called from raster thread. |
| 33 | virtual void OnGrContextDestroyed() = 0; |
| 34 | |
| 35 | private: |
| 36 | FML_DISALLOW_COPY_AND_ASSIGN(ContextListener); |
| 37 | }; |
| 38 | |
| 39 | class Texture : public ContextListener { |
| 40 | public: |
| 41 | struct PaintContext { |
| 42 | DlCanvas* canvas = nullptr; |
| 43 | GrDirectContext* gr_context = nullptr; |
| 44 | impeller::AiksContext* aiks_context = nullptr; |
| 45 | const DlPaint* paint = nullptr; |
| 46 | }; |
| 47 | |
| 48 | explicit Texture(int64_t id); // Called from UI or raster thread. |
| 49 | virtual ~Texture(); // Called from raster thread. |
| 50 | |
| 51 | // Called from raster thread. |
| 52 | virtual void Paint(PaintContext& context, |
| 53 | const SkRect& bounds, |
| 54 | bool freeze, |
| 55 | const DlImageSampling sampling) = 0; |
| 56 | |
| 57 | // Called on raster thread. |
| 58 | virtual void MarkNewFrameAvailable() = 0; |
| 59 | |
| 60 | // Called on raster thread. |
| 61 | virtual void OnTextureUnregistered() = 0; |
| 62 | |
| 63 | int64_t Id() { return id_; } |
| 64 | |
| 65 | private: |
| 66 | int64_t id_; |
| 67 | FML_DISALLOW_COPY_AND_ASSIGN(Texture); |
| 68 | }; |
| 69 | |
| 70 | class TextureRegistry { |
| 71 | public: |
| 72 | TextureRegistry(); |
| 73 | |
| 74 | // Called from raster thread. |
| 75 | void RegisterTexture(const std::shared_ptr<Texture>& texture); |
| 76 | |
| 77 | // Called from raster thread. |
| 78 | void RegisterContextListener(uintptr_t id, |
| 79 | std::weak_ptr<ContextListener> image); |
| 80 | |
| 81 | // Called from raster thread. |
| 82 | void UnregisterTexture(int64_t id); |
| 83 | |
| 84 | // Called from the raster thread. |
| 85 | void UnregisterContextListener(uintptr_t id); |
| 86 | |
| 87 | // Called from raster thread. |
| 88 | std::shared_ptr<Texture> GetTexture(int64_t id); |
| 89 | |
| 90 | // Called from raster thread. |
| 91 | void OnGrContextCreated(); |
| 92 | |
| 93 | // Called from raster thread. |
| 94 | void OnGrContextDestroyed(); |
| 95 | |
| 96 | private: |
| 97 | std::map<int64_t, std::shared_ptr<Texture>> mapping_; |
| 98 | size_t image_counter_; |
| 99 | // This map keeps track of registered context listeners by their own |
| 100 | // externally provided id. It indexes into ordered_images_. |
| 101 | std::map<uintptr_t, size_t> image_indices_; |
| 102 | // This map makes sure that iteration of images happens in insertion order |
| 103 | // (managed by image_counter_) so that images which depend on other images get |
| 104 | // re-created in the right order. |
| 105 | using InsertionOrderMap = |
| 106 | std::map<size_t, std::pair<uintptr_t, std::weak_ptr<ContextListener>>>; |
| 107 | InsertionOrderMap ordered_images_; |
| 108 | |
| 109 | FML_DISALLOW_COPY_AND_ASSIGN(TextureRegistry); |
| 110 | }; |
| 111 | |
| 112 | } // namespace flutter |
| 113 | |
| 114 | #endif // FLUTTER_COMMON_GRAPHICS_TEXTURE_H_ |
| 115 |
