1 | /* |
2 | * Copyright (C) 2017 Metrological Group B.V. |
3 | * Copyright (C) 2017 Igalia S.L. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following |
13 | * disclaimer in the documentation and/or other materials provided |
14 | * with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | #include "NicosiaPaintingEngineThreaded.h" |
31 | |
32 | #if USE(COORDINATED_GRAPHICS) |
33 | |
34 | #include "GraphicsContext.h" |
35 | #include "GraphicsLayer.h" |
36 | #include "NicosiaBuffer.h" |
37 | #include "NicosiaPaintingContext.h" |
38 | |
39 | namespace Nicosia { |
40 | using namespace WebCore; |
41 | |
42 | static void paintLayer(GraphicsContext& context, GraphicsLayer& layer, const IntRect& sourceRect, const IntRect& mappedSourceRect, const IntRect& targetRect, float contentsScale, bool supportsAlpha) |
43 | { |
44 | context.save(); |
45 | context.clip(targetRect); |
46 | context.translate(targetRect.x(), targetRect.y()); |
47 | |
48 | if (supportsAlpha) { |
49 | context.setCompositeOperation(CompositeCopy); |
50 | context.fillRect(IntRect(IntPoint::zero(), sourceRect.size()), Color::transparent); |
51 | context.setCompositeOperation(CompositeSourceOver); |
52 | } |
53 | |
54 | context.translate(-sourceRect.x(), -sourceRect.y()); |
55 | context.scale(FloatSize(contentsScale, contentsScale)); |
56 | |
57 | layer.paintGraphicsLayerContents(context, mappedSourceRect); |
58 | |
59 | context.restore(); |
60 | } |
61 | |
62 | PaintingEngineThreaded::PaintingEngineThreaded(unsigned numThreads) |
63 | : m_workerPool(WorkerPool::create("PaintingThread"_s , numThreads)) |
64 | { |
65 | } |
66 | |
67 | PaintingEngineThreaded::~PaintingEngineThreaded() |
68 | { |
69 | } |
70 | |
71 | bool PaintingEngineThreaded::paint(GraphicsLayer& layer, Ref<Buffer>&& buffer, const IntRect& sourceRect, const IntRect& mappedSourceRect, const IntRect& targetRect, float contentsScale) |
72 | { |
73 | buffer->beginPainting(); |
74 | |
75 | PaintingOperations paintingOperations; |
76 | PaintingContext::record(paintingOperations, |
77 | [&](GraphicsContext& context) |
78 | { |
79 | paintLayer(context, layer, sourceRect, mappedSourceRect, targetRect, contentsScale, buffer->supportsAlpha()); |
80 | }); |
81 | |
82 | m_workerPool->postTask([paintingOperations = WTFMove(paintingOperations), buffer = WTFMove(buffer)] { |
83 | PaintingContext::replay(buffer, paintingOperations); |
84 | buffer->completePainting(); |
85 | }); |
86 | |
87 | return true; |
88 | } |
89 | |
90 | } // namespace Nicosia |
91 | |
92 | #endif // USE(COORDINATED_GRAPHICS) |
93 | |