-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathDefaultRenderHandler.cs
More file actions
268 lines (230 loc) · 11.2 KB
/
DefaultRenderHandler.cs
File metadata and controls
268 lines (230 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright © 2018 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using CefSharp.Enums;
using CefSharp.Structs;
using Point = System.Drawing.Point;
using Range = CefSharp.Structs.Range;
using Size = System.Drawing.Size;
namespace CefSharp.OffScreen
{
/// <summary>
/// Default implementation of <see cref="IRenderHandler"/>, this class handles Offscreen Rendering (OSR).
/// Upstream documentation at http://magpcss.org/ceforum/apidocs3/projects/(default)/CefRenderHandler.html
/// </summary>
public class DefaultRenderHandler : IRenderHandler
{
private ChromiumWebBrowser browser;
private Size popupSize;
private Point popupPosition;
/// <summary>
/// Need a lock because the caller may be asking for the bitmap
/// while Chromium async rendering has returned on another thread.
/// </summary>
public readonly object BitmapLock = new object();
/// <summary>
/// Gets or sets a value indicating whether the popup is open.
/// </summary>
/// <value>
/// <c>true</c> if popup is opened; otherwise, <c>false</c>.
/// </value>
public bool PopupOpen { get; protected set; }
/// <summary>
/// Contains the last bitmap buffer. Direct access
/// to the underlying buffer - there is no locking when trying
/// to access directly, use <see cref="BitmapBuffer.BitmapLock" /> where appropriate.
/// </summary>
/// <value>The bitmap.</value>
public BitmapBuffer BitmapBuffer { get; private set; }
/// <summary>
/// The popup Bitmap.
/// </summary>
public BitmapBuffer PopupBuffer { get; private set; }
/// <summary>
/// Gets the size of the popup.
/// </summary>
public Size PopupSize
{
get { return popupSize; }
}
/// <summary>
/// Gets the popup position.
/// </summary>
public Point PopupPosition
{
get { return popupPosition; }
}
/// <summary>
/// Create a new instance of DefaultRenderHadler
/// </summary>
/// <param name="browser">reference to the ChromiumWebBrowser</param>
public DefaultRenderHandler(ChromiumWebBrowser browser)
{
this.browser = browser;
popupPosition = new Point();
popupSize = new Size();
BitmapBuffer = new BitmapBuffer(BitmapLock);
PopupBuffer = new BitmapBuffer(BitmapLock);
}
/// <summary>
/// Dispose of this instance.
/// </summary>
public void Dispose()
{
browser = null;
BitmapBuffer = null;
PopupBuffer = null;
}
/// <summary>
/// Called to allow the client to return a ScreenInfo object with appropriate values.
/// If null is returned then the rectangle from GetViewRect will be used.
/// If the rectangle is still empty or invalid popups may not be drawn correctly.
/// </summary>
/// <returns>Return null if no screenInfo structure is provided.</returns>
public virtual ScreenInfo? GetScreenInfo()
{
var deviceScaleFactor = browser?.DeviceScaleFactor;
if (deviceScaleFactor == null)
{
return null;
}
var screenInfo = new ScreenInfo { DeviceScaleFactor = deviceScaleFactor.Value };
return screenInfo;
}
/// <summary>
/// Called to retrieve the view rectangle which is relative to screen coordinates.
/// This method must always provide a non-empty rectangle.
/// </summary>
/// <returns>Return a ViewRect strict containing the rectangle.</returns>
public virtual Rect GetViewRect()
{
//TODO: See if this can be refactored and remove browser reference
var size = browser?.Size;
if (size == null)
{
return new Rect(0, 0, 1, 1);
}
var viewRect = new Rect(0, 0, size.Value.Width, size.Value.Height);
return viewRect;
}
/// <summary>
/// Called to retrieve the translation from view coordinates to actual screen coordinates.
/// </summary>
/// <param name="viewX">x</param>
/// <param name="viewY">y</param>
/// <param name="screenX">screen x</param>
/// <param name="screenY">screen y</param>
/// <returns>Return true if the screen coordinates were provided.</returns>
public virtual bool GetScreenPoint(int viewX, int viewY, out int screenX, out int screenY)
{
screenX = viewX;
screenY = viewY;
return false;
}
/// <summary>
/// Called when an element has been rendered to the shared texture handle.
/// This method is only called when <see cref="IWindowInfo.SharedTextureEnabled"/> is set to true
///
/// The underlying implementation uses a pool to deliver frames. As a result,
/// the handle may differ every frame depending on how many frames are
/// in-progress. The handle's resource cannot be cached and cannot be accessed
/// outside of this callback. It should be reopened each time this callback is
/// executed and the contents should be copied to a texture owned by the
/// client application. The contents of <paramref name="acceleratedPaintInfo"/>acceleratedPaintInfo
/// will be released back to the pool after this callback returns.
/// </summary>
/// <param name="type">indicates whether the element is the view or the popup widget.</param>
/// <param name="dirtyRect">contains the set of rectangles in pixel coordinates that need to be repainted</param>
/// <param name="acceleratedPaintInfo">contains the shared handle; on Windows it is a
/// HANDLE to a texture that can be opened with D3D11 OpenSharedResource.</param>
public virtual void OnAcceleratedPaint(PaintElementType type, Rect dirtyRect, AcceleratedPaintInfo acceleratedPaintInfo)
{
//NOT USED
}
/// <summary>
/// Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates based on the
/// value of <see cref="ScreenInfo.DeviceScaleFactor"/> returned from <see cref="GetScreenInfo"/>.
/// This method is only called when <see cref="IWindowInfo.SharedTextureEnabled"/> is set to false.
/// Called on the CEF UI Thread
/// </summary>
/// <param name="type">indicates whether the element is the view or the popup widget.</param>
/// <param name="dirtyRect">contains the set of rectangles in pixel coordinates that need to be repainted</param>
/// <param name="buffer">The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin</param>
/// <param name="width">width</param>
/// <param name="height">height</param>
public virtual void OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height)
{
var isPopup = type == PaintElementType.Popup;
var bitmapBuffer = isPopup ? PopupBuffer : BitmapBuffer;
bitmapBuffer.UpdateBuffer(width, height, buffer, dirtyRect);
}
/// <summary>
/// Called when the browser's cursor has changed.
/// </summary>
/// <param name="cursor">If type is Custom then customCursorInfo will be populated with the custom cursor information</param>
/// <param name="type">cursor type</param>
/// <param name="customCursorInfo">custom cursor Information</param>
public virtual void OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
{
}
/// <summary>
/// Called when the user starts dragging content in the web view. Contextual information about the dragged content is
/// supplied by dragData. OS APIs that run a system message loop may be used within the StartDragging call.
/// Don't call any of the IBrowserHost.DragSource*Ended* methods after returning false.
/// Return true to handle the drag operation. Call <see cref="IBrowserHost.DragSourceEndedAt"/> and <see cref="IBrowserHost.DragSourceSystemDragEnded"/> either synchronously or asynchronously to inform
/// the web view that the drag operation has ended.
/// </summary>
/// <param name="dragData">drag data</param>
/// <param name="mask">operation mask</param>
/// <param name="x">combined x and y provide the drag start location in screen coordinates</param>
/// <param name="y">combined x and y provide the drag start location in screen coordinates</param>
/// <returns>Return false to abort the drag operation.</returns>
public virtual bool StartDragging(IDragData dragData, DragOperationsMask mask, int x, int y)
{
return false;
}
/// <summary>
/// Called when the web view wants to update the mouse cursor during a drag & drop operation.
/// </summary>
/// <param name="operation">describes the allowed operation (none, move, copy, link). </param>
public virtual void UpdateDragCursor(DragOperationsMask operation)
{
}
/// <summary>
/// Called when the browser wants to show or hide the popup widget.
/// </summary>
/// <param name="show">The popup should be shown if show is true and hidden if show is false.</param>
public virtual void OnPopupShow(bool show)
{
PopupOpen = show;
}
/// <summary>
/// Called when the browser wants to move or resize the popup widget.
/// </summary>
/// <param name="rect">contains the new location and size in view coordinates. </param>
public virtual void OnPopupSize(Rect rect)
{
popupPosition.X = rect.X;
popupPosition.Y = rect.Y;
popupSize.Width = rect.Width;
popupSize.Height = rect.Height;
}
/// <summary>
/// Called when the IME composition range has changed.
/// </summary>
/// <param name="selectedRange">is the range of characters that have been selected</param>
/// <param name="characterBounds">is the bounds of each character in view coordinates.</param>
public virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[] characterBounds)
{
}
/// <summary>
/// Called when an on-screen keyboard should be shown or hidden for the specified browser.
/// </summary>
/// <param name="browser">the browser</param>
/// <param name="inputMode">specifies what kind of keyboard should be opened. If <see cref="TextInputMode.None"/>, any existing keyboard for this browser should be hidden.</param>
public virtual void OnVirtualKeyboardRequested(IBrowser browser, TextInputMode inputMode)
{
}
}
}