paint_preview_compositor_impl.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 The Chromium 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. #ifndef COMPONENTS_SERVICES_PAINT_PREVIEW_COMPOSITOR_PAINT_PREVIEW_COMPOSITOR_IMPL_H_
  5. #define COMPONENTS_SERVICES_PAINT_PREVIEW_COMPOSITOR_PAINT_PREVIEW_COMPOSITOR_IMPL_H_
  6. #include <stdint.h>
  7. #include "base/callback.h"
  8. #include "base/containers/flat_map.h"
  9. #include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
  10. #include "components/paint_preview/common/proto/paint_preview.pb.h"
  11. #include "components/paint_preview/common/recording_map.h"
  12. #include "components/services/paint_preview_compositor/paint_preview_frame.h"
  13. #include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h"
  14. #include "mojo/public/cpp/bindings/pending_receiver.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "ui/gfx/geometry/rect.h"
  17. #include "url/gurl.h"
  18. namespace paint_preview {
  19. struct SkpResult;
  20. class PaintPreviewCompositorImpl : public mojom::PaintPreviewCompositor {
  21. public:
  22. using FileMap = base::flat_map<base::UnguessableToken, base::File>;
  23. // Creates a new PaintPreviewCompositorImpl that receives mojo requests over
  24. // |receiver|. |receiver| should be created by the remote and
  25. // |disconnect_handler| is invoked when the remote closes the connection
  26. // invalidating |receiver|.
  27. //
  28. // For testing |receiver| can be a NullReceiver (i.e. a 'local' instance not
  29. // connected to a remote) and |disconnect_handler| should be a no-op.
  30. explicit PaintPreviewCompositorImpl(
  31. mojo::PendingReceiver<mojom::PaintPreviewCompositor> receiver,
  32. scoped_refptr<discardable_memory::ClientDiscardableSharedMemoryManager>
  33. discardable_shared_memory_manager,
  34. base::OnceClosure disconnect_handler);
  35. ~PaintPreviewCompositorImpl() override;
  36. PaintPreviewCompositorImpl(const PaintPreviewCompositorImpl&) = delete;
  37. PaintPreviewCompositorImpl& operator=(const PaintPreviewCompositorImpl&) =
  38. delete;
  39. // PaintPreviewCompositor implementation.
  40. void BeginSeparatedFrameComposite(
  41. mojom::PaintPreviewBeginCompositeRequestPtr request,
  42. BeginSeparatedFrameCompositeCallback callback) override;
  43. void BitmapForSeparatedFrame(
  44. const base::UnguessableToken& frame_guid,
  45. const gfx::Rect& clip_rect,
  46. float scale_factor,
  47. BitmapForSeparatedFrameCallback callback) override;
  48. void BeginMainFrameComposite(
  49. mojom::PaintPreviewBeginCompositeRequestPtr request,
  50. BeginMainFrameCompositeCallback callback) override;
  51. void BitmapForMainFrame(const gfx::Rect& clip_rect,
  52. float scale_factor,
  53. BitmapForMainFrameCallback callback) override;
  54. void SetRootFrameUrl(const GURL& url) override;
  55. private:
  56. // Adds |frame_proto| to |frames_| and copies required data into |response|.
  57. // Consumes the corresponding file in |file_map|. Returns true on success.
  58. bool AddFrame(
  59. const PaintPreviewFrameProto& frame_proto,
  60. const base::flat_map<base::UnguessableToken, SkpResult>& skp_map,
  61. mojom::PaintPreviewBeginCompositeResponsePtr* response);
  62. static base::flat_map<base::UnguessableToken, SkpResult> DeserializeAllFrames(
  63. RecordingMap&& recording_map);
  64. // Deserialize a the recording of the frame specified by |frame_proto|.
  65. // Subframes are recursed into and loaded into |loaded_frames| so the current
  66. // frame will have them available during its own deserialization. Recordings
  67. // are erased from |recording_map| as they are consumed.
  68. // |subframe_failed| returns whether or not any subframes (or subframes of
  69. // subframes) failed during deserialization.
  70. // The resulting picture will contain subframes (or an empty placeholder in
  71. // the case of failed subframes) or will return |nullptr| on failure.
  72. static sk_sp<SkPicture> DeserializeFrameRecursive(
  73. const PaintPreviewFrameProto& frame_proto,
  74. const PaintPreviewProto& proto,
  75. base::flat_map<base::UnguessableToken, sk_sp<SkPicture>>* loaded_frames,
  76. RecordingMap* recording_map,
  77. bool* subframe_failed);
  78. mojo::Receiver<mojom::PaintPreviewCompositor> receiver_{this};
  79. GURL url_;
  80. // A mapping from frame GUID to its associated data. Empty until
  81. // |BeginSeparatedFrameComposite| is called.
  82. // Must be modified only by |BeginSeparatedFrameComposite|.
  83. base::flat_map<base::UnguessableToken, PaintPreviewFrame> frames_;
  84. // Contains the root frame, including content from subframes. |nullptr| until
  85. // |BeginMainFrameComposite| succeeds.
  86. // Must be modified only by |BeginMainFrameComposite|.
  87. sk_sp<SkPicture> root_frame_;
  88. scoped_refptr<discardable_memory::ClientDiscardableSharedMemoryManager>
  89. discardable_shared_memory_manager_;
  90. base::WeakPtrFactory<PaintPreviewCompositorImpl> weak_ptr_factory_{this};
  91. };
  92. } // namespace paint_preview
  93. #endif // COMPONENTS_SERVICES_PAINT_PREVIEW_COMPOSITOR_PAINT_PREVIEW_COMPOSITOR_IMPL_H_