Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/DeferredImageDecoderTest.cpp

Issue 2523673004: [NOT FOR COMMIT] Fully replace SkCanvas uses.
Patch Set: Support Android build. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 19 matching lines...) Expand all
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/test/MockImageDecoder.h" 33 #include "platform/graphics/test/MockImageDecoder.h"
34 #include "public/platform/Platform.h" 34 #include "public/platform/Platform.h"
35 #include "public/platform/WebThread.h" 35 #include "public/platform/WebThread.h"
36 #include "public/platform/WebTraceLocation.h" 36 #include "public/platform/WebTraceLocation.h"
37 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "third_party/skia/include/core/SkCanvas.h" 38 #include "third_party/skia/include/core/SkCanvas.h"
39 #include "third_party/skia/include/core/SkImage.h" 39 #include "third_party/skia/include/core/SkImage.h"
40 #include "third_party/skia/include/core/SkPicture.h" 40 #include "skia/ext/cdl_canvas.h"
41 #include "third_party/skia/include/core/SkPictureRecorder.h" 41 #include "skia/ext/cdl_surface.h"
42 #include "skia/ext/cdl_picture.h"
43 #include "skia/ext/cdl_picture_recorder.h"
42 #include "third_party/skia/include/core/SkPixmap.h" 44 #include "third_party/skia/include/core/SkPixmap.h"
43 #include "third_party/skia/include/core/SkSurface.h" 45 #include "third_party/skia/include/core/SkSurface.h"
44 #include "wtf/PassRefPtr.h" 46 #include "wtf/PassRefPtr.h"
45 #include "wtf/PtrUtil.h" 47 #include "wtf/PtrUtil.h"
46 #include "wtf/RefPtr.h" 48 #include "wtf/RefPtr.h"
47 #include <memory> 49 #include <memory>
48 50
49 namespace blink { 51 namespace blink {
50 52
51 namespace { 53 namespace {
(...skipping 25 matching lines...) Expand all
77 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 79 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, 80 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, 81 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, 82 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, 83 0xf9, 0x04, 0x00, 0x14, 0x00, 0xff, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
82 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, 0x01, 0x00, 0x3b, 84 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, 0x01, 0x00, 0x3b,
83 }; 85 };
84 86
85 struct Rasterizer { 87 struct Rasterizer {
86 SkCanvas* canvas; 88 SkCanvas* canvas;
87 SkPicture* picture; 89 CdlPicture* picture;
88 }; 90 };
89 91
90 } // namespace 92 } // namespace
91 93
92 class DeferredImageDecoderTest : public ::testing::Test, 94 class DeferredImageDecoderTest : public ::testing::Test,
93 public MockImageDecoderClient { 95 public MockImageDecoderClient {
94 public: 96 public:
95 void SetUp() override { 97 void SetUp() override {
96 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); 98 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024);
97 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); 99 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG));
98 m_frameCount = 1; 100 m_frameCount = 1;
99 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); 101 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this);
100 m_actualDecoder = decoder.get(); 102 m_actualDecoder = decoder.get();
101 m_actualDecoder->setSize(1, 1); 103 m_actualDecoder->setSize(1, 1);
102 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); 104 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder));
103 m_surface = SkSurface::MakeRasterN32Premul(100, 100); 105 m_surface = CdlSurface::MakeRasterN32Premul(100, 100);
104 ASSERT_TRUE(m_surface.get()); 106 ASSERT_TRUE(m_surface.get());
105 m_decodeRequestCount = 0; 107 m_decodeRequestCount = 0;
106 m_repetitionCount = cAnimationNone; 108 m_repetitionCount = cAnimationNone;
107 m_status = ImageFrame::FrameComplete; 109 m_status = ImageFrame::FrameComplete;
108 m_frameDuration = 0; 110 m_frameDuration = 0;
109 m_decodedSize = m_actualDecoder->size(); 111 m_decodedSize = m_actualDecoder->size();
110 } 112 }
111 113
112 void TearDown() override { ImageDecodingStore::instance().clear(); } 114 void TearDown() override { ImageDecodingStore::instance().clear(); }
113 115
(...skipping 13 matching lines...) Expand all
127 129
128 protected: 130 protected:
129 void useMockImageDecoderFactory() { 131 void useMockImageDecoderFactory() {
130 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( 132 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(
131 MockImageDecoderFactory::create(this, m_decodedSize)); 133 MockImageDecoderFactory::create(this, m_decodedSize));
132 } 134 }
133 135
134 // Don't own this but saves the pointer to query states. 136 // Don't own this but saves the pointer to query states.
135 MockImageDecoder* m_actualDecoder; 137 MockImageDecoder* m_actualDecoder;
136 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; 138 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder;
137 sk_sp<SkSurface> m_surface; 139 sk_sp<CdlSurface> m_surface;
138 int m_decodeRequestCount; 140 int m_decodeRequestCount;
139 RefPtr<SharedBuffer> m_data; 141 RefPtr<SharedBuffer> m_data;
140 size_t m_frameCount; 142 size_t m_frameCount;
141 int m_repetitionCount; 143 int m_repetitionCount;
142 ImageFrame::Status m_status; 144 ImageFrame::Status m_status;
143 float m_frameDuration; 145 float m_frameDuration;
144 IntSize m_decodedSize; 146 IntSize m_decodedSize;
145 }; 147 };
146 148
147 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) { 149 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) {
148 m_lazyDecoder->setData(m_data, true); 150 m_lazyDecoder->setData(m_data, true);
149 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 151 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
150 ASSERT_TRUE(image); 152 ASSERT_TRUE(image);
151 EXPECT_EQ(1, image->width()); 153 EXPECT_EQ(1, image->width());
152 EXPECT_EQ(1, image->height()); 154 EXPECT_EQ(1, image->height());
153 155
154 SkPictureRecorder recorder; 156 CdlPictureRecorder recorder;
155 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 157 CdlCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
156 tempCanvas->drawImage(image.get(), 0, 0); 158 tempCanvas->drawImage(image.get(), 0, 0);
157 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); 159 sk_sp<CdlPicture> picture = recorder.finishRecordingAsPicture();
158 EXPECT_EQ(0, m_decodeRequestCount); 160 EXPECT_EQ(0, m_decodeRequestCount);
159 161
160 m_surface->getCanvas()->drawPicture(picture); 162 m_surface->getCanvas()->drawPicture(picture);
161 EXPECT_EQ(0, m_decodeRequestCount); 163 EXPECT_EQ(0, m_decodeRequestCount);
162 164
163 SkBitmap canvasBitmap; 165 SkBitmap canvasBitmap;
164 canvasBitmap.allocN32Pixels(100, 100); 166 canvasBitmap.allocN32Pixels(100, 100);
165 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 167 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0));
166 SkAutoLockPixels autoLock(canvasBitmap); 168 SkAutoLockPixels autoLock(canvasBitmap);
167 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 169 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
168 } 170 }
169 171
170 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) { 172 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) {
171 RefPtr<SharedBuffer> partialData = 173 RefPtr<SharedBuffer> partialData =
172 SharedBuffer::create(m_data->data(), m_data->size() - 10); 174 SharedBuffer::create(m_data->data(), m_data->size() - 10);
173 175
174 // Received only half the file. 176 // Received only half the file.
175 m_lazyDecoder->setData(partialData, false); 177 m_lazyDecoder->setData(partialData, false);
176 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 178 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
177 ASSERT_TRUE(image); 179 ASSERT_TRUE(image);
178 SkPictureRecorder recorder; 180 CdlPictureRecorder recorder;
179 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 181 CdlCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
180 tempCanvas->drawImage(image.get(), 0, 0); 182 tempCanvas->drawImage(image.get(), 0, 0);
181 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); 183 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture());
182 184
183 // Fully received the file and draw the SkPicture again. 185 // Fully received the file and draw the CdlPicture again.
184 m_lazyDecoder->setData(m_data, true); 186 m_lazyDecoder->setData(m_data, true);
185 image = m_lazyDecoder->createFrameAtIndex(0); 187 image = m_lazyDecoder->createFrameAtIndex(0);
186 ASSERT_TRUE(image); 188 ASSERT_TRUE(image);
187 tempCanvas = recorder.beginRecording(100, 100, 0, 0); 189 tempCanvas = recorder.beginRecording(100, 100, 0, 0);
188 tempCanvas->drawImage(image.get(), 0, 0); 190 tempCanvas->drawImage(image.get(), 0, 0);
189 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); 191 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture());
190 192
191 SkBitmap canvasBitmap; 193 SkBitmap canvasBitmap;
192 canvasBitmap.allocN32Pixels(100, 100); 194 canvasBitmap.allocN32Pixels(100, 100);
193 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 195 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0));
194 SkAutoLockPixels autoLock(canvasBitmap); 196 SkAutoLockPixels autoLock(canvasBitmap);
195 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 197 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
196 } 198 }
197 199
198 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) { 200 static void rasterizeMain(CdlCanvas* canvas, CdlPicture* picture) {
199 canvas->drawPicture(picture); 201 canvas->drawPicture(picture);
200 } 202 }
201 203
202 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { 204 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) {
203 m_lazyDecoder->setData(m_data, true); 205 m_lazyDecoder->setData(m_data, true);
204 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 206 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
205 ASSERT_TRUE(image); 207 ASSERT_TRUE(image);
206 EXPECT_EQ(1, image->width()); 208 EXPECT_EQ(1, image->width());
207 EXPECT_EQ(1, image->height()); 209 EXPECT_EQ(1, image->height());
208 210
209 SkPictureRecorder recorder; 211 CdlPictureRecorder recorder;
210 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 212 CdlCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
211 tempCanvas->drawImage(image.get(), 0, 0); 213 tempCanvas->drawImage(image.get(), 0, 0);
212 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); 214 sk_sp<CdlPicture> picture = recorder.finishRecordingAsPicture();
213 EXPECT_EQ(0, m_decodeRequestCount); 215 EXPECT_EQ(0, m_decodeRequestCount);
214 216
215 // Create a thread to rasterize SkPicture. 217 // Create a thread to rasterize CdlPicture.
216 std::unique_ptr<WebThread> thread = 218 std::unique_ptr<WebThread> thread =
217 wrapUnique(Platform::current()->createThread("RasterThread")); 219 wrapUnique(Platform::current()->createThread("RasterThread"));
218 thread->getWebTaskRunner()->postTask( 220 thread->getWebTaskRunner()->postTask(
219 BLINK_FROM_HERE, 221 BLINK_FROM_HERE,
220 crossThreadBind(&rasterizeMain, 222 crossThreadBind(&rasterizeMain,
221 crossThreadUnretained(m_surface->getCanvas()), 223 crossThreadUnretained(m_surface->getCanvas()),
222 crossThreadUnretained(picture.get()))); 224 crossThreadUnretained(picture.get())));
223 thread.reset(); 225 thread.reset();
224 EXPECT_EQ(0, m_decodeRequestCount); 226 EXPECT_EQ(0, m_decodeRequestCount);
225 227
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 m_decodedSize = IntSize(22, 33); 301 m_decodedSize = IntSize(22, 33);
300 m_lazyDecoder->setData(m_data, true); 302 m_lazyDecoder->setData(m_data, true);
301 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 303 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
302 ASSERT_TRUE(image); 304 ASSERT_TRUE(image);
303 EXPECT_EQ(m_decodedSize.width(), image->width()); 305 EXPECT_EQ(m_decodedSize.width(), image->width());
304 EXPECT_EQ(m_decodedSize.height(), image->height()); 306 EXPECT_EQ(m_decodedSize.height(), image->height());
305 307
306 useMockImageDecoderFactory(); 308 useMockImageDecoderFactory();
307 309
308 // The following code should not fail any assert. 310 // The following code should not fail any assert.
309 SkPictureRecorder recorder; 311 CdlPictureRecorder recorder;
310 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 312 CdlCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
311 tempCanvas->drawImage(image.get(), 0, 0); 313 tempCanvas->drawImage(image.get(), 0, 0);
312 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); 314 sk_sp<CdlPicture> picture = recorder.finishRecordingAsPicture();
313 EXPECT_EQ(0, m_decodeRequestCount); 315 EXPECT_EQ(0, m_decodeRequestCount);
314 m_surface->getCanvas()->drawPicture(picture); 316 m_surface->getCanvas()->drawPicture(picture);
315 EXPECT_EQ(1, m_decodeRequestCount); 317 EXPECT_EQ(1, m_decodeRequestCount);
316 } 318 }
317 319
318 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { 320 TEST_F(DeferredImageDecoderTest, smallerFrameCount) {
319 m_frameCount = 1; 321 m_frameCount = 1;
320 m_lazyDecoder->setData(m_data, false); 322 m_lazyDecoder->setData(m_data, false);
321 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 323 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
322 m_frameCount = 2; 324 m_frameCount = 2;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 SharedBuffer::create(m_data->data(), m_data->size()); 383 SharedBuffer::create(m_data->data(), m_data->size());
382 EXPECT_EQ(originalData->size(), m_data->size()); 384 EXPECT_EQ(originalData->size(), m_data->size());
383 m_lazyDecoder->setData(originalData, false); 385 m_lazyDecoder->setData(originalData, false);
384 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); 386 RefPtr<SharedBuffer> newData = m_lazyDecoder->data();
385 EXPECT_EQ(originalData->size(), newData->size()); 387 EXPECT_EQ(originalData->size(), newData->size());
386 EXPECT_EQ( 388 EXPECT_EQ(
387 0, std::memcmp(originalData->data(), newData->data(), newData->size())); 389 0, std::memcmp(originalData->data(), newData->data(), newData->size()));
388 } 390 }
389 391
390 } // namespace blink 392 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698