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

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

Issue 1001703003: Take NativeImageSkia out behind the woodshed. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Switch to skia-style API (return bool instead of SkBitmap) Created 5 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 | Annotate | Revision Log
« no previous file with comments | « Source/platform/graphics/DeferredImageDecoder.cpp ('k') | Source/platform/graphics/FrameData.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 16 matching lines...) Expand all
27 #include "platform/graphics/DeferredImageDecoder.h" 27 #include "platform/graphics/DeferredImageDecoder.h"
28 28
29 #include "SkBitmapDevice.h" 29 #include "SkBitmapDevice.h"
30 #include "SkCanvas.h" 30 #include "SkCanvas.h"
31 #include "SkPicture.h" 31 #include "SkPicture.h"
32 #include "SkPictureRecorder.h" 32 #include "SkPictureRecorder.h"
33 #include "SkSurface.h" 33 #include "SkSurface.h"
34 #include "platform/SharedBuffer.h" 34 #include "platform/SharedBuffer.h"
35 #include "platform/Task.h" 35 #include "platform/Task.h"
36 #include "platform/graphics/ImageDecodingStore.h" 36 #include "platform/graphics/ImageDecodingStore.h"
37 #include "platform/graphics/skia/NativeImageSkia.h"
38 #include "platform/graphics/test/MockImageDecoder.h" 37 #include "platform/graphics/test/MockImageDecoder.h"
39 #include "public/platform/Platform.h" 38 #include "public/platform/Platform.h"
40 #include "public/platform/WebThread.h" 39 #include "public/platform/WebThread.h"
41 #include "public/platform/WebTraceLocation.h" 40 #include "public/platform/WebTraceLocation.h"
42 #include "wtf/PassRefPtr.h" 41 #include "wtf/PassRefPtr.h"
43 #include "wtf/RefPtr.h" 42 #include "wtf/RefPtr.h"
44 #include <gtest/gtest.h> 43 #include <gtest/gtest.h>
45 44
46 namespace blink { 45 namespace blink {
47 46
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 size_t m_frameCount; 144 size_t m_frameCount;
146 int m_repetitionCount; 145 int m_repetitionCount;
147 ImageFrame::Status m_status; 146 ImageFrame::Status m_status;
148 float m_frameDuration; 147 float m_frameDuration;
149 IntSize m_decodedSize; 148 IntSize m_decodedSize;
150 }; 149 };
151 150
152 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) 151 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture)
153 { 152 {
154 m_lazyDecoder->setData(*m_data, true); 153 m_lazyDecoder->setData(*m_data, true);
155 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); 154 SkBitmap bitmap;
156 EXPECT_EQ(1, image->bitmap().width()); 155 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
157 EXPECT_EQ(1, image->bitmap().height()); 156 EXPECT_EQ(1, bitmap.width());
158 EXPECT_FALSE(image->bitmap().isNull()); 157 EXPECT_EQ(1, bitmap.height());
159 EXPECT_TRUE(image->bitmap().isImmutable()); 158 EXPECT_FALSE(bitmap.isNull());
159 EXPECT_TRUE(bitmap.isImmutable());
160 160
161 SkPictureRecorder recorder; 161 SkPictureRecorder recorder;
162 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 162 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
163 tempCanvas->drawBitmap(image->bitmap(), 0, 0); 163 tempCanvas->drawBitmap(bitmap, 0, 0);
164 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); 164 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording());
165 EXPECT_EQ(0, m_frameBufferRequestCount); 165 EXPECT_EQ(0, m_frameBufferRequestCount);
166 166
167 m_surface->getCanvas()->drawPicture(picture.get()); 167 m_surface->getCanvas()->drawPicture(picture.get());
168 EXPECT_EQ(0, m_frameBufferRequestCount); 168 EXPECT_EQ(0, m_frameBufferRequestCount);
169 169
170 SkBitmap canvasBitmap; 170 SkBitmap canvasBitmap;
171 canvasBitmap.allocN32Pixels(100, 100); 171 canvasBitmap.allocN32Pixels(100, 100);
172 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 172 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0));
173 SkAutoLockPixels autoLock(canvasBitmap); 173 SkAutoLockPixels autoLock(canvasBitmap);
174 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 174 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
175 } 175 }
176 176
177 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) 177 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive)
178 { 178 {
179 RefPtr<SharedBuffer> partialData = SharedBuffer::create(m_data->data(), m_da ta->size() - 10); 179 RefPtr<SharedBuffer> partialData = SharedBuffer::create(m_data->data(), m_da ta->size() - 10);
180 180
181 // Received only half the file. 181 // Received only half the file.
182 m_lazyDecoder->setData(*partialData, false); 182 m_lazyDecoder->setData(*partialData, false);
183 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); 183 SkBitmap bitmap;
184 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
184 SkPictureRecorder recorder; 185 SkPictureRecorder recorder;
185 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 186 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
186 tempCanvas->drawBitmap(image->bitmap(), 0, 0); 187 tempCanvas->drawBitmap(bitmap, 0, 0);
187 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); 188 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording());
188 m_surface->getCanvas()->drawPicture(picture.get()); 189 m_surface->getCanvas()->drawPicture(picture.get());
189 190
190 // Fully received the file and draw the SkPicture again. 191 // Fully received the file and draw the SkPicture again.
191 m_lazyDecoder->setData(*m_data, true); 192 m_lazyDecoder->setData(*m_data, true);
192 image = m_lazyDecoder->createFrameAtIndex(0); 193 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
193 tempCanvas = recorder.beginRecording(100, 100, 0, 0); 194 tempCanvas = recorder.beginRecording(100, 100, 0, 0);
194 tempCanvas->drawBitmap(image->bitmap(), 0, 0); 195 tempCanvas->drawBitmap(bitmap, 0, 0);
195 picture = adoptRef(recorder.endRecording()); 196 picture = adoptRef(recorder.endRecording());
196 m_surface->getCanvas()->drawPicture(picture.get()); 197 m_surface->getCanvas()->drawPicture(picture.get());
197 198
198 SkBitmap canvasBitmap; 199 SkBitmap canvasBitmap;
199 canvasBitmap.allocN32Pixels(100, 100); 200 canvasBitmap.allocN32Pixels(100, 100);
200 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 201 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0));
201 SkAutoLockPixels autoLock(canvasBitmap); 202 SkAutoLockPixels autoLock(canvasBitmap);
202 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 203 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
203 } 204 }
204 205
205 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) 206 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture)
206 { 207 {
207 canvas->drawPicture(picture); 208 canvas->drawPicture(picture);
208 } 209 }
209 210
210 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) 211 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread)
211 { 212 {
212 m_lazyDecoder->setData(*m_data, true); 213 m_lazyDecoder->setData(*m_data, true);
213 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); 214 SkBitmap bitmap;
214 EXPECT_EQ(1, image->bitmap().width()); 215 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
215 EXPECT_EQ(1, image->bitmap().height()); 216 EXPECT_EQ(1, bitmap.width());
216 EXPECT_FALSE(image->bitmap().isNull()); 217 EXPECT_EQ(1, bitmap.height());
217 EXPECT_TRUE(image->bitmap().isImmutable()); 218 EXPECT_FALSE(bitmap.isNull());
219 EXPECT_TRUE(bitmap.isImmutable());
218 220
219 SkPictureRecorder recorder; 221 SkPictureRecorder recorder;
220 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 222 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
221 tempCanvas->drawBitmap(image->bitmap(), 0, 0); 223 tempCanvas->drawBitmap(bitmap, 0, 0);
222 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); 224 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording());
223 EXPECT_EQ(0, m_frameBufferRequestCount); 225 EXPECT_EQ(0, m_frameBufferRequestCount);
224 226
225 // Create a thread to rasterize SkPicture. 227 // Create a thread to rasterize SkPicture.
226 OwnPtr<WebThread> thread = adoptPtr(Platform::current()->createThread("Raste rThread")); 228 OwnPtr<WebThread> thread = adoptPtr(Platform::current()->createThread("Raste rThread"));
227 thread->postTask(FROM_HERE, new Task(WTF::bind(&rasterizeMain, m_surface->ge tCanvas(), picture.get()))); 229 thread->postTask(FROM_HERE, new Task(WTF::bind(&rasterizeMain, m_surface->ge tCanvas(), picture.get())));
228 thread.clear(); 230 thread.clear();
229 EXPECT_EQ(0, m_frameBufferRequestCount); 231 EXPECT_EQ(0, m_frameBufferRequestCount);
230 232
231 SkBitmap canvasBitmap; 233 SkBitmap canvasBitmap;
232 canvasBitmap.allocN32Pixels(100, 100); 234 canvasBitmap.allocN32Pixels(100, 100);
233 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); 235 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0));
234 SkAutoLockPixels autoLock(canvasBitmap); 236 SkAutoLockPixels autoLock(canvasBitmap);
235 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); 237 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0));
236 } 238 }
237 239
238 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) 240 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading)
239 { 241 {
240 m_status = ImageFrame::FramePartial; 242 m_status = ImageFrame::FramePartial;
241 m_lazyDecoder->setData(*m_data, false); 243 m_lazyDecoder->setData(*m_data, false);
242 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
243 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenerat ionID(); 245 SkBitmap bitmap;
246 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
247 unsigned firstId = bitmap.getGenerationID();
244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 248 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
245 EXPECT_TRUE(m_actualDecoder); 249 EXPECT_TRUE(m_actualDecoder);
246 250
247 m_status = ImageFrame::FrameComplete; 251 m_status = ImageFrame::FrameComplete;
248 m_data->append(" ", 1); 252 m_data->append(" ", 1);
249 m_lazyDecoder->setData(*m_data, true); 253 m_lazyDecoder->setData(*m_data, true);
250 EXPECT_FALSE(m_actualDecoder); 254 EXPECT_FALSE(m_actualDecoder);
251 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 255 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
252 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenera tionID(); 256 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
257 unsigned secondId = bitmap.getGenerationID();
253 EXPECT_FALSE(m_frameBufferRequestCount); 258 EXPECT_FALSE(m_frameBufferRequestCount);
254 EXPECT_NE(firstId, secondId); 259 EXPECT_NE(firstId, secondId);
255 } 260 }
256 261
257 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading) 262 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading)
258 { 263 {
259 m_repetitionCount = 10; 264 m_repetitionCount = 10;
260 m_frameCount = 1; 265 m_frameCount = 1;
261 m_frameDuration = 10; 266 m_frameDuration = 10;
262 m_status = ImageFrame::FramePartial; 267 m_status = ImageFrame::FramePartial;
263 m_lazyDecoder->setData(*m_data, false); 268 m_lazyDecoder->setData(*m_data, false);
264 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenerat ionID(); 269 SkBitmap bitmap;
270 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
271 unsigned firstId = bitmap.getGenerationID();
265 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 272 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0));
266 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); 273 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
267 274
268 m_frameCount = 2; 275 m_frameCount = 2;
269 m_frameDuration = 20; 276 m_frameDuration = 20;
270 m_status = ImageFrame::FrameComplete; 277 m_status = ImageFrame::FrameComplete;
271 m_data->append(" ", 1); 278 m_data->append(" ", 1);
272 m_lazyDecoder->setData(*m_data, false); 279 m_lazyDecoder->setData(*m_data, false);
273 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenera tionID(); 280 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
281 unsigned secondId = bitmap.getGenerationID();
274 EXPECT_NE(firstId, secondId); 282 EXPECT_NE(firstId, secondId);
275 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 283 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
276 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); 284 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
277 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); 285 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
278 EXPECT_TRUE(m_actualDecoder); 286 EXPECT_TRUE(m_actualDecoder);
279 287
280 m_frameCount = 3; 288 m_frameCount = 3;
281 m_frameDuration = 30; 289 m_frameDuration = 30;
282 m_status = ImageFrame::FrameComplete; 290 m_status = ImageFrame::FrameComplete;
283 m_lazyDecoder->setData(*m_data, true); 291 m_lazyDecoder->setData(*m_data, true);
284 EXPECT_FALSE(m_actualDecoder); 292 EXPECT_FALSE(m_actualDecoder);
285 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); 293 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0));
286 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); 294 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1));
287 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2)); 295 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2));
288 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); 296 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0));
289 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); 297 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1));
290 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2)); 298 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2));
291 EXPECT_EQ(10, m_lazyDecoder->repetitionCount()); 299 EXPECT_EQ(10, m_lazyDecoder->repetitionCount());
292 } 300 }
293 301
294 TEST_F(DeferredImageDecoderTest, decodedSize) 302 TEST_F(DeferredImageDecoderTest, decodedSize)
295 { 303 {
296 m_decodedSize = IntSize(22, 33); 304 m_decodedSize = IntSize(22, 33);
297 m_lazyDecoder->setData(*m_data, true); 305 m_lazyDecoder->setData(*m_data, true);
298 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); 306 SkBitmap bitmap;
299 EXPECT_EQ(m_decodedSize.width(), image->bitmap().width()); 307 EXPECT_TRUE(m_lazyDecoder->createFrameAtIndex(0, &bitmap));
300 EXPECT_EQ(m_decodedSize.height(), image->bitmap().height()); 308 EXPECT_EQ(m_decodedSize.width(), bitmap.width());
301 EXPECT_FALSE(image->bitmap().isNull()); 309 EXPECT_EQ(m_decodedSize.height(), bitmap.height());
302 EXPECT_TRUE(image->bitmap().isImmutable()); 310 EXPECT_FALSE(bitmap.isNull());
311 EXPECT_TRUE(bitmap.isImmutable());
303 312
304 useMockImageDecoderFactory(); 313 useMockImageDecoderFactory();
305 314
306 // The following code should not fail any assert. 315 // The following code should not fail any assert.
307 SkPictureRecorder recorder; 316 SkPictureRecorder recorder;
308 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); 317 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0);
309 tempCanvas->drawBitmap(image->bitmap(), 0, 0); 318 tempCanvas->drawBitmap(bitmap, 0, 0);
310 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); 319 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording());
311 EXPECT_EQ(0, m_frameBufferRequestCount); 320 EXPECT_EQ(0, m_frameBufferRequestCount);
312 m_surface->getCanvas()->drawPicture(picture.get()); 321 m_surface->getCanvas()->drawPicture(picture.get());
313 EXPECT_EQ(1, m_frameBufferRequestCount); 322 EXPECT_EQ(1, m_frameBufferRequestCount);
314 } 323 }
315 324
316 TEST_F(DeferredImageDecoderTest, smallerFrameCount) 325 TEST_F(DeferredImageDecoderTest, smallerFrameCount)
317 { 326 {
318 m_frameCount = 1; 327 m_frameCount = 1;
319 m_lazyDecoder->setData(*m_data, false); 328 m_lazyDecoder->setData(*m_data, false);
320 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 329 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
321 m_frameCount = 2; 330 m_frameCount = 2;
322 m_lazyDecoder->setData(*m_data, false); 331 m_lazyDecoder->setData(*m_data, false);
323 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 332 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
324 m_frameCount = 0; 333 m_frameCount = 0;
325 m_lazyDecoder->setData(*m_data, true); 334 m_lazyDecoder->setData(*m_data, true);
326 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); 335 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount());
327 } 336 }
328 337
329 } // namespace blink 338 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/graphics/DeferredImageDecoder.cpp ('k') | Source/platform/graphics/FrameData.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698