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

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

Issue 2764633002: cc/paint: Remove cc::PaintSurface. (Closed)
Patch Set: more stuff Created 3 years, 9 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
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
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 <memory> 28 #include <memory>
29 #include "cc/paint/skia_paint_canvas.h"
29 #include "platform/CrossThreadFunctional.h" 30 #include "platform/CrossThreadFunctional.h"
30 #include "platform/SharedBuffer.h" 31 #include "platform/SharedBuffer.h"
31 #include "platform/WebTaskRunner.h" 32 #include "platform/WebTaskRunner.h"
32 #include "platform/graphics/ImageDecodingStore.h" 33 #include "platform/graphics/ImageDecodingStore.h"
33 #include "platform/graphics/ImageFrameGenerator.h" 34 #include "platform/graphics/ImageFrameGenerator.h"
34 #include "platform/graphics/paint/PaintCanvas.h" 35 #include "platform/graphics/paint/PaintCanvas.h"
35 #include "platform/graphics/paint/PaintRecord.h" 36 #include "platform/graphics/paint/PaintRecord.h"
36 #include "platform/graphics/paint/PaintRecorder.h" 37 #include "platform/graphics/paint/PaintRecorder.h"
37 #include "platform/graphics/paint/PaintSurface.h"
38 #include "platform/graphics/test/MockImageDecoder.h" 38 #include "platform/graphics/test/MockImageDecoder.h"
39 #include "public/platform/Platform.h" 39 #include "public/platform/Platform.h"
40 #include "public/platform/WebThread.h" 40 #include "public/platform/WebThread.h"
41 #include "public/platform/WebTraceLocation.h" 41 #include "public/platform/WebTraceLocation.h"
42 #include "testing/gtest/include/gtest/gtest.h" 42 #include "testing/gtest/include/gtest/gtest.h"
43 #include "third_party/skia/include/core/SkImage.h" 43 #include "third_party/skia/include/core/SkImage.h"
44 #include "third_party/skia/include/core/SkPixmap.h" 44 #include "third_party/skia/include/core/SkPixmap.h"
45 #include "third_party/skia/include/core/SkSurface.h" 45 #include "third_party/skia/include/core/SkSurface.h"
46 #include "wtf/PassRefPtr.h" 46 #include "wtf/PassRefPtr.h"
47 #include "wtf/PtrUtil.h" 47 #include "wtf/PtrUtil.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 public MockImageDecoderClient { 102 public MockImageDecoderClient {
103 public: 103 public:
104 void SetUp() override { 104 void SetUp() override {
105 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); 105 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024);
106 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); 106 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG));
107 m_frameCount = 1; 107 m_frameCount = 1;
108 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); 108 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this);
109 m_actualDecoder = decoder.get(); 109 m_actualDecoder = decoder.get();
110 m_actualDecoder->setSize(1, 1); 110 m_actualDecoder->setSize(1, 1);
111 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); 111 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder));
112 m_surface = PaintSurface::MakeRasterN32Premul(100, 100); 112 m_canvas = cc::SkiaPaintCanvas::CreateRasterZeroed(
113 ASSERT_TRUE(m_surface.get()); 113 SkImageInfo::MakeN32Premul(100, 100));
114 ASSERT_TRUE(m_canvas.get());
114 m_decodeRequestCount = 0; 115 m_decodeRequestCount = 0;
115 m_repetitionCount = cAnimationNone; 116 m_repetitionCount = cAnimationNone;
116 m_status = ImageFrame::FrameComplete; 117 m_status = ImageFrame::FrameComplete;
117 m_frameDuration = 0; 118 m_frameDuration = 0;
118 m_decodedSize = m_actualDecoder->size(); 119 m_decodedSize = m_actualDecoder->size();
119 } 120 }
120 121
121 void TearDown() override { ImageDecodingStore::instance().clear(); } 122 void TearDown() override { ImageDecodingStore::instance().clear(); }
122 123
123 void decoderBeingDestroyed() override { m_actualDecoder = 0; } 124 void decoderBeingDestroyed() override { m_actualDecoder = 0; }
(...skipping 12 matching lines...) Expand all
136 137
137 protected: 138 protected:
138 void useMockImageDecoderFactory() { 139 void useMockImageDecoderFactory() {
139 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( 140 m_lazyDecoder->frameGenerator()->setImageDecoderFactory(
140 MockImageDecoderFactory::create(this, m_decodedSize)); 141 MockImageDecoderFactory::create(this, m_decodedSize));
141 } 142 }
142 143
143 // Don't own this but saves the pointer to query states. 144 // Don't own this but saves the pointer to query states.
144 MockImageDecoder* m_actualDecoder; 145 MockImageDecoder* m_actualDecoder;
145 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; 146 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder;
146 sk_sp<PaintSurface> m_surface; 147 std::unique_ptr<cc::PaintCanvas> m_canvas;
147 int m_decodeRequestCount; 148 int m_decodeRequestCount;
148 RefPtr<SharedBuffer> m_data; 149 RefPtr<SharedBuffer> m_data;
149 size_t m_frameCount; 150 size_t m_frameCount;
150 int m_repetitionCount; 151 int m_repetitionCount;
151 ImageFrame::Status m_status; 152 ImageFrame::Status m_status;
152 float m_frameDuration; 153 float m_frameDuration;
153 IntSize m_decodedSize; 154 IntSize m_decodedSize;
154 }; 155 };
155 156
156 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecord) { 157 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecord) {
157 m_lazyDecoder->setData(m_data, true); 158 m_lazyDecoder->setData(m_data, true);
158 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 159 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
159 ASSERT_TRUE(image); 160 ASSERT_TRUE(image);
160 EXPECT_EQ(1, image->width()); 161 EXPECT_EQ(1, image->width());
161 EXPECT_EQ(1, image->height()); 162 EXPECT_EQ(1, image->height());
162 163
163 PaintRecorder recorder; 164 PaintRecorder recorder;
164 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); 165 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100);
165 tempCanvas->drawImage(image, 0, 0); 166 tempCanvas->drawImage(image, 0, 0);
166 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); 167 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture();
167 EXPECT_EQ(0, m_decodeRequestCount); 168 EXPECT_EQ(0, m_decodeRequestCount);
168 169
169 m_surface->getCanvas()->drawPicture(record); 170 m_canvas->drawPicture(record);
170 EXPECT_EQ(0, m_decodeRequestCount); 171 EXPECT_EQ(0, m_decodeRequestCount);
171 172
172 SkBitmap canvasBitmap; 173 SkBitmap canvasBitmap;
173 canvasBitmap.allocN32Pixels(100, 100); 174 canvasBitmap.allocN32Pixels(100, 100);
174 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 175 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
175 SkAutoLockPixels autoLock(canvasBitmap); 176 SkAutoLockPixels autoLock(canvasBitmap);
176 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 177 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
177 } 178 }
178 179
179 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecordProgressive) { 180 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecordProgressive) {
180 RefPtr<SharedBuffer> partialData = 181 RefPtr<SharedBuffer> partialData =
181 SharedBuffer::create(m_data->data(), m_data->size() - 10); 182 SharedBuffer::create(m_data->data(), m_data->size() - 10);
182 183
183 // Received only half the file. 184 // Received only half the file.
184 m_lazyDecoder->setData(partialData, false); 185 m_lazyDecoder->setData(partialData, false);
185 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 186 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
186 ASSERT_TRUE(image); 187 ASSERT_TRUE(image);
187 PaintRecorder recorder; 188 PaintRecorder recorder;
188 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); 189 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100);
189 tempCanvas->drawImage(std::move(image), 0, 0); 190 tempCanvas->drawImage(std::move(image), 0, 0);
190 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); 191 m_canvas->drawPicture(recorder.finishRecordingAsPicture());
191 192
192 // Fully received the file and draw the PaintRecord again. 193 // Fully received the file and draw the PaintRecord again.
193 m_lazyDecoder->setData(m_data, true); 194 m_lazyDecoder->setData(m_data, true);
194 image = m_lazyDecoder->createFrameAtIndex(0); 195 image = m_lazyDecoder->createFrameAtIndex(0);
195 ASSERT_TRUE(image); 196 ASSERT_TRUE(image);
196 tempCanvas = recorder.beginRecording(100, 100); 197 tempCanvas = recorder.beginRecording(100, 100);
197 tempCanvas->drawImage(std::move(image), 0, 0); 198 tempCanvas->drawImage(std::move(image), 0, 0);
198 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); 199 m_canvas->drawPicture(recorder.finishRecordingAsPicture());
199 200
200 SkBitmap canvasBitmap; 201 SkBitmap canvasBitmap;
201 canvasBitmap.allocN32Pixels(100, 100); 202 canvasBitmap.allocN32Pixels(100, 100);
202 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 203 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
203 SkAutoLockPixels autoLock(canvasBitmap); 204 SkAutoLockPixels autoLock(canvasBitmap);
204 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 205 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
205 } 206 }
206 207
207 static void rasterizeMain(PaintCanvas* canvas, sk_sp<PaintRecord> record) { 208 static void rasterizeMain(PaintCanvas* canvas, sk_sp<PaintRecord> record) {
208 canvas->drawPicture(record); 209 canvas->drawPicture(record);
209 } 210 }
210 211
211 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { 212 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) {
212 m_lazyDecoder->setData(m_data, true); 213 m_lazyDecoder->setData(m_data, true);
213 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 214 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
214 ASSERT_TRUE(image); 215 ASSERT_TRUE(image);
215 EXPECT_EQ(1, image->width()); 216 EXPECT_EQ(1, image->width());
216 EXPECT_EQ(1, image->height()); 217 EXPECT_EQ(1, image->height());
217 218
218 PaintRecorder recorder; 219 PaintRecorder recorder;
219 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); 220 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100);
220 tempCanvas->drawImage(std::move(image), 0, 0); 221 tempCanvas->drawImage(std::move(image), 0, 0);
221 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); 222 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture();
222 EXPECT_EQ(0, m_decodeRequestCount); 223 EXPECT_EQ(0, m_decodeRequestCount);
223 224
224 // Create a thread to rasterize PaintRecord. 225 // Create a thread to rasterize PaintRecord.
225 std::unique_ptr<WebThread> thread = 226 std::unique_ptr<WebThread> thread =
226 WTF::wrapUnique(Platform::current()->createThread("RasterThread")); 227 WTF::wrapUnique(Platform::current()->createThread("RasterThread"));
227 thread->getWebTaskRunner()->postTask( 228 thread->getWebTaskRunner()->postTask(
228 BLINK_FROM_HERE, 229 BLINK_FROM_HERE,
229 crossThreadBind(&rasterizeMain, 230 crossThreadBind(&rasterizeMain, crossThreadUnretained(m_canvas.get()),
230 crossThreadUnretained(m_surface->getCanvas()), record)); 231 record));
231 thread.reset(); 232 thread.reset();
232 EXPECT_EQ(0, m_decodeRequestCount); 233 EXPECT_EQ(0, m_decodeRequestCount);
233 234
234 SkBitmap canvasBitmap; 235 SkBitmap canvasBitmap;
235 canvasBitmap.allocN32Pixels(100, 100); 236 canvasBitmap.allocN32Pixels(100, 100);
236 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 237 ASSERT_TRUE(m_canvas->readPixels(&canvasBitmap, 0, 0));
237 SkAutoLockPixels autoLock(canvasBitmap); 238 SkAutoLockPixels autoLock(canvasBitmap);
238 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 239 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
239 } 240 }
240 241
241 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) { 242 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) {
242 m_status = ImageFrame::FramePartial; 243 m_status = ImageFrame::FramePartial;
243 m_lazyDecoder->setData(m_data, false); 244 m_lazyDecoder->setData(m_data, false);
244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 245 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
245 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); 246 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0);
246 ASSERT_TRUE(image); 247 ASSERT_TRUE(image);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 EXPECT_EQ(m_decodedSize.height(), image->height()); 313 EXPECT_EQ(m_decodedSize.height(), image->height());
313 314
314 useMockImageDecoderFactory(); 315 useMockImageDecoderFactory();
315 316
316 // The following code should not fail any assert. 317 // The following code should not fail any assert.
317 PaintRecorder recorder; 318 PaintRecorder recorder;
318 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); 319 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100);
319 tempCanvas->drawImage(std::move(image), 0, 0); 320 tempCanvas->drawImage(std::move(image), 0, 0);
320 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); 321 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture();
321 EXPECT_EQ(0, m_decodeRequestCount); 322 EXPECT_EQ(0, m_decodeRequestCount);
322 m_surface->getCanvas()->drawPicture(record); 323 m_canvas->drawPicture(record);
323 EXPECT_EQ(1, m_decodeRequestCount); 324 EXPECT_EQ(1, m_decodeRequestCount);
324 } 325 }
325 326
326 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { 327 TEST_F(DeferredImageDecoderTest, smallerFrameCount) {
327 m_frameCount = 1; 328 m_frameCount = 1;
328 m_lazyDecoder->setData(m_data, false); 329 m_lazyDecoder->setData(m_data, false);
329 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 330 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
330 m_frameCount = 2; 331 m_frameCount = 2;
331 m_lazyDecoder->setData(m_data, false); 332 m_lazyDecoder->setData(m_data, false);
332 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 333 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 SharedBuffer::create(m_data->data(), m_data->size()); 395 SharedBuffer::create(m_data->data(), m_data->size());
395 EXPECT_EQ(originalData->size(), m_data->size()); 396 EXPECT_EQ(originalData->size(), m_data->size());
396 m_lazyDecoder->setData(originalData, false); 397 m_lazyDecoder->setData(originalData, false);
397 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); 398 RefPtr<SharedBuffer> newData = m_lazyDecoder->data();
398 EXPECT_EQ(originalData->size(), newData->size()); 399 EXPECT_EQ(originalData->size(), newData->size());
399 EXPECT_EQ( 400 EXPECT_EQ(
400 0, std::memcmp(originalData->data(), newData->data(), newData->size())); 401 0, std::memcmp(originalData->data(), newData->data(), newData->size()));
401 } 402 }
402 403
403 } // namespace blink 404 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698