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

Side by Side Diff: tests/SerializationTest.cpp

Issue 2334123003: Add SkColor4f serialization (Closed)
Patch Set: Switched to memcpy, rebased Created 4 years, 2 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
« no previous file with comments | « src/core/SkWriteBuffer.cpp ('k') | tools/debugger/SkDrawCommand.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 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 "Resources.h"
9 #include "SkAnnotationKeys.h" 9 #include "SkAnnotationKeys.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 template<> struct SerializationUtils<SkColor> { 98 template<> struct SerializationUtils<SkColor> {
99 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) { 99 static void Write(SkWriteBuffer& writer, SkColor* data, uint32_t arraySize) {
100 writer.writeColorArray(data, arraySize); 100 writer.writeColorArray(data, arraySize);
101 } 101 }
102 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arr aySize) { 102 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arr aySize) {
103 return reader.readColorArray(data, arraySize); 103 return reader.readColorArray(data, arraySize);
104 } 104 }
105 }; 105 };
106 106
107 template<> struct SerializationUtils<SkColor4f> {
108 static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize ) {
109 writer.writeColor4fArray(data, arraySize);
110 }
111 static bool Read(SkValidatingReadBuffer& reader, SkColor4f* data, uint32_t a rraySize) {
112 return reader.readColor4fArray(data, arraySize);
113 }
114 };
115
107 template<> struct SerializationUtils<int32_t> { 116 template<> struct SerializationUtils<int32_t> {
108 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) { 117 static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
109 writer.writeIntArray(data, arraySize); 118 writer.writeIntArray(data, arraySize);
110 } 119 }
111 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arr aySize) { 120 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arr aySize) {
112 return reader.readIntArray(data, arraySize); 121 return reader.readIntArray(data, arraySize);
113 } 122 }
114 }; 123 };
115 124
116 template<> struct SerializationUtils<SkPoint> { 125 template<> struct SerializationUtils<SkPoint> {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 227 }
219 228
220 template<typename T> 229 template<typename T>
221 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { 230 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
222 SkBinaryWriteBuffer writer; 231 SkBinaryWriteBuffer writer;
223 SerializationUtils<T>::Write(writer, data, kArraySize); 232 SerializationUtils<T>::Write(writer, data, kArraySize);
224 size_t bytesWritten = writer.bytesWritten(); 233 size_t bytesWritten = writer.bytesWritten();
225 // This should write the length (in 4 bytes) and the array 234 // This should write the length (in 4 bytes) and the array
226 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); 235 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
227 236
228 unsigned char dataWritten[1024]; 237 unsigned char dataWritten[2048];
229 writer.writeToMemory(dataWritten); 238 writer.writeToMemory(dataWritten);
230 239
231 // Make sure this fails when it should 240 // Make sure this fails when it should
232 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); 241 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
233 T dataRead[kArraySize]; 242 T dataRead[kArraySize];
234 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2) ; 243 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2) ;
235 // This should have failed, since the provided size was too small 244 // This should have failed, since the provided size was too small
236 REPORTER_ASSERT(reporter, !success); 245 REPORTER_ASSERT(reporter, !success);
237 246
238 // Make sure this succeeds when it should 247 // Make sure this succeeds when it should
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 unsigned char data[kArraySize] = { 1, 2, 3 }; 496 unsigned char data[kArraySize] = { 1, 2, 3 };
488 TestArraySerialization(data, reporter); 497 TestArraySerialization(data, reporter);
489 } 498 }
490 499
491 // Test readColorArray 500 // Test readColorArray
492 { 501 {
493 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED } ; 502 SkColor data[kArraySize] = { SK_ColorBLACK, SK_ColorWHITE, SK_ColorRED } ;
494 TestArraySerialization(data, reporter); 503 TestArraySerialization(data, reporter);
495 } 504 }
496 505
506 // Test readColor4fArray
507 {
508 SkColor4f data[kArraySize] = {
509 SkColor4f::FromColor(SK_ColorBLACK),
510 SkColor4f::FromColor(SK_ColorWHITE),
511 SkColor4f::FromColor(SK_ColorRED),
512 { 1.f, 2.f, 4.f, 8.f }
513 };
514 TestArraySerialization(data, reporter);
515 }
516
497 // Test readIntArray 517 // Test readIntArray
498 { 518 {
499 int32_t data[kArraySize] = { 1, 2, 4, 8 }; 519 int32_t data[kArraySize] = { 1, 2, 4, 8 };
500 TestArraySerialization(data, reporter); 520 TestArraySerialization(data, reporter);
501 } 521 }
502 522
503 // Test readPointArray 523 // Test readPointArray
504 { 524 {
505 SkPoint data[kArraySize] = { {6, 7}, {42, 128} }; 525 SkPoint data[kArraySize] = { {6, 7}, {42, 128} };
506 TestArraySerialization(data, reporter); 526 TestArraySerialization(data, reporter);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) }, 714 { r1, SkAnnotationKeys::Define_Named_Dest_Key(), std::move(d1) },
695 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) }, 715 { r2, SkAnnotationKeys::Link_Named_Dest_Key(), std::move(d2) },
696 }; 716 };
697 717
698 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture()); 718 sk_sp<SkPicture> pict0(recorder.finishRecordingAsPicture());
699 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get())); 719 sk_sp<SkPicture> pict1(copy_picture_via_serialization(pict0.get()));
700 720
701 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs)); 721 TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
702 canvas.drawPicture(pict1); 722 canvas.drawPicture(pict1);
703 } 723 }
OLDNEW
« no previous file with comments | « src/core/SkWriteBuffer.cpp ('k') | tools/debugger/SkDrawCommand.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698