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

Side by Side Diff: tests/SerializationTest.cpp

Issue 353993003: Fix SkTypeface::serialize() on Mac by properly indicating local fonts (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: style nits Created 6 years, 5 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
« src/ports/SkFontHost_mac.cpp ('K') | « tests/FontHostStreamTest.cpp ('k') | no next file » | 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 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Resources.h"
8 #include "SkBitmapDevice.h" 9 #include "SkBitmapDevice.h"
9 #include "SkBitmapSource.h" 10 #include "SkBitmapSource.h"
10 #include "SkCanvas.h" 11 #include "SkCanvas.h"
11 #include "SkMallocPixelRef.h" 12 #include "SkMallocPixelRef.h"
13 #include "SkOSFile.h"
12 #include "SkPictureRecorder.h" 14 #include "SkPictureRecorder.h"
13 #include "SkTemplates.h" 15 #include "SkTemplates.h"
16 #include "SkTypeface.h"
14 #include "SkWriteBuffer.h" 17 #include "SkWriteBuffer.h"
15 #include "SkValidatingReadBuffer.h" 18 #include "SkValidatingReadBuffer.h"
16 #include "SkXfermodeImageFilter.h" 19 #include "SkXfermodeImageFilter.h"
17 #include "Test.h" 20 #include "Test.h"
18 21
19 static const uint32_t kArraySize = 64; 22 static const uint32_t kArraySize = 64;
20 static const int kBitmapSize = 256; 23 static const int kBitmapSize = 256;
21 24
22 template<typename T> 25 template<typename T>
23 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { 26 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) {
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 bitmap.allocN32Pixels(24, 24); 255 bitmap.allocN32Pixels(24, 24);
253 SkCanvas canvas(bitmap); 256 SkCanvas canvas(bitmap);
254 canvas.clear(0x00000000); 257 canvas.clear(0x00000000);
255 SkPaint paint; 258 SkPaint paint;
256 paint.setImageFilter(deserializedFilter); 259 paint.setImageFilter(deserializedFilter);
257 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar( 24))); 260 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar( 24)));
258 canvas.drawBitmap(bitmap, 0, 0, &paint); 261 canvas.drawBitmap(bitmap, 0, 0, &paint);
259 } 262 }
260 } 263 }
261 264
265 static SkBitmap draw_picture(SkPicture& picture) {
266 SkBitmap bitmap;
267 bitmap.allocN32Pixels(picture.width(), picture.height());
268 SkCanvas canvas(bitmap);
269 picture.draw(&canvas);
270 return bitmap;
271 }
272
273 static void compare_bitmaps(skiatest::Reporter* reporter,
274 const SkBitmap& b1, const SkBitmap& b2) {
275 REPORTER_ASSERT(reporter, b1.width() == b2.width());
276 REPORTER_ASSERT(reporter, b1.height() == b2.height());
277 SkAutoLockPixels autoLockPixels1(b1);
278 SkAutoLockPixels autoLockPixels2(b2);
279
280 if ((b1.width() != b2.width()) ||
281 (b1.height() != b2.height())) {
282 return;
283 }
284
285 int pixelErrors = 0;
286 for (int y = 0; y < b2.height(); ++y) {
287 for (int x = 0; x < b2.width(); ++x) {
288 if (b1.getColor(x, y) != b2.getColor(x, y))
289 ++pixelErrors;
290 }
291 }
292 REPORTER_ASSERT(reporter, 0 == pixelErrors);
293 }
294
295 static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
296 // Load typeface form file.
297 // This test cannot run if there is no resource path.
298 SkString resourcePath = GetResourcePath();
299 if (resourcePath.isEmpty()) {
300 SkDebugf("Could not run fontstream test because resourcePath not specifi ed.");
301 return;
302 }
303 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "test.ttc");
304 SkTypeface* typeface = SkTypeface::CreateFromFile(filename.c_str());
305 if (!typeface) {
306 SkDebugf("Could not run fontstream test because test.ttc not found.");
307 return;
308 }
309
310 // Create a paint with the typeface we loaded.
311 SkPaint paint;
312 paint.setColor(SK_ColorGRAY);
313 paint.setTextSize(SkIntToScalar(30));
314 SkSafeUnref(paint.setTypeface(typeface));
315
316 // Paint some text.
317 SkPictureRecorder recorder;
318 SkIRect canvasRect = SkIRect::MakeWH(kBitmapSize, kBitmapSize);
319 SkCanvas* canvas = recorder.beginRecording(canvasRect.width(), canvasRect.he ight(), NULL, 0);
320 canvas->drawColor(SK_ColorWHITE);
321 canvas->drawText("A", 1, 24, 32, paint);
322 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
323
324 // Serlialize picture and create its clone from stream.
325 SkDynamicMemoryWStream stream;
326 picture->serialize(&stream);
327 SkAutoTUnref<SkStream> inputStream(stream.detachAsStream());
328 SkAutoTUnref<SkPicture> loadedPicture(SkPicture::CreateFromStream(inputStrea m.get()));
329
330 // Draw both original and clone picture and compare bitmaps -- they should b e identical.
331 SkBitmap origBitmap = draw_picture(*picture);
332 SkBitmap destBitmap = draw_picture(*loadedPicture);
333 compare_bitmaps(reporter, origBitmap, destBitmap);
334 }
335
262 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { 336 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
263 SkImageInfo info = SkImageInfo::Make( 337 SkImageInfo info = SkImageInfo::Make(
264 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType); 338 kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
265 return bitmap->allocPixels(info); 339 return bitmap->allocPixels(info);
266 } 340 }
267 341
268 static bool make_checkerboard_bitmap(SkBitmap& bitmap) { 342 static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
269 bool success = setup_bitmap_for_canvas(&bitmap); 343 bool success = setup_bitmap_for_canvas(&bitmap);
270 344
271 SkCanvas canvas(bitmap); 345 SkCanvas canvas(bitmap);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k BitmapSize/4), paint); 389 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k BitmapSize/4), paint);
316 390
317 return success; 391 return success;
318 } 392 }
319 393
320 DEF_TEST(Serialization, reporter) { 394 DEF_TEST(Serialization, reporter) {
321 // Test matrix serialization 395 // Test matrix serialization
322 { 396 {
323 SkMatrix matrix = SkMatrix::I(); 397 SkMatrix matrix = SkMatrix::I();
324 TestObjectSerialization(&matrix, reporter); 398 TestObjectSerialization(&matrix, reporter);
325 } 399 }
326 400
327 // Test path serialization 401 // Test path serialization
328 { 402 {
329 SkPath path; 403 SkPath path;
330 TestObjectSerialization(&path, reporter); 404 TestObjectSerialization(&path, reporter);
331 } 405 }
332 406
333 // Test region serialization 407 // Test region serialization
334 { 408 {
335 SkRegion region; 409 SkRegion region;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 size_t size = writer.bytesWritten(); 489 size_t size = writer.bytesWritten();
416 SkAutoTMalloc<unsigned char> data(size); 490 SkAutoTMalloc<unsigned char> data(size);
417 writer.writeToMemory(static_cast<void*>(data.get())); 491 writer.writeToMemory(static_cast<void*>(data.get()));
418 492
419 // Deserialize picture 493 // Deserialize picture
420 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); 494 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size);
421 SkAutoTUnref<SkPicture> readPict( 495 SkAutoTUnref<SkPicture> readPict(
422 SkPicture::CreateFromBuffer(reader)); 496 SkPicture::CreateFromBuffer(reader));
423 REPORTER_ASSERT(reporter, NULL != readPict.get()); 497 REPORTER_ASSERT(reporter, NULL != readPict.get());
424 } 498 }
499
500 TestPictureTypefaceSerialization(reporter);
425 } 501 }
OLDNEW
« src/ports/SkFontHost_mac.cpp ('K') | « tests/FontHostStreamTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698