-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathCefBrowserOsr.java
More file actions
723 lines (637 loc) · 28.5 KB
/
CefBrowserOsr.java
File metadata and controls
723 lines (637 loc) · 28.5 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package org.cef.browser;
import com.jogamp.nativewindow.NativeSurface;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLContext;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.GLBuffers;
import org.cef.CefBrowserSettings;
import org.cef.CefClient;
import org.cef.OS;
import org.cef.callback.CefDragData;
import org.cef.handler.CefAcceleratedPaintInfo;
import org.cef.handler.CefRenderHandler;
import org.cef.handler.CefScreenInfo;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceAdapter;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DropTarget;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.lang.ClassNotFoundException;
import java.lang.IllegalAccessException;
import java.lang.IllegalArgumentException;
import java.lang.NoSuchMethodException;
import java.lang.SecurityException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
/**
* This class represents an off-screen rendered browser.
* The visibility of this class is "package". To create a new
* CefBrowser instance, please use CefBrowserFactory.
*/
class CefBrowserOsr extends CefBrowser_N implements CefRenderHandler {
private CefRenderer renderer_;
private GLCanvas canvas_;
private long window_handle_ = 0;
private boolean justCreated_ = false;
private Rectangle browser_rect_ = new Rectangle(0, 0, 1, 1); // Work around CEF issue #1437.
private Point screenPoint_ = new Point(0, 0);
private double scaleFactor_ = 1.0;
private int depth = 32;
private int depth_per_component = 8;
private boolean isTransparent_;
private CopyOnWriteArrayList<Consumer<CefPaintEvent>> onPaintListeners =
new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Consumer<CefAcceleratedPaintEvent>> onAcceleratedPaintListeners =
new CopyOnWriteArrayList<>();
CefBrowserOsr(CefClient client, String url, boolean transparent, CefRequestContext context,
CefBrowserSettings settings) {
this(client, url, transparent, context, null, null, settings);
}
private CefBrowserOsr(CefClient client, String url, boolean transparent,
CefRequestContext context, CefBrowserOsr parent, Point inspectAt,
CefBrowserSettings settings) {
super(client, url, context, parent, inspectAt, settings);
isTransparent_ = transparent;
renderer_ = new CefRenderer(transparent);
createGLCanvas();
}
@Override
public void createImmediately() {
justCreated_ = true;
// Create the browser immediately.
createBrowserIfRequired(false);
}
@Override
public Component getUIComponent() {
return canvas_;
}
@Override
public CefRenderHandler getRenderHandler() {
return this;
}
@Override
protected CefBrowser_N createDevToolsBrowser(CefClient client, String url,
CefRequestContext context, CefBrowser_N parent, Point inspectAt) {
return new CefBrowserOsr(
client, url, isTransparent_, context, (CefBrowserOsr) this, inspectAt, null);
}
private synchronized long getWindowHandle() {
if (window_handle_ == 0) {
NativeSurface surface = canvas_.getNativeSurface();
if (surface != null) {
surface.lockSurface();
window_handle_ = getWindowHandle(surface.getSurfaceHandle());
surface.unlockSurface();
assert (window_handle_ != 0);
}
}
return window_handle_;
}
@SuppressWarnings("serial")
private void createGLCanvas() {
GLProfile glprofile = GLProfile.getMaxFixedFunc(true);
GLCapabilities glcapabilities = new GLCapabilities(glprofile);
canvas_ = new GLCanvas(glcapabilities) {
private Method scaleFactorAccessor = null;
private boolean removed_ = true;
@Override
public void paint(Graphics g) {
createBrowserIfRequired(true);
if (g instanceof Graphics2D) {
GraphicsConfiguration config = ((Graphics2D) g).getDeviceConfiguration();
depth = config.getColorModel().getPixelSize();
depth_per_component = config.getColorModel().getComponentSize()[0];
if (OS.isMacintosh()
&& System.getProperty("java.runtime.version").startsWith("1.8")) {
// This fixes a weird thing on MacOS: the scale factor being read from
// getTransform().getScaleX() is incorrect for Java 8 VMs; it is always
// 1, even though Retina display scaling of window sizes etc. is
// definitely ongoing somewhere in the lower levels of AWT. This isn't
// too big of a problem for us, because the transparent scaling handles
// the situation, except for one thing: the screenshot-grabbing
// code below, which reads from the OpenGL context, must know the real
// scale factor, because the image to be read is larger by that factor
// and thus a bigger buffer is required. This is why there's some
// admittedly-ugly reflection magic going on below that's able to get
// the real scale factor.
// All of this is not relevant for either Windows or MacOS JDKs > 8,
// for which the official "getScaleX()" approach works fine.
try {
if (scaleFactorAccessor == null) {
scaleFactorAccessor = getClass()
.getClassLoader()
.loadClass("sun.awt.CGraphicsDevice")
.getDeclaredMethod("getScaleFactor");
}
Object factor = scaleFactorAccessor.invoke(config.getDevice());
if (factor instanceof Integer) {
scaleFactor_ = ((Integer) factor).doubleValue();
} else {
scaleFactor_ = 1.0;
}
} catch (InvocationTargetException | IllegalAccessException
| IllegalArgumentException | NoSuchMethodException
| SecurityException | ClassNotFoundException exc) {
scaleFactor_ = 1.0;
}
} else {
scaleFactor_ = ((Graphics2D) g).getTransform().getScaleX();
}
}
super.paint(g);
}
@Override
public void addNotify() {
super.addNotify();
if (removed_) {
notifyAfterParentChanged();
removed_ = false;
}
}
@Override
public void removeNotify() {
if (!removed_) {
if (!isClosed()) {
notifyAfterParentChanged();
}
removed_ = true;
}
super.removeNotify();
}
};
// The GLContext will be re-initialized when changing displays, resulting in calls to
// dispose/init/reshape.
canvas_.addGLEventListener(new GLEventListener() {
@Override
public void reshape(
GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
int newWidth = width;
int newHeight = height;
if (OS.isMacintosh()) {
// HiDPI display scale correction support code
// For some reason this does seem to be necessary on MacOS only.
// If doing this correction on Windows, the browser content would be too
// small and in the lower left corner of the canvas only.
newWidth = (int) (width / scaleFactor_);
newHeight = (int) (height / scaleFactor_);
}
browser_rect_.setBounds(x, y, newWidth, newHeight);
screenPoint_ = canvas_.getLocationOnScreen();
wasResized(newWidth, newHeight);
}
@Override
public void init(GLAutoDrawable glautodrawable) {
renderer_.initialize(glautodrawable.getGL().getGL2());
}
@Override
public void dispose(GLAutoDrawable glautodrawable) {
renderer_.cleanup(glautodrawable.getGL().getGL2());
}
@Override
public void display(GLAutoDrawable glautodrawable) {
renderer_.render(glautodrawable.getGL().getGL2());
}
});
canvas_.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
sendMouseEvent(e);
}
@Override
public void mouseReleased(MouseEvent e) {
sendMouseEvent(e);
}
@Override
public void mouseEntered(MouseEvent e) {
sendMouseEvent(e);
}
@Override
public void mouseExited(MouseEvent e) {
sendMouseEvent(e);
}
@Override
public void mouseClicked(MouseEvent e) {
sendMouseEvent(e);
}
});
canvas_.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
sendMouseEvent(e);
}
@Override
public void mouseDragged(MouseEvent e) {
sendMouseEvent(e);
}
});
canvas_.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
sendMouseWheelEvent(e);
}
});
canvas_.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
sendKeyEvent(e);
}
@Override
public void keyPressed(KeyEvent e) {
sendKeyEvent(e);
}
@Override
public void keyReleased(KeyEvent e) {
sendKeyEvent(e);
}
});
canvas_.setFocusable(true);
canvas_.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
setFocus(false);
}
@Override
public void focusGained(FocusEvent e) {
// Dismiss any Java menus that are currently displayed.
MenuSelectionManager.defaultManager().clearSelectedPath();
setFocus(true);
}
});
// Connect the Canvas with a drag and drop listener.
new DropTarget(canvas_, new CefDropTargetListener(this));
}
@Override
public Rectangle getViewRect(CefBrowser browser) {
return browser_rect_;
}
@Override
public Point getScreenPoint(CefBrowser browser, Point viewPoint) {
Point screenPoint = new Point(screenPoint_);
screenPoint.translate(viewPoint.x, viewPoint.y);
return screenPoint;
}
@Override
public void onPopupShow(CefBrowser browser, boolean show) {
if (!show) {
renderer_.clearPopupRects();
invalidate();
}
}
@Override
public void onPopupSize(CefBrowser browser, Rectangle size) {
renderer_.onPopupSize(size);
}
@Override
public void addOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.add(listener);
}
@Override
public void setOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.clear();
onPaintListeners.add(listener);
}
@Override
public void removeOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.remove(listener);
}
@Override
public void addOnAcceleratedPaintListener(Consumer<CefAcceleratedPaintEvent> listener) {
onAcceleratedPaintListeners.add(listener);
}
@Override
public void setOnAcceleratedPaintListener(Consumer<CefAcceleratedPaintEvent> listener) {
onAcceleratedPaintListeners.clear();
onAcceleratedPaintListeners.add(listener);
}
@Override
public void removeOnAcceleratedPaintListener(Consumer<CefAcceleratedPaintEvent> listener) {
onAcceleratedPaintListeners.remove(listener);
}
@Override
public void onPaint(CefBrowser browser, boolean popup, Rectangle[] dirtyRects,
ByteBuffer buffer, int width, int height) {
// if window is closing, canvas_ or opengl context could be null
final GLContext context = canvas_ != null ? canvas_.getContext() : null;
if (context == null) {
return;
}
// This result can occur due to GLContext re-initialization when changing displays.
if (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
return;
}
renderer_.onPaint(canvas_.getGL().getGL2(), popup, dirtyRects, buffer, width, height);
context.release();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
canvas_.display();
}
});
if (!onPaintListeners.isEmpty()) {
CefPaintEvent paintEvent =
new CefPaintEvent(browser, popup, dirtyRects, buffer, width, height);
for (Consumer<CefPaintEvent> l : onPaintListeners) {
l.accept(paintEvent);
}
}
}
@Override
public void onAcceleratedPaint(CefBrowser browser, boolean popup, Rectangle[] dirtyRects, CefAcceleratedPaintInfo info) {
// If window is closing, canvas_ or opengl context could be null
final GLContext context = canvas_ != null ? canvas_.getContext() : null;
if (context == null) {
return;
}
// This result can occur due to GLContext re-initialization when changing displays.
if (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
return;
}
// TODO: Implement renderer handling for accelerated paint
// On Windows, convert the D3D11 shared texture handle using ImportMemoryWin32HandleEXT
// through OpenGL's EXT_external_objects_win32. LWJGL supports it, but I am not familiar with JOGL's API.
// renderer_.onAcceleratedPaint(canvas_.getGL().getGL2(), popup, dirtyRects, info);
context.release();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
canvas_.display();
}
});
if (!onAcceleratedPaintListeners.isEmpty()) {
CefAcceleratedPaintEvent paintEvent =
new CefAcceleratedPaintEvent(browser, popup, dirtyRects, info);
for (Consumer<CefAcceleratedPaintEvent> l : onAcceleratedPaintListeners) {
l.accept(paintEvent);
}
}
}
@Override
public boolean onCursorChange(CefBrowser browser, final int cursorType) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
canvas_.setCursor(new Cursor(cursorType));
}
});
// OSR always handles the cursor change.
return true;
}
private static final class SyntheticDragGestureRecognizer extends DragGestureRecognizer {
public SyntheticDragGestureRecognizer(Component c, int action, MouseEvent triggerEvent) {
super(new DragSource(), c, action);
appendEvent(triggerEvent);
}
protected void registerListeners() {}
protected void unregisterListeners() {}
};
private static int getDndAction(int mask) {
// Default to copy if multiple operations are specified.
int action = DnDConstants.ACTION_NONE;
if ((mask & CefDragData.DragOperations.DRAG_OPERATION_COPY)
== CefDragData.DragOperations.DRAG_OPERATION_COPY) {
action = DnDConstants.ACTION_COPY;
} else if ((mask & CefDragData.DragOperations.DRAG_OPERATION_MOVE)
== CefDragData.DragOperations.DRAG_OPERATION_MOVE) {
action = DnDConstants.ACTION_MOVE;
} else if ((mask & CefDragData.DragOperations.DRAG_OPERATION_LINK)
== CefDragData.DragOperations.DRAG_OPERATION_LINK) {
action = DnDConstants.ACTION_LINK;
}
return action;
}
@Override
public boolean startDragging(CefBrowser browser, CefDragData dragData, int mask, int x, int y) {
int action = getDndAction(mask);
MouseEvent triggerEvent =
new MouseEvent(canvas_, MouseEvent.MOUSE_DRAGGED, 0, 0, x, y, 0, false);
DragGestureEvent ev = new DragGestureEvent(
new SyntheticDragGestureRecognizer(canvas_, action, triggerEvent), action,
new Point(x, y), new ArrayList<>(Arrays.asList(triggerEvent)));
DragSource.getDefaultDragSource().startDrag(ev, /*dragCursor=*/null,
new StringSelection(dragData.getFragmentText()), new DragSourceAdapter() {
@Override
public void dragDropEnd(DragSourceDropEvent dsde) {
dragSourceEndedAt(dsde.getLocation(), action);
dragSourceSystemDragEnded();
}
});
return true;
}
@Override
public void updateDragCursor(CefBrowser browser, int operation) {
// TODO: Consider calling onCursorChange() if we want different cursors based on
// |operation|.
}
private void createBrowserIfRequired(boolean hasParent) {
long windowHandle = 0;
if (hasParent) {
windowHandle = getWindowHandle();
}
if (getNativeRef("CefBrowser") == 0) {
if (getParentBrowser() != null) {
createDevTools(getParentBrowser(), getClient(), windowHandle, true, isTransparent_,
null, getInspectAt());
} else {
createBrowser(getClient(), windowHandle, getUrl(), true, isTransparent_, null,
getRequestContext());
}
} else if (hasParent && justCreated_) {
notifyAfterParentChanged();
setFocus(true);
justCreated_ = false;
}
}
private void notifyAfterParentChanged() {
// With OSR there is no native window to reparent but we still need to send the
// notification.
getClient().onAfterParentChanged(this);
}
@Override
public boolean getScreenInfo(CefBrowser browser, CefScreenInfo screenInfo) {
screenInfo.Set(scaleFactor_, depth, depth_per_component, false, browser_rect_.getBounds(),
browser_rect_.getBounds());
return true;
}
@Override
public CompletableFuture<BufferedImage> createScreenshot(boolean nativeResolution) {
int width = (int) Math.ceil(canvas_.getWidth() * scaleFactor_);
int height = (int) Math.ceil(canvas_.getHeight() * scaleFactor_);
// In order to grab a screenshot of the browser window, we need to get the OpenGL internals
// from the GLCanvas that displays the browser.
GL2 gl = canvas_.getGL().getGL2();
int textureId = renderer_.getTextureID();
// This mirrors the two ways in which CefRenderer may render images internally - either via
// an incrementally updated texture that is the same size as the window and simply rendered
// onto a textured quad by graphics hardware, in which case we capture the data directly
// from this texture, or by directly writing pixels into the OpenGL framebuffer, in which
// case we directly read those pixels back. The latter is the way chosen if there is no
// hardware rasterizer capability detected. We can simply distinguish both approaches by
// looking whether the textureId of the renderer is a valid (non-zero) one.
boolean useReadPixels = (textureId == 0);
// This Callable encapsulates the pixel-reading code. After running it, the screenshot
// BufferedImage contains the grabbed image.
final Callable<BufferedImage> pixelGrabberCallable = new Callable<BufferedImage>() {
@Override
public BufferedImage call() {
BufferedImage screenshot =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
ByteBuffer buffer = GLBuffers.newDirectByteBuffer(width * height * 4);
gl.getContext().makeCurrent();
try {
if (useReadPixels) {
// If pixels are copied directly to the framebuffer, we also directly read
// them back.
gl.glReadPixels(
0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buffer);
} else {
// In this case, read the texture pixel data from the previously-retrieved
// texture ID
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, textureId);
gl.glGetTexImage(
GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buffer);
gl.glDisable(GL.GL_TEXTURE_2D);
}
} finally {
gl.getContext().release();
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// The OpenGL functions only support RGBA, while Java BufferedImage uses
// ARGB. We must convert.
int r = (buffer.get() & 0xff);
int g = (buffer.get() & 0xff);
int b = (buffer.get() & 0xff);
int a = (buffer.get() & 0xff);
int argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
// If pixels were read from the framebuffer, we have to flip the resulting
// image on the Y axis, as the OpenGL framebuffer's y axis starts at the
// bottom of the image pointing "upwards", while BufferedImage has the
// origin in the upper left corner. This flipping is done when drawing into
// the BufferedImage.
screenshot.setRGB(x, useReadPixels ? (height - y - 1) : y, argb);
}
}
if (!nativeResolution && scaleFactor_ != 1.0) {
// HiDPI images should be resized down to "normal" levels
BufferedImage resized =
new BufferedImage((int) (screenshot.getWidth() / scaleFactor_),
(int) (screenshot.getHeight() / scaleFactor_),
BufferedImage.TYPE_INT_ARGB);
AffineTransform tempTransform = new AffineTransform();
tempTransform.scale(1.0 / scaleFactor_, 1.0 / scaleFactor_);
AffineTransformOp tempScaleOperation =
new AffineTransformOp(tempTransform, AffineTransformOp.TYPE_BILINEAR);
resized = tempScaleOperation.filter(screenshot, resized);
return resized;
} else {
return screenshot;
}
}
};
if (SwingUtilities.isEventDispatchThread()) {
// If called on the AWT event thread, just access the GL API
try {
BufferedImage screenshot = pixelGrabberCallable.call();
return CompletableFuture.completedFuture(screenshot);
} catch (Exception e) {
CompletableFuture<BufferedImage> future = new CompletableFuture<BufferedImage>();
future.completeExceptionally(e);
return future;
}
} else {
// If called from another thread, register a GLEventListener and trigger an async
// redraw, during which we use the GL API to grab the pixel data. An unresolved Future
// is returned, on which the caller can wait for a result (but not with the Event
// Thread, as we need that for pixel grabbing, which is why there's a safeguard in place
// to catch that situation if it accidentally happens).
CompletableFuture<BufferedImage> future = new CompletableFuture<BufferedImage>() {
private void safeguardGet() {
if (SwingUtilities.isEventDispatchThread()) {
throw new RuntimeException(
"Waiting on this Future using the AWT Event Thread is illegal, "
+ "because it can potentially deadlock the thread.");
}
}
@Override
public BufferedImage get() throws InterruptedException, ExecutionException {
safeguardGet();
return super.get();
}
@Override
public BufferedImage get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
safeguardGet();
return super.get(timeout, unit);
}
};
canvas_.addGLEventListener(new GLEventListener() {
@Override
public void reshape(
GLAutoDrawable aDrawable, int aArg1, int aArg2, int aArg3, int aArg4) {
// ignore
}
@Override
public void init(GLAutoDrawable aDrawable) {
// ignore
}
@Override
public void dispose(GLAutoDrawable aDrawable) {
// ignore
}
@Override
public void display(GLAutoDrawable aDrawable) {
canvas_.removeGLEventListener(this);
try {
future.complete(pixelGrabberCallable.call());
} catch (Exception e) {
future.completeExceptionally(e);
}
}
});
// This repaint triggers an indirect call to the listeners' display method above, which
// ultimately completes the future that we return immediately.
canvas_.repaint();
return future;
}
}
}