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.
5 #ifndef CONTENT_BROWSER_COMPOSITOR_ACCELERATED_WIDGET_HELPER_MAC_H_
6 #define CONTENT_BROWSER_COMPOSITOR_ACCELERATED_WIDGET_HELPER_MAC_H_
8 #include <IOSurface/IOSurfaceAPI.h>
11 #include "skia/ext/platform_canvas.h"
12 #include "ui/events/latency_info.h"
13 #include "ui/gfx/geometry/size.h"
14 #include "ui/gfx/native_widget_types.h"
17 #include <Cocoa/Cocoa.h>
18 #include "base/mac/scoped_nsobject.h"
19 #include "content/browser/compositor/io_surface_layer_mac.h"
20 #include "content/browser/compositor/software_layer_mac.h"
21 #include "ui/base/cocoa/remote_layer_api.h"
25 class SoftwareFrameData
;
30 class AcceleratedWidgetMac
;
32 // A class through which an AcceleratedWidget may be bound to draw the contents
33 // of an NSView. An AcceleratedWidget may be bound to multiple different views
34 // throughout its lifetime (one at a time, though).
35 class AcceleratedWidgetMacNSView
{
37 virtual NSView
* AcceleratedWidgetGetNSView() const = 0;
38 virtual bool AcceleratedWidgetShouldIgnoreBackpressure() const = 0;
39 virtual void AcceleratedWidgetSwapCompleted(
40 const std::vector
<ui::LatencyInfo
>& latency_info
) = 0;
41 virtual void AcceleratedWidgetHitError() = 0;
46 // AcceleratedWidgetMac owns a tree of CALayers. The widget may be passed
47 // to a ui::Compositor, which will cause, through its output surface, calls to
48 // GotAcceleratedFrame and GotSoftwareFrame. The CALayers may be installed
49 // in an NSView by setting the AcceleratedWidgetMacNSView for the helper.
50 class AcceleratedWidgetMac
: public IOSurfaceLayerClient
{
52 AcceleratedWidgetMac();
53 virtual ~AcceleratedWidgetMac();
55 gfx::AcceleratedWidget
accelerated_widget() { return native_widget_
; }
57 void SetNSView(AcceleratedWidgetMacNSView
* view
);
60 // Return true if the last frame swapped has a size in DIP of |dip_size|.
61 bool HasFrameOfSize(const gfx::Size
& dip_size
) const;
63 // Return the CGL renderer ID for the surface, if one is available.
64 int GetRendererID() const;
66 // Return true if the renderer should not be throttled by GPU back-pressure.
67 bool IsRendererThrottlingDisabled() const;
69 // Mark a bracket in which new frames are being pumped in a restricted nested
71 void BeginPumpingFrames();
72 void EndPumpingFrames();
74 void GotAcceleratedFrame(
75 uint64 surface_handle
,
76 const std::vector
<ui::LatencyInfo
>& latency_info
,
79 const base::Closure
& drawn_callback
);
81 void GotSoftwareFrame(
82 cc::SoftwareFrameData
* frame_data
, float scale_factor
, SkCanvas
* canvas
);
85 // IOSurfaceLayerClient implementation:
86 bool IOSurfaceLayerShouldAckImmediately() const override
;
87 void IOSurfaceLayerDidDrawFrame() override
;
88 void IOSurfaceLayerHitError() override
;
90 void GotAcceleratedCAContextFrame(
91 CAContextID ca_context_id
, gfx::Size pixel_size
, float scale_factor
);
93 void GotAcceleratedIOSurfaceFrame(
94 IOSurfaceID io_surface_id
, gfx::Size pixel_size
, float scale_factor
);
96 void AcknowledgeAcceleratedFrame();
98 // Remove a layer from the heirarchy and destroy it. Because the accelerated
99 // layer types may be replaced by a layer of the same type, the layer to
100 // destroy is parameterized, and, if it is the current layer, the current
102 void DestroyCAContextLayer(
103 base::scoped_nsobject
<CALayerHost
> ca_context_layer
);
104 void DestroyIOSurfaceLayer(
105 base::scoped_nsobject
<IOSurfaceLayer
> io_surface_layer
);
106 void DestroySoftwareLayer();
108 // The AcceleratedWidgetMacNSView that is using this as its internals.
109 AcceleratedWidgetMacNSView
* view_
;
111 // A phony NSView handle used to identify this.
112 gfx::AcceleratedWidget native_widget_
;
114 // A flipped layer, which acts as the parent of the compositing and software
115 // layers. This layer is flipped so that the we don't need to recompute the
116 // origin for sub-layers when their position changes (this is impossible when
117 // using remote layers, as their size change cannot be synchronized with the
118 // window). This indirection is needed because flipping hosted layers (like
119 // |background_layer_| of RenderWidgetHostViewCocoa) leads to unpredictable
121 base::scoped_nsobject
<CALayer
> flipped_layer_
;
123 // The accelerated CoreAnimation layer hosted by the GPU process.
124 base::scoped_nsobject
<CALayerHost
> ca_context_layer_
;
126 // The locally drawn accelerated CoreAnimation layer.
127 base::scoped_nsobject
<IOSurfaceLayer
> io_surface_layer_
;
129 // The locally drawn software layer.
130 base::scoped_nsobject
<SoftwareLayer
> software_layer_
;
132 // If an accelerated frame has come in which has not yet been drawn and acked
133 // then this is the latency info and the callback to make when the frame is
134 // drawn. If there is no such frame then the callback is null.
135 std::vector
<ui::LatencyInfo
> accelerated_latency_info_
;
136 base::Closure accelerated_frame_drawn_callback_
;
138 // The size in DIP of the last swap received from |compositor_|.
139 gfx::Size last_swap_size_dip_
;
141 DISALLOW_COPY_AND_ASSIGN(AcceleratedWidgetMac
);
146 void AcceleratedWidgetMacGotAcceleratedFrame(
147 gfx::AcceleratedWidget widget
, uint64 surface_handle
,
148 const std::vector
<ui::LatencyInfo
>& latency_info
,
149 gfx::Size pixel_size
, float scale_factor
,
150 const base::Closure
& drawn_callback
,
151 bool* disable_throttling
, int* renderer_id
);
153 void AcceleratedWidgetMacGotSoftwareFrame(
154 gfx::AcceleratedWidget widget
,
155 cc::SoftwareFrameData
* frame_data
, float scale_factor
, SkCanvas
* canvas
);
157 } // namespace content
159 #endif // CONTENT_BROWSER_COMPOSITOR_ACCELERATED_WIDGET_HELPER_MAC_H_