1 // Copyright 2018 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 
5 #ifndef UI_OZONE_PLATFORM_SCENIC_SCENIC_WINDOW_CANVAS_H_
6 #define UI_OZONE_PLATFORM_SCENIC_SCENIC_WINDOW_CANVAS_H_
7 
8 #include <lib/ui/scenic/cpp/resources.h>
9 #include <memory>
10 
11 #include "base/macros.h"
12 #include "base/memory/shared_memory_mapping.h"
13 #include "third_party/skia/include/core/SkRegion.h"
14 #include "third_party/skia/include/core/SkSurface.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/ozone/platform/scenic/scenic_surface.h"
17 #include "ui/ozone/platform/scenic/scenic_surface_factory.h"
18 #include "ui/ozone/public/surface_ozone_canvas.h"
19 
20 class SkSurface;
21 
22 namespace scenic {
23 class Session;
24 }  // namespace scenic
25 
26 namespace ui {
27 
28 class ScenicWindow;
29 
30 // SurfaceOzoneCanvas implementation for ScenicWindow. It allows to draw on a
31 // ScenicWindow.
32 class ScenicWindowCanvas : public SurfaceOzoneCanvas {
33  public:
34   // |scenic_surface| must outlive the canvas. ScenicSurface owns the
35   // scenic::Session used in this class for all drawing operations.
36   explicit ScenicWindowCanvas(ScenicSurface* scenic_surface);
37   ~ScenicWindowCanvas() override;
38 
39   // SurfaceOzoneCanvas implementation.
40   void ResizeCanvas(const gfx::Size& viewport_size) override;
41   SkCanvas* GetCanvas() override;
42   void PresentCanvas(const gfx::Rect& damage) override;
43   std::unique_ptr<gfx::VSyncProvider> CreateVSyncProvider() override;
44 
45  private:
46   // Use 2 buffers: one is shown on the screen while the other is used to render
47   // the next frame.
48   static const int kNumBuffers = 2;
49 
50   struct Frame {
51     Frame();
52     ~Frame();
53 
54     // Allocates and maps memory for a frame of |size| (in physical in pixels)
55     // and then registers it with |scenic|.
56     void Initialize(gfx::Size size, scenic::Session* scenic);
57 
58     // Copies pixels covered by |dirty_region| from another |frame|.
59     void CopyDirtyRegionFrom(const Frame& frame);
60 
is_emptyFrame61     bool is_empty() { return !surface; }
62 
63     // Shared memory for the buffer.
64     base::WritableSharedMemoryMapping memory_mapping;
65 
66     // Scenic Memory resource for |memory_region|.
67     std::unique_ptr<scenic::Memory> scenic_memory;
68 
69     // SkSurface that wraps |memory_mapping|.
70     sk_sp<SkSurface> surface;
71 
72     // Fence that will be released by Scenic when it stops using this frame.
73     zx::event release_fence;
74 
75     // The region of the frame that's not up-to-date.
76     SkRegion dirty_region;
77   };
78 
79   Frame frames_[kNumBuffers];
80 
81   // Buffer index in |frames_| for the frame that's currently being rendered.
82   int current_frame_ = 0;
83 
84   // View size in device pixels.
85   gfx::Size viewport_size_;
86 
87   ScenicSurface* const scenic_surface_;
88 
89   DISALLOW_COPY_AND_ASSIGN(ScenicWindowCanvas);
90 };
91 
92 }  // namespace ui
93 
94 #endif  // UI_OZONE_PLATFORM_SCENIC_SCENIC_WINDOW_CANVAS_H_
95