| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/graphics/RecordingImageBufferSurface.h" | 5 #include "platform/graphics/RecordingImageBufferSurface.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include "platform/WebTaskRunner.h" | 8 #include "platform/WebTaskRunner.h" |
| 9 #include "platform/graphics/GraphicsContext.h" | 9 #include "platform/graphics/GraphicsContext.h" |
| 10 #include "platform/graphics/ImageBuffer.h" | 10 #include "platform/graphics/ImageBuffer.h" |
| 11 #include "platform/graphics/ImageBufferClient.h" | 11 #include "platform/graphics/ImageBufferClient.h" |
| 12 #include "platform/graphics/UnacceleratedImageBufferSurface.h" | 12 #include "platform/graphics/UnacceleratedImageBufferSurface.h" |
| 13 #include "platform/graphics/paint/PaintCanvas.h" | 13 #include "platform/graphics/paint/PaintCanvas.h" |
| 14 #include "platform/graphics/paint/PaintRecord.h" | 14 #include "platform/graphics/paint/PaintRecord.h" |
| 15 #include "platform/testing/TestingPlatformSupport.h" | 15 #include "platform/testing/TestingPlatformSupport.h" |
| 16 #include "platform/wtf/PtrUtil.h" | 16 #include "platform/wtf/PtrUtil.h" |
| 17 #include "platform/wtf/RefPtr.h" | 17 #include "platform/wtf/RefPtr.h" |
| 18 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 19 #include "public/platform/WebThread.h" | 19 #include "public/platform/WebThread.h" |
| 20 #include "public/platform/WebTraceLocation.h" | 20 #include "public/platform/WebTraceLocation.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 using testing::Test; | 24 using testing::Test; |
| 25 | 25 |
| 26 namespace blink { | 26 namespace blink { |
| 27 | 27 |
| 28 class MockSurfaceFactory : public RecordingImageBufferFallbackSurfaceFactory { | |
| 29 public: | |
| 30 MockSurfaceFactory() : create_surface_count_(0) {} | |
| 31 | |
| 32 virtual std::unique_ptr<ImageBufferSurface> CreateSurface( | |
| 33 const IntSize& size, | |
| 34 OpacityMode opacity_mode, | |
| 35 const CanvasColorParams& color_params) { | |
| 36 create_surface_count_++; | |
| 37 return WTF::WrapUnique(new UnacceleratedImageBufferSurface( | |
| 38 size, opacity_mode, kInitializeImagePixels, color_params)); | |
| 39 } | |
| 40 | |
| 41 virtual ~MockSurfaceFactory() {} | |
| 42 | |
| 43 int CreateSurfaceCount() { return create_surface_count_; } | |
| 44 | |
| 45 private: | |
| 46 int create_surface_count_; | |
| 47 }; | |
| 48 | |
| 49 class RecordingImageBufferSurfaceTest : public Test { | 28 class RecordingImageBufferSurfaceTest : public Test { |
| 50 protected: | 29 protected: |
| 51 RecordingImageBufferSurfaceTest() { | 30 RecordingImageBufferSurfaceTest() { |
| 52 std::unique_ptr<MockSurfaceFactory> surface_factory = | 31 auto test_surface = WTF::MakeUnique<RecordingImageBufferSurface>( |
| 53 WTF::MakeUnique<MockSurfaceFactory>(); | 32 IntSize(10, 10), kNonOpaque); |
| 54 surface_factory_ = surface_factory.get(); | |
| 55 std::unique_ptr<RecordingImageBufferSurface> test_surface = | |
| 56 WTF::WrapUnique(new RecordingImageBufferSurface( | |
| 57 IntSize(10, 10), std::move(surface_factory), kNonOpaque)); | |
| 58 test_surface_ = test_surface.get(); | 33 test_surface_ = test_surface.get(); |
| 59 // We create an ImageBuffer in order for the testSurface to be | 34 // We create an ImageBuffer in order for the |test_surface| to be |
| 60 // properly initialized with a GraphicsContext | 35 // properly initialized with a GraphicsContext |
| 61 image_buffer_ = ImageBuffer::Create(std::move(test_surface)); | 36 image_buffer_ = ImageBuffer::Create(std::move(test_surface)); |
| 62 EXPECT_FALSE(!image_buffer_); | 37 EXPECT_TRUE(image_buffer_); |
| 63 test_surface_->InitializeCurrentFrame(); | |
| 64 } | 38 } |
| 65 | 39 |
| 66 public: | 40 public: |
| 67 RecordingImageBufferSurface* TestSurface() { return test_surface_; } | 41 RecordingImageBufferSurface* TestSurface() { return test_surface_; } |
| 68 int CreateSurfaceCount() { return surface_factory_->CreateSurfaceCount(); } | |
| 69 PaintCanvas* Canvas() { return image_buffer_->Canvas(); } | 42 PaintCanvas* Canvas() { return image_buffer_->Canvas(); } |
| 70 | 43 |
| 71 void ExpectDisplayListEnabled(bool display_list_enabled) { | |
| 72 EXPECT_EQ(display_list_enabled, (bool)test_surface_->current_frame_.get()); | |
| 73 EXPECT_EQ(!display_list_enabled, | |
| 74 (bool)test_surface_->fallback_surface_.get()); | |
| 75 int expected_surface_creation_count = display_list_enabled ? 0 : 1; | |
| 76 EXPECT_EQ(expected_surface_creation_count, | |
| 77 surface_factory_->CreateSurfaceCount()); | |
| 78 } | |
| 79 | |
| 80 private: | 44 private: |
| 81 MockSurfaceFactory* surface_factory_; | |
| 82 RecordingImageBufferSurface* test_surface_; | 45 RecordingImageBufferSurface* test_surface_; |
| 83 std::unique_ptr<ImageBuffer> image_buffer_; | 46 std::unique_ptr<ImageBuffer> image_buffer_; |
| 84 }; | 47 }; |
| 85 | 48 |
| 86 TEST_F(RecordingImageBufferSurfaceTest, testEmptyPicture) { | 49 TEST_F(RecordingImageBufferSurfaceTest, testEmptyPicture) { |
| 87 sk_sp<PaintRecord> record = TestSurface()->GetRecord(); | 50 sk_sp<PaintRecord> record = TestSurface()->GetRecord(); |
| 88 EXPECT_TRUE(record.get()); | 51 EXPECT_TRUE(record.get()); |
| 89 ExpectDisplayListEnabled(true); | 52 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 90 } | 53 } |
| 91 | 54 |
| 92 TEST_F(RecordingImageBufferSurfaceTest, testNoFallbackWithClear) { | 55 TEST_F(RecordingImageBufferSurfaceTest, testNoFallbackWithClear) { |
| 93 TestSurface()->WillOverwriteCanvas(); | 56 TestSurface()->WillOverwriteCanvas(); |
| 94 TestSurface()->GetRecord(); | 57 TestSurface()->GetRecord(); |
| 95 ExpectDisplayListEnabled(true); | 58 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 96 } | 59 } |
| 97 | 60 |
| 98 TEST_F(RecordingImageBufferSurfaceTest, testNonAnimatedCanvasUpdate) { | 61 TEST_F(RecordingImageBufferSurfaceTest, testNonAnimatedCanvasUpdate) { |
| 99 // Acquire picture twice to simulate a static canvas: nothing drawn between | 62 // Acquire picture twice to simulate a static canvas: nothing drawn between |
| 100 // updates. | 63 // updates. |
| 101 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 64 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 102 TestSurface()->GetRecord(); | 65 TestSurface()->GetRecord(); |
| 103 TestSurface()->GetRecord(); | 66 TestSurface()->GetRecord(); |
| 104 ExpectDisplayListEnabled(true); | 67 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 105 } | 68 } |
| 106 | 69 |
| 107 TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithoutClear) { | 70 TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithoutClear) { |
| 108 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 71 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 109 TestSurface()->GetRecord(); | 72 TestSurface()->GetRecord(); |
| 110 EXPECT_EQ(0, CreateSurfaceCount()); | 73 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 111 ExpectDisplayListEnabled(true); // first frame has an implicit clear | |
| 112 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 74 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 113 TestSurface()->GetRecord(); | 75 TestSurface()->GetRecord(); |
| 114 ExpectDisplayListEnabled(false); | 76 EXPECT_FALSE(TestSurface()->IsRecording()); |
| 115 } | 77 } |
| 116 | 78 |
| 117 TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithClear) { | 79 TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithClear) { |
| 118 TestSurface()->GetRecord(); | 80 TestSurface()->GetRecord(); |
| 119 TestSurface()->WillOverwriteCanvas(); | 81 TestSurface()->WillOverwriteCanvas(); |
| 120 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 82 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 121 TestSurface()->GetRecord(); | 83 TestSurface()->GetRecord(); |
| 122 ExpectDisplayListEnabled(true); | 84 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 123 // clear after use | 85 // Clear after use. |
| 124 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 86 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 125 TestSurface()->WillOverwriteCanvas(); | 87 TestSurface()->WillOverwriteCanvas(); |
| 126 TestSurface()->GetRecord(); | 88 TestSurface()->GetRecord(); |
| 127 ExpectDisplayListEnabled(true); | 89 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 128 } | 90 } |
| 129 | 91 |
| 130 TEST_F(RecordingImageBufferSurfaceTest, testClearRect) { | 92 TEST_F(RecordingImageBufferSurfaceTest, testClearRect) { |
| 131 TestSurface()->GetRecord(); | 93 TestSurface()->GetRecord(); |
| 132 PaintFlags clear_flags; | 94 PaintFlags clear_flags; |
| 133 clear_flags.setBlendMode(SkBlendMode::kClear); | 95 clear_flags.setBlendMode(SkBlendMode::kClear); |
| 134 Canvas()->drawRect(SkRect::MakeWH(TestSurface()->size().Width(), | 96 Canvas()->drawRect(SkRect::MakeWH(TestSurface()->size().Width(), |
| 135 TestSurface()->size().Height()), | 97 TestSurface()->size().Height()), |
| 136 clear_flags); | 98 clear_flags); |
| 137 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); | 99 TestSurface()->DidDraw(FloatRect(0, 0, 1, 1)); |
| 138 TestSurface()->GetRecord(); | 100 TestSurface()->GetRecord(); |
| 139 ExpectDisplayListEnabled(true); | 101 EXPECT_TRUE(TestSurface()->IsRecording()); |
| 140 } | 102 } |
| 141 | 103 |
| 142 } // namespace blink | 104 } // namespace blink |
| OLD | NEW |