OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "config.h" |
| 6 #include "web/PageOverlay.h" |
| 7 |
| 8 #include "core/frame/FrameView.h" |
| 9 #include "core/rendering/RenderView.h" |
| 10 #include "platform/graphics/Color.h" |
| 11 #include "platform/graphics/GraphicsContext.h" |
| 12 #include "platform/graphics/paint/DisplayItemList.h" |
| 13 #include "platform/graphics/paint/DrawingRecorder.h" |
| 14 #include "public/platform/Platform.h" |
| 15 #include "public/platform/WebCanvas.h" |
| 16 #include "public/platform/WebThread.h" |
| 17 #include "public/web/WebPageOverlay.h" |
| 18 #include "public/web/WebSettings.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "third_party/skia/include/core/SkCanvas.h" |
| 21 #include "third_party/skia/include/core/SkColor.h" |
| 22 #include "third_party/skia/include/core/SkPaint.h" |
| 23 #include "web/WebGraphicsContextImpl.h" |
| 24 #include "web/WebLocalFrameImpl.h" |
| 25 #include "web/WebViewImpl.h" |
| 26 #include "web/tests/FrameTestHelpers.h" |
| 27 #include <gmock/gmock.h> |
| 28 #include <gtest/gtest.h> |
| 29 |
| 30 using testing::_; |
| 31 using testing::AtLeast; |
| 32 using testing::Property; |
| 33 |
| 34 namespace blink { |
| 35 namespace { |
| 36 |
| 37 static const int viewportWidth = 800; |
| 38 static const int viewportHeight = 600; |
| 39 |
| 40 // These unit tests cover both PageOverlay and PageOverlayList. |
| 41 |
| 42 void enableAcceleratedCompositing(WebSettings* settings) |
| 43 { |
| 44 settings->setAcceleratedCompositingEnabled(true); |
| 45 } |
| 46 |
| 47 void disableAcceleratedCompositing(WebSettings* settings) |
| 48 { |
| 49 settings->setAcceleratedCompositingEnabled(false); |
| 50 } |
| 51 |
| 52 class PageOverlayTest : public ::testing::Test { |
| 53 protected: |
| 54 enum CompositingMode { AcceleratedCompositing, UnacceleratedCompositing }; |
| 55 |
| 56 void initialize(CompositingMode compositingMode) |
| 57 { |
| 58 m_helper.initialize( |
| 59 false /* enableJavascript */, nullptr /* webFrameClient */, nullptr
/* webViewClient */, |
| 60 compositingMode == AcceleratedCompositing ? enableAcceleratedComposi
ting : disableAcceleratedCompositing); |
| 61 webViewImpl()->resize(WebSize(viewportWidth, viewportHeight)); |
| 62 ASSERT_EQ(compositingMode == AcceleratedCompositing, webViewImpl()->isAc
celeratedCompositingActive()); |
| 63 ASSERT_TRUE(!webViewImpl()->pageOverlays() || webViewImpl()->pageOverlay
s()->empty()); |
| 64 } |
| 65 |
| 66 WebViewImpl* webViewImpl() const { return m_helper.webViewImpl(); } |
| 67 |
| 68 template <typename OverlayType> |
| 69 void runPageOverlayTestWithUnacceleratedCompositing(); |
| 70 |
| 71 template <typename OverlayType> |
| 72 void runPageOverlayTestWithAcceleratedCompositing(); |
| 73 |
| 74 private: |
| 75 FrameTestHelpers::WebViewHelper m_helper; |
| 76 }; |
| 77 |
| 78 // WebPageOverlay that uses a WebCanvas to draw a solid color. |
| 79 class SimpleCanvasOverlay : public WebPageOverlay { |
| 80 public: |
| 81 SimpleCanvasOverlay(SkColor color) : m_color(color) { } |
| 82 |
| 83 void paintPageOverlay(WebGraphicsContext* context, const WebSize& size) over
ride |
| 84 { |
| 85 WebFloatRect rect(0, 0, size.width, size.height); |
| 86 WebCanvas* canvas = context->beginDrawing(rect); |
| 87 SkPaint paint; |
| 88 paint.setColor(m_color); |
| 89 paint.setStyle(SkPaint::kFill_Style); |
| 90 canvas->drawRectCoords(0, 0, size.width, size.height, paint); |
| 91 context->endDrawing(); |
| 92 } |
| 93 |
| 94 private: |
| 95 SkColor m_color; |
| 96 }; |
| 97 |
| 98 // WebPageOverlay that uses the underlying blink::GraphicsContext to paint a |
| 99 // solid color. |
| 100 class PrivateGraphicsContextOverlay : public WebPageOverlay { |
| 101 public: |
| 102 PrivateGraphicsContextOverlay(Color color) : m_color(color) { } |
| 103 |
| 104 void paintPageOverlay(WebGraphicsContext* context, const WebSize& size) over
ride |
| 105 { |
| 106 GraphicsContext& graphicsContext = toWebGraphicsContextImpl(context)->gr
aphicsContext(); |
| 107 FloatRect rect(0, 0, size.width, size.height); |
| 108 DrawingRecorder drawingRecorder(&graphicsContext, toDisplayItemClient(th
is), DisplayItem::PageOverlay, rect); |
| 109 graphicsContext.fillRect(rect, m_color); |
| 110 } |
| 111 |
| 112 private: |
| 113 Color m_color; |
| 114 }; |
| 115 |
| 116 template <bool(*getter)(), void(*setter)(bool)> |
| 117 class RuntimeFeatureChange { |
| 118 public: |
| 119 RuntimeFeatureChange(bool newValue) : m_oldValue(getter()) { setter(newValue
); } |
| 120 ~RuntimeFeatureChange() { setter(m_oldValue); } |
| 121 private: |
| 122 bool m_oldValue; |
| 123 }; |
| 124 using SlimmingPaintScope = RuntimeFeatureChange<&RuntimeEnabledFeatures::slimmin
gPaintEnabled, RuntimeEnabledFeatures::setSlimmingPaintEnabled>; |
| 125 |
| 126 class MockCanvas : public SkCanvas { |
| 127 public: |
| 128 MockCanvas(int width, int height) : SkCanvas(width, height) { } |
| 129 MOCK_METHOD2(onDrawRect, void(const SkRect&, const SkPaint&)); |
| 130 }; |
| 131 |
| 132 template <typename OverlayType> |
| 133 void PageOverlayTest::runPageOverlayTestWithUnacceleratedCompositing() |
| 134 { |
| 135 initialize(UnacceleratedCompositing); |
| 136 |
| 137 OverlayType overlay(SK_ColorYELLOW); |
| 138 webViewImpl()->addPageOverlay(&overlay, 0 /* zOrder */); |
| 139 EXPECT_TRUE(webViewImpl()->pageOverlays() && !webViewImpl()->pageOverlays()-
>empty()); |
| 140 webViewImpl()->layout(); |
| 141 |
| 142 MockCanvas canvas(viewportWidth, viewportHeight); |
| 143 EXPECT_CALL(canvas, onDrawRect(_, _)).Times(AtLeast(1)); |
| 144 EXPECT_CALL(canvas, onDrawRect(SkRect::MakeWH(viewportWidth, viewportHeight)
, Property(&SkPaint::getColor, SK_ColorYELLOW))); |
| 145 webViewImpl()->paint(&canvas, WebRect(0, 0, viewportWidth, viewportHeight)); |
| 146 } |
| 147 |
| 148 TEST_F(PageOverlayTest, SimpleCanvasOverlay_UnacceleratedCompositing_NoSlimmingP
aint) |
| 149 { |
| 150 SlimmingPaintScope slimmingPaintEnabled(false); |
| 151 runPageOverlayTestWithUnacceleratedCompositing<SimpleCanvasOverlay>(); |
| 152 } |
| 153 |
| 154 TEST_F(PageOverlayTest, SimpleCanvasOverlay_UnacceleratedCompositing_SlimmingPai
nt) |
| 155 { |
| 156 SlimmingPaintScope slimmingPaintEnabled(true); |
| 157 runPageOverlayTestWithUnacceleratedCompositing<SimpleCanvasOverlay>(); |
| 158 } |
| 159 |
| 160 TEST_F(PageOverlayTest, PrivateGraphicsContextOverlay_UnacceleratedCompositing_N
oSlimmingPaint) |
| 161 { |
| 162 SlimmingPaintScope slimmingPaintEnabled(false); |
| 163 runPageOverlayTestWithUnacceleratedCompositing<PrivateGraphicsContextOverlay
>(); |
| 164 } |
| 165 |
| 166 TEST_F(PageOverlayTest, PrivateGraphicsContextOverlay_UnacceleratedCompositing_S
limmingPaint) |
| 167 { |
| 168 SlimmingPaintScope slimmingPaintEnabled(true); |
| 169 runPageOverlayTestWithUnacceleratedCompositing<PrivateGraphicsContextOverlay
>(); |
| 170 } |
| 171 |
| 172 template <typename OverlayType> |
| 173 void PageOverlayTest::runPageOverlayTestWithAcceleratedCompositing() |
| 174 { |
| 175 initialize(AcceleratedCompositing); |
| 176 webViewImpl()->layerTreeView()->setViewportSize(WebSize(viewportWidth, viewp
ortHeight)); |
| 177 |
| 178 OverlayType overlay(SK_ColorYELLOW); |
| 179 webViewImpl()->addPageOverlay(&overlay, 0 /* zOrder */); |
| 180 EXPECT_TRUE(webViewImpl()->pageOverlays() && !webViewImpl()->pageOverlays()-
>empty()); |
| 181 webViewImpl()->layout(); |
| 182 |
| 183 // Ideally, we would get results from the compositor that showed that this |
| 184 // page overlay actually winds up getting drawn on top of the rest. |
| 185 // For now, we just check that the GraphicsLayer will draw the right thing. |
| 186 |
| 187 MockCanvas canvas(viewportWidth, viewportHeight); |
| 188 EXPECT_CALL(canvas, onDrawRect(_, _)).Times(AtLeast(0)); |
| 189 EXPECT_CALL(canvas, onDrawRect(SkRect::MakeWH(viewportWidth, viewportHeight)
, Property(&SkPaint::getColor, SK_ColorYELLOW))); |
| 190 |
| 191 GraphicsLayer* graphicsLayer = webViewImpl()->pageOverlays()->graphicsLayerF
orTesting(); |
| 192 if (RuntimeEnabledFeatures::slimmingPaintEnabled()) { |
| 193 // If slimming paint is on, we paint the layer with a null canvas to get |
| 194 // a display list, and then replay that onto the mock canvas for |
| 195 // examination. This is about as close to the real path as we can easily |
| 196 // get. |
| 197 GraphicsContext graphicsContext(nullptr /* canvas */, graphicsLayer->dis
playItemList()); |
| 198 graphicsContext.setCertainlyOpaque(false); |
| 199 graphicsLayer->paint(graphicsContext, WebRect(0, 0, viewportWidth, viewp
ortHeight)); |
| 200 |
| 201 GraphicsContext replayContext(&canvas, nullptr /* displayItemList */); |
| 202 replayContext.setCertainlyOpaque(false); |
| 203 graphicsLayer->displayItemList()->replay(&replayContext); |
| 204 } else { |
| 205 GraphicsContext graphicsContext(&canvas, nullptr /* displayItemList */); |
| 206 graphicsContext.setCertainlyOpaque(false); |
| 207 graphicsLayer->paint(graphicsContext, WebRect(0, 0, viewportWidth, viewp
ortHeight)); |
| 208 } |
| 209 } |
| 210 |
| 211 TEST_F(PageOverlayTest, SimpleCanvasOverlay_AcceleratedCompositing_NoSlimmingPai
nt) |
| 212 { |
| 213 SlimmingPaintScope slimmingPaintEnabled(false); |
| 214 runPageOverlayTestWithAcceleratedCompositing<SimpleCanvasOverlay>(); |
| 215 } |
| 216 |
| 217 TEST_F(PageOverlayTest, SimpleCanvasOverlay_AcceleratedCompositing_SlimmingPaint
) |
| 218 { |
| 219 SlimmingPaintScope slimmingPaintEnabled(true); |
| 220 runPageOverlayTestWithAcceleratedCompositing<SimpleCanvasOverlay>(); |
| 221 } |
| 222 |
| 223 TEST_F(PageOverlayTest, PrivateGraphicsContextOverlay_AcceleratedCompositing_NoS
limmingPaint) |
| 224 { |
| 225 SlimmingPaintScope slimmingPaintEnabled(false); |
| 226 runPageOverlayTestWithAcceleratedCompositing<PrivateGraphicsContextOverlay>(
); |
| 227 } |
| 228 |
| 229 TEST_F(PageOverlayTest, PrivateGraphicsContextOverlay_AcceleratedCompositing_Sli
mmingPaint) |
| 230 { |
| 231 SlimmingPaintScope slimmingPaintEnabled(true); |
| 232 runPageOverlayTestWithAcceleratedCompositing<PrivateGraphicsContextOverlay>(
); |
| 233 } |
| 234 |
| 235 } // namespace |
| 236 } // namespace blink |
OLD | NEW |