| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "platform/graphics/DeferredImageDecoder.h" | 26 #include "platform/graphics/DeferredImageDecoder.h" |
| 27 | 27 |
| 28 #include "platform/CrossThreadFunctional.h" | 28 #include "platform/CrossThreadFunctional.h" |
| 29 #include "platform/SharedBuffer.h" | 29 #include "platform/SharedBuffer.h" |
| 30 #include "platform/WebTaskRunner.h" | 30 #include "platform/WebTaskRunner.h" |
| 31 #include "platform/graphics/ImageDecodingStore.h" | 31 #include "platform/graphics/ImageDecodingStore.h" |
| 32 #include "platform/graphics/ImageFrameGenerator.h" | 32 #include "platform/graphics/ImageFrameGenerator.h" |
| 33 #include "platform/graphics/paint/PaintCanvas.h" |
| 34 #include "platform/graphics/paint/PaintRecord.h" |
| 35 #include "platform/graphics/paint/PaintRecorder.h" |
| 36 #include "platform/graphics/paint/PaintSurface.h" |
| 33 #include "platform/graphics/test/MockImageDecoder.h" | 37 #include "platform/graphics/test/MockImageDecoder.h" |
| 34 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
| 35 #include "public/platform/WebThread.h" | 39 #include "public/platform/WebThread.h" |
| 36 #include "public/platform/WebTraceLocation.h" | 40 #include "public/platform/WebTraceLocation.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "third_party/skia/include/core/SkCanvas.h" | |
| 39 #include "third_party/skia/include/core/SkImage.h" | 42 #include "third_party/skia/include/core/SkImage.h" |
| 40 #include "third_party/skia/include/core/SkPicture.h" | |
| 41 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 42 #include "third_party/skia/include/core/SkPixmap.h" | 43 #include "third_party/skia/include/core/SkPixmap.h" |
| 43 #include "third_party/skia/include/core/SkSurface.h" | 44 #include "third_party/skia/include/core/SkSurface.h" |
| 44 #include "wtf/PassRefPtr.h" | 45 #include "wtf/PassRefPtr.h" |
| 45 #include "wtf/PtrUtil.h" | 46 #include "wtf/PtrUtil.h" |
| 46 #include "wtf/RefPtr.h" | 47 #include "wtf/RefPtr.h" |
| 47 #include <memory> | 48 #include <memory> |
| 48 | 49 |
| 49 namespace blink { | 50 namespace blink { |
| 50 | 51 |
| 51 namespace { | 52 namespace { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 77 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, | 78 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, |
| 78 0x65, 0x7a, 0x67, 0x69, 0x66, 0x2e, 0x63, 0x6f, 0x6d, 0x20, 0x67, 0x69, | 79 0x65, 0x7a, 0x67, 0x69, 0x66, 0x2e, 0x63, 0x6f, 0x6d, 0x20, 0x67, 0x69, |
| 79 0x66, 0x20, 0x6d, 0x61, 0x6b, 0x65, 0x72, 0x00, 0x2c, 0x00, 0x00, 0x00, | 80 0x66, 0x20, 0x6d, 0x61, 0x6b, 0x65, 0x72, 0x00, 0x2c, 0x00, 0x00, 0x00, |
| 80 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x21, | 81 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x21, |
| 81 0xf9, 0x04, 0x00, 0x14, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, | 82 0xf9, 0x04, 0x00, 0x14, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, |
| 82 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, 0x01, 0x00, 0x3b, | 83 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, 0x01, 0x00, 0x3b, |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 struct Rasterizer { | 86 struct Rasterizer { |
| 86 SkCanvas* canvas; | 87 SkCanvas* canvas; |
| 87 SkPicture* picture; | 88 PaintRecord* picture; |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 } // namespace | 91 } // namespace |
| 91 | 92 |
| 92 class DeferredImageDecoderTest : public ::testing::Test, | 93 class DeferredImageDecoderTest : public ::testing::Test, |
| 93 public MockImageDecoderClient { | 94 public MockImageDecoderClient { |
| 94 public: | 95 public: |
| 95 void SetUp() override { | 96 void SetUp() override { |
| 96 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); | 97 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); |
| 97 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); | 98 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); |
| 98 m_frameCount = 1; | 99 m_frameCount = 1; |
| 99 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); | 100 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); |
| 100 m_actualDecoder = decoder.get(); | 101 m_actualDecoder = decoder.get(); |
| 101 m_actualDecoder->setSize(1, 1); | 102 m_actualDecoder->setSize(1, 1); |
| 102 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); | 103 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); |
| 103 m_surface = SkSurface::MakeRasterN32Premul(100, 100); | 104 m_surface = PaintSurface::MakeRasterN32Premul(100, 100); |
| 104 ASSERT_TRUE(m_surface.get()); | 105 ASSERT_TRUE(m_surface.get()); |
| 105 m_decodeRequestCount = 0; | 106 m_decodeRequestCount = 0; |
| 106 m_repetitionCount = cAnimationNone; | 107 m_repetitionCount = cAnimationNone; |
| 107 m_status = ImageFrame::FrameComplete; | 108 m_status = ImageFrame::FrameComplete; |
| 108 m_frameDuration = 0; | 109 m_frameDuration = 0; |
| 109 m_decodedSize = m_actualDecoder->size(); | 110 m_decodedSize = m_actualDecoder->size(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 void TearDown() override { ImageDecodingStore::instance().clear(); } | 113 void TearDown() override { ImageDecodingStore::instance().clear(); } |
| 113 | 114 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 127 | 128 |
| 128 protected: | 129 protected: |
| 129 void useMockImageDecoderFactory() { | 130 void useMockImageDecoderFactory() { |
| 130 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( | 131 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( |
| 131 MockImageDecoderFactory::create(this, m_decodedSize)); | 132 MockImageDecoderFactory::create(this, m_decodedSize)); |
| 132 } | 133 } |
| 133 | 134 |
| 134 // Don't own this but saves the pointer to query states. | 135 // Don't own this but saves the pointer to query states. |
| 135 MockImageDecoder* m_actualDecoder; | 136 MockImageDecoder* m_actualDecoder; |
| 136 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; | 137 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; |
| 137 sk_sp<SkSurface> m_surface; | 138 sk_sp<PaintSurface> m_surface; |
| 138 int m_decodeRequestCount; | 139 int m_decodeRequestCount; |
| 139 RefPtr<SharedBuffer> m_data; | 140 RefPtr<SharedBuffer> m_data; |
| 140 size_t m_frameCount; | 141 size_t m_frameCount; |
| 141 int m_repetitionCount; | 142 int m_repetitionCount; |
| 142 ImageFrame::Status m_status; | 143 ImageFrame::Status m_status; |
| 143 float m_frameDuration; | 144 float m_frameDuration; |
| 144 IntSize m_decodedSize; | 145 IntSize m_decodedSize; |
| 145 }; | 146 }; |
| 146 | 147 |
| 147 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) { | 148 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) { |
| 148 m_lazyDecoder->setData(m_data, true); | 149 m_lazyDecoder->setData(m_data, true); |
| 149 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 150 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
| 150 ASSERT_TRUE(image); | 151 ASSERT_TRUE(image); |
| 151 EXPECT_EQ(1, image->width()); | 152 EXPECT_EQ(1, image->width()); |
| 152 EXPECT_EQ(1, image->height()); | 153 EXPECT_EQ(1, image->height()); |
| 153 | 154 |
| 154 SkPictureRecorder recorder; | 155 PaintRecorder recorder; |
| 155 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 156 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
| 156 tempCanvas->drawImage(image.get(), 0, 0); | 157 tempCanvas->drawImage(image.get(), 0, 0); |
| 157 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); | 158 sk_sp<PaintRecord> picture = recorder.finishRecordingAsPicture(); |
| 158 EXPECT_EQ(0, m_decodeRequestCount); | 159 EXPECT_EQ(0, m_decodeRequestCount); |
| 159 | 160 |
| 160 m_surface->getCanvas()->drawPicture(picture); | 161 m_surface->getCanvas()->drawPicture(picture); |
| 161 EXPECT_EQ(0, m_decodeRequestCount); | 162 EXPECT_EQ(0, m_decodeRequestCount); |
| 162 | 163 |
| 163 SkBitmap canvasBitmap; | 164 SkBitmap canvasBitmap; |
| 164 canvasBitmap.allocN32Pixels(100, 100); | 165 canvasBitmap.allocN32Pixels(100, 100); |
| 165 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 166 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
| 166 SkAutoLockPixels autoLock(canvasBitmap); | 167 SkAutoLockPixels autoLock(canvasBitmap); |
| 167 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 168 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
| 168 } | 169 } |
| 169 | 170 |
| 170 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) { | 171 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) { |
| 171 RefPtr<SharedBuffer> partialData = | 172 RefPtr<SharedBuffer> partialData = |
| 172 SharedBuffer::create(m_data->data(), m_data->size() - 10); | 173 SharedBuffer::create(m_data->data(), m_data->size() - 10); |
| 173 | 174 |
| 174 // Received only half the file. | 175 // Received only half the file. |
| 175 m_lazyDecoder->setData(partialData, false); | 176 m_lazyDecoder->setData(partialData, false); |
| 176 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 177 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
| 177 ASSERT_TRUE(image); | 178 ASSERT_TRUE(image); |
| 178 SkPictureRecorder recorder; | 179 PaintRecorder recorder; |
| 179 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 180 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
| 180 tempCanvas->drawImage(image.get(), 0, 0); | 181 tempCanvas->drawImage(image.get(), 0, 0); |
| 181 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); | 182 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); |
| 182 | 183 |
| 183 // Fully received the file and draw the SkPicture again. | 184 // Fully received the file and draw the PaintRecord again. |
| 184 m_lazyDecoder->setData(m_data, true); | 185 m_lazyDecoder->setData(m_data, true); |
| 185 image = m_lazyDecoder->createFrameAtIndex(0); | 186 image = m_lazyDecoder->createFrameAtIndex(0); |
| 186 ASSERT_TRUE(image); | 187 ASSERT_TRUE(image); |
| 187 tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 188 tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
| 188 tempCanvas->drawImage(image.get(), 0, 0); | 189 tempCanvas->drawImage(image.get(), 0, 0); |
| 189 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); | 190 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); |
| 190 | 191 |
| 191 SkBitmap canvasBitmap; | 192 SkBitmap canvasBitmap; |
| 192 canvasBitmap.allocN32Pixels(100, 100); | 193 canvasBitmap.allocN32Pixels(100, 100); |
| 193 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 194 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
| 194 SkAutoLockPixels autoLock(canvasBitmap); | 195 SkAutoLockPixels autoLock(canvasBitmap); |
| 195 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 196 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
| 196 } | 197 } |
| 197 | 198 |
| 198 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) { | 199 static void rasterizeMain(PaintCanvas* canvas, PaintRecord* picture) { |
| 199 canvas->drawPicture(picture); | 200 canvas->drawPicture(picture); |
| 200 } | 201 } |
| 201 | 202 |
| 202 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { | 203 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { |
| 203 m_lazyDecoder->setData(m_data, true); | 204 m_lazyDecoder->setData(m_data, true); |
| 204 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 205 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
| 205 ASSERT_TRUE(image); | 206 ASSERT_TRUE(image); |
| 206 EXPECT_EQ(1, image->width()); | 207 EXPECT_EQ(1, image->width()); |
| 207 EXPECT_EQ(1, image->height()); | 208 EXPECT_EQ(1, image->height()); |
| 208 | 209 |
| 209 SkPictureRecorder recorder; | 210 PaintRecorder recorder; |
| 210 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 211 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
| 211 tempCanvas->drawImage(image.get(), 0, 0); | 212 tempCanvas->drawImage(image.get(), 0, 0); |
| 212 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); | 213 sk_sp<PaintRecord> picture = recorder.finishRecordingAsPicture(); |
| 213 EXPECT_EQ(0, m_decodeRequestCount); | 214 EXPECT_EQ(0, m_decodeRequestCount); |
| 214 | 215 |
| 215 // Create a thread to rasterize SkPicture. | 216 // Create a thread to rasterize PaintRecord. |
| 216 std::unique_ptr<WebThread> thread = | 217 std::unique_ptr<WebThread> thread = |
| 217 WTF::wrapUnique(Platform::current()->createThread("RasterThread")); | 218 WTF::wrapUnique(Platform::current()->createThread("RasterThread")); |
| 218 thread->getWebTaskRunner()->postTask( | 219 thread->getWebTaskRunner()->postTask( |
| 219 BLINK_FROM_HERE, | 220 BLINK_FROM_HERE, |
| 220 crossThreadBind(&rasterizeMain, | 221 crossThreadBind(&rasterizeMain, |
| 221 crossThreadUnretained(m_surface->getCanvas()), | 222 crossThreadUnretained(m_surface->getCanvas()), |
| 222 crossThreadUnretained(picture.get()))); | 223 crossThreadUnretained(picture.get()))); |
| 223 thread.reset(); | 224 thread.reset(); |
| 224 EXPECT_EQ(0, m_decodeRequestCount); | 225 EXPECT_EQ(0, m_decodeRequestCount); |
| 225 | 226 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 m_decodedSize = IntSize(22, 33); | 300 m_decodedSize = IntSize(22, 33); |
| 300 m_lazyDecoder->setData(m_data, true); | 301 m_lazyDecoder->setData(m_data, true); |
| 301 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 302 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
| 302 ASSERT_TRUE(image); | 303 ASSERT_TRUE(image); |
| 303 EXPECT_EQ(m_decodedSize.width(), image->width()); | 304 EXPECT_EQ(m_decodedSize.width(), image->width()); |
| 304 EXPECT_EQ(m_decodedSize.height(), image->height()); | 305 EXPECT_EQ(m_decodedSize.height(), image->height()); |
| 305 | 306 |
| 306 useMockImageDecoderFactory(); | 307 useMockImageDecoderFactory(); |
| 307 | 308 |
| 308 // The following code should not fail any assert. | 309 // The following code should not fail any assert. |
| 309 SkPictureRecorder recorder; | 310 PaintRecorder recorder; |
| 310 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 311 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
| 311 tempCanvas->drawImage(image.get(), 0, 0); | 312 tempCanvas->drawImage(image.get(), 0, 0); |
| 312 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); | 313 sk_sp<PaintRecord> picture = recorder.finishRecordingAsPicture(); |
| 313 EXPECT_EQ(0, m_decodeRequestCount); | 314 EXPECT_EQ(0, m_decodeRequestCount); |
| 314 m_surface->getCanvas()->drawPicture(picture); | 315 m_surface->getCanvas()->drawPicture(picture); |
| 315 EXPECT_EQ(1, m_decodeRequestCount); | 316 EXPECT_EQ(1, m_decodeRequestCount); |
| 316 } | 317 } |
| 317 | 318 |
| 318 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { | 319 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { |
| 319 m_frameCount = 1; | 320 m_frameCount = 1; |
| 320 m_lazyDecoder->setData(m_data, false); | 321 m_lazyDecoder->setData(m_data, false); |
| 321 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 322 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
| 322 m_frameCount = 2; | 323 m_frameCount = 2; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 SharedBuffer::create(m_data->data(), m_data->size()); | 381 SharedBuffer::create(m_data->data(), m_data->size()); |
| 381 EXPECT_EQ(originalData->size(), m_data->size()); | 382 EXPECT_EQ(originalData->size(), m_data->size()); |
| 382 m_lazyDecoder->setData(originalData, false); | 383 m_lazyDecoder->setData(originalData, false); |
| 383 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); | 384 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); |
| 384 EXPECT_EQ(originalData->size(), newData->size()); | 385 EXPECT_EQ(originalData->size(), newData->size()); |
| 385 EXPECT_EQ( | 386 EXPECT_EQ( |
| 386 0, std::memcmp(originalData->data(), newData->data(), newData->size())); | 387 0, std::memcmp(originalData->data(), newData->data(), newData->size())); |
| 387 } | 388 } |
| 388 | 389 |
| 389 } // namespace blink | 390 } // namespace blink |
| OLD | NEW |