1 // Copyright 2014 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 COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_SOFTWARE_OUTPUT_DEVICE_MAC_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_SOFTWARE_OUTPUT_DEVICE_MAC_H_
7 
8 #include <IOSurface/IOSurface.h>
9 #include <list>
10 
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/macros.h"
13 #include "components/viz/service/display/software_output_device.h"
14 #include "components/viz/service/viz_service_export.h"
15 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkRegion.h"
17 
18 class SkCanvas;
19 
20 namespace viz {
21 
22 class VIZ_SERVICE_EXPORT SoftwareOutputDeviceMac : public SoftwareOutputDevice {
23  public:
24   explicit SoftwareOutputDeviceMac(
25       scoped_refptr<base::SequencedTaskRunner> task_runner);
26   ~SoftwareOutputDeviceMac() override;
27 
28   // SoftwareOutputDevice implementation.
29   void Resize(const gfx::Size& pixel_size, float scale_factor) override;
30   SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override;
31   void EndPaint() override;
32   void DiscardBackbuffer() override;
33   void EnsureBackbuffer() override;
34   gfx::VSyncProvider* GetVSyncProvider() override;
35 
36   // Testing methods.
LastCopyRegionForTesting()37   SkRegion LastCopyRegionForTesting() const {
38     return last_copy_region_for_testing_;
39   }
CurrentPaintIOSurfaceForTesting()40   IOSurfaceRef CurrentPaintIOSurfaceForTesting() const {
41     return current_paint_buffer_->io_surface.get();
42   }
BufferQueueSizeForTesting()43   size_t BufferQueueSizeForTesting() const { return buffer_queue_.size(); }
44 
45  private:
46   struct Buffer {
47     Buffer();
48     ~Buffer();
49     base::ScopedCFTypeRef<IOSurfaceRef> io_surface;
50     // The damage of all BeginPaints since this buffer was the back buffer.
51     SkRegion accumulated_damage;
52   };
53 
54   // Copy the pixels from the previous buffer to the new buffer, and union
55   // |new_damage_rect| into all |buffer_queue_|'s accumulated damages.
56   void UpdateAndCopyBufferDamage(Buffer* previous_paint_buffer,
57                                  const SkRegion& new_damage_rect);
58 
59   gfx::Size pixel_size_;
60   float scale_factor_ = 1;
61 
62   // The queue of buffers. The back is the most recently painted buffer
63   // (sometimes equal to |current_paint_buffer_|), and the front is the
64   // least recently painted buffer.
65   std::list<std::unique_ptr<Buffer>> buffer_queue_;
66 
67   // A pointer to the last element of |buffer_queue_| during paint. It is only
68   // valid between BeginPaint and EndPaint.
69   Buffer* current_paint_buffer_ = nullptr;
70 
71   // The SkCanvas wraps the mapped |current_paint_buffer_|'s IOSurface. It is
72   // valid only between BeginPaint and EndPaint.
73   std::unique_ptr<SkCanvas> current_paint_canvas_;
74 
75   SkRegion last_copy_region_for_testing_;
76 
77   DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceMac);
78 };
79 
80 }  // namespace viz
81 
82 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_SOFTWARE_OUTPUT_DEVICE_MAC_H_
83