OLD | NEW |
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 "SkAnnotationKeys.h" |
8 #include "Resources.h" | 9 #include "Resources.h" |
9 #include "SkAnnotationKeys.h" | |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkFixed.h" | 11 #include "SkFixed.h" |
12 #include "SkFontDescriptor.h" | 12 #include "SkFontDescriptor.h" |
13 #include "SkImage.h" | 13 #include "SkImage.h" |
14 #include "SkImageSource.h" | 14 #include "SkImageSource.h" |
15 #include "SkLightingShader.h" | |
16 #include "SkMallocPixelRef.h" | 15 #include "SkMallocPixelRef.h" |
17 #include "SkOSFile.h" | 16 #include "SkOSFile.h" |
18 #include "SkPictureRecorder.h" | 17 #include "SkPictureRecorder.h" |
19 #include "SkTableColorFilter.h" | 18 #include "SkTableColorFilter.h" |
20 #include "SkTemplates.h" | 19 #include "SkTemplates.h" |
21 #include "SkTypeface.h" | 20 #include "SkTypeface.h" |
22 #include "SkWriteBuffer.h" | 21 #include "SkWriteBuffer.h" |
23 #include "SkValidatingReadBuffer.h" | 22 #include "SkValidatingReadBuffer.h" |
24 #include "SkXfermodeImageFilter.h" | 23 #include "SkXfermodeImageFilter.h" |
25 #include "sk_tool_utils.h" | |
26 #include "Test.h" | 24 #include "Test.h" |
27 | 25 |
28 static const uint32_t kArraySize = 64; | 26 static const uint32_t kArraySize = 64; |
29 static const int kBitmapSize = 256; | 27 static const int kBitmapSize = 256; |
30 | 28 |
31 template<typename T> | 29 template<typename T> |
32 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { | 30 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
33 // Test memory read/write functions directly | 31 // Test memory read/write functions directly |
34 unsigned char dataWritten[1024]; | 32 unsigned char dataWritten[1024]; |
35 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); | 33 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 175 } |
178 | 176 |
179 template<typename T> | 177 template<typename T> |
180 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, | 178 static T* TestFlattenableSerialization(T* testObj, bool shouldSucceed, |
181 skiatest::Reporter* reporter) { | 179 skiatest::Reporter* reporter) { |
182 SkBinaryWriteBuffer writer; | 180 SkBinaryWriteBuffer writer; |
183 SerializationUtils<T>::Write(writer, testObj); | 181 SerializationUtils<T>::Write(writer, testObj); |
184 size_t bytesWritten = writer.bytesWritten(); | 182 size_t bytesWritten = writer.bytesWritten(); |
185 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | 183 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); |
186 | 184 |
187 SkASSERT(bytesWritten <= 4096); | |
188 unsigned char dataWritten[4096]; | 185 unsigned char dataWritten[4096]; |
| 186 SkASSERT(bytesWritten <= sizeof(dataWritten)); |
189 writer.writeToMemory(dataWritten); | 187 writer.writeToMemory(dataWritten); |
190 | 188 |
191 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) | 189 // Make sure this fails when it should (test with smaller size, but still mu
ltiple of 4) |
192 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | 190 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
193 T* obj = nullptr; | 191 T* obj = nullptr; |
194 SerializationUtils<T>::Read(buffer, &obj); | 192 SerializationUtils<T>::Read(buffer, &obj); |
195 REPORTER_ASSERT(reporter, !buffer.isValid()); | 193 REPORTER_ASSERT(reporter, !buffer.isValid()); |
196 REPORTER_ASSERT(reporter, nullptr == obj); | 194 REPORTER_ASSERT(reporter, nullptr == obj); |
197 | 195 |
198 // Make sure this succeeds when it should | 196 // Make sure this succeeds when it should |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 SkAutoTMalloc<unsigned char> data(size); | 539 SkAutoTMalloc<unsigned char> data(size); |
542 writer.writeToMemory(static_cast<void*>(data.get())); | 540 writer.writeToMemory(static_cast<void*>(data.get())); |
543 | 541 |
544 // Deserialize picture | 542 // Deserialize picture |
545 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); | 543 SkValidatingReadBuffer reader(static_cast<void*>(data.get()), size); |
546 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); | 544 sk_sp<SkPicture> readPict(SkPicture::MakeFromBuffer(reader)); |
547 REPORTER_ASSERT(reporter, readPict.get()); | 545 REPORTER_ASSERT(reporter, readPict.get()); |
548 } | 546 } |
549 | 547 |
550 TestPictureTypefaceSerialization(reporter); | 548 TestPictureTypefaceSerialization(reporter); |
551 | |
552 // Test SkLightingShader/NormalMapSource serialization | |
553 { | |
554 const int kTexSize = 2; | |
555 | |
556 SkLights::Builder builder; | |
557 | |
558 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
559 SkVector3::Make(1.0f, 0.0f, 0.0f))); | |
560 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | |
561 | |
562 sk_sp<SkLights> fLights = builder.finish(); | |
563 | |
564 SkBitmap diffuse = sk_tool_utils::create_checkerboard_bitmap( | |
565 kTexSize, kTexSize, | |
566 sk_tool_utils::color_to_565(0x0), | |
567 sk_tool_utils::color_to_565(0xFF804020), | |
568 8); | |
569 | |
570 SkRect bitmapBounds = SkRect::MakeIWH(diffuse.width(), diffuse.height())
; | |
571 | |
572 SkMatrix matrix; | |
573 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz
e)); | |
574 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit); | |
575 | |
576 SkVector invNormRotation = { SkScalarSqrt(0.3f), SkScalarSqrt(0.7f) }; | |
577 SkBitmap normals; | |
578 normals.allocN32Pixels(kTexSize, kTexSize); | |
579 | |
580 sk_tool_utils::create_frustum_normal_map(&normals, SkIRect::MakeWH(kTexS
ize, kTexSize)); | |
581 sk_sp<SkShader> lightingShader = SkLightingShader::Make(diffuse, normals
, fLights, | |
582 invNormRotation, &matrix, &matrix); | |
583 | |
584 TestFlattenableSerialization(lightingShader.get(), true, reporter); | |
585 // TODO test equality? | |
586 | |
587 } | |
588 } | 549 } |
589 | 550 |
590 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 551 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
591 #include "SkAnnotation.h" | 552 #include "SkAnnotation.h" |
592 | 553 |
593 static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { | 554 static sk_sp<SkPicture> copy_picture_via_serialization(SkPicture* src) { |
594 SkDynamicMemoryWStream wstream; | 555 SkDynamicMemoryWStream wstream; |
595 src->serialize(&wstream); | 556 src->serialize(&wstream); |
596 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); | 557 SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream()); |
597 return SkPicture::MakeFromStream(rstream); | 558 return SkPicture::MakeFromStream(rstream); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, | 621 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 }, |
661 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, | 622 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 }, |
662 }; | 623 }; |
663 | 624 |
664 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); | 625 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); |
665 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); | 626 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); |
666 | 627 |
667 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); | 628 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); |
668 canvas.drawPicture(pict1); | 629 canvas.drawPicture(pict1); |
669 } | 630 } |
OLD | NEW |