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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkMallocPixelRef.h" | 11 #include "SkMallocPixelRef.h" |
12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
13 #include "SkValidatingReadBuffer.h" | 13 #include "SkValidatingReadBuffer.h" |
14 #include "SkXfermodeImageFilter.h" | 14 #include "SkXfermodeImageFilter.h" |
15 #include "Test.h" | 15 #include "Test.h" |
16 | 16 |
17 static const uint32_t kArraySize = 64; | 17 static const uint32_t kArraySize = 64; |
| 18 static const int kBitmapSize = 256; |
18 | 19 |
19 template<typename T> | 20 template<typename T> |
20 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { | 21 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { |
21 // Test memory read/write functions directly | 22 // Test memory read/write functions directly |
22 unsigned char dataWritten[1024]; | 23 unsigned char dataWritten[1024]; |
23 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); | 24 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); |
24 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMe
mory); | 25 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMe
mory); |
25 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWritt
enToMemory); | 26 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWritt
enToMemory); |
26 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemo
ry); | 27 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemo
ry); |
27 } | 28 } |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 bitmap.allocN32Pixels(24, 24); | 223 bitmap.allocN32Pixels(24, 24); |
223 SkCanvas canvas(bitmap); | 224 SkCanvas canvas(bitmap); |
224 canvas.clear(0x00000000); | 225 canvas.clear(0x00000000); |
225 SkPaint paint; | 226 SkPaint paint; |
226 paint.setImageFilter(deserializedFilter); | 227 paint.setImageFilter(deserializedFilter); |
227 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(
24))); | 228 canvas.clipRect(SkRect::MakeXYWH(0, 0, SkIntToScalar(24), SkIntToScalar(
24))); |
228 canvas.drawBitmap(bitmap, 0, 0, &paint); | 229 canvas.drawBitmap(bitmap, 0, 0, &paint); |
229 } | 230 } |
230 } | 231 } |
231 | 232 |
| 233 static bool setup_bitmap_for_canvas(SkBitmap* bitmap) { |
| 234 SkImageInfo info = SkImageInfo::Make( |
| 235 kBitmapSize, kBitmapSize, kPMColor_SkColorType, kPremul_SkAlphaType); |
| 236 return bitmap->allocPixels(info); |
| 237 } |
| 238 |
| 239 static bool make_checkerboard_bitmap(SkBitmap& bitmap) { |
| 240 bool success = setup_bitmap_for_canvas(&bitmap); |
| 241 |
| 242 SkCanvas canvas(bitmap); |
| 243 canvas.clear(0x00000000); |
| 244 SkPaint darkPaint; |
| 245 darkPaint.setColor(0xFF804020); |
| 246 SkPaint lightPaint; |
| 247 lightPaint.setColor(0xFF244484); |
| 248 const int i = kBitmapSize / 8; |
| 249 const SkScalar f = SkIntToScalar(i); |
| 250 for (int y = 0; y < kBitmapSize; y += i) { |
| 251 for (int x = 0; x < kBitmapSize; x += i) { |
| 252 canvas.save(); |
| 253 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 254 canvas.drawRect(SkRect::MakeXYWH(0, 0, f, f), darkPaint); |
| 255 canvas.drawRect(SkRect::MakeXYWH(f, 0, f, f), lightPaint); |
| 256 canvas.drawRect(SkRect::MakeXYWH(0, f, f, f), lightPaint); |
| 257 canvas.drawRect(SkRect::MakeXYWH(f, f, f, f), darkPaint); |
| 258 canvas.restore(); |
| 259 } |
| 260 } |
| 261 |
| 262 return success; |
| 263 } |
| 264 |
| 265 static bool drawSomething(SkCanvas* canvas) { |
| 266 SkPaint paint; |
| 267 SkBitmap bitmap; |
| 268 bool success = make_checkerboard_bitmap(bitmap); |
| 269 |
| 270 canvas->save(); |
| 271 canvas->scale(0.5f, 0.5f); |
| 272 canvas->drawBitmap(bitmap, 0, 0, NULL); |
| 273 canvas->restore(); |
| 274 |
| 275 const char beforeStr[] = "before circle"; |
| 276 const char afterStr[] = "after circle"; |
| 277 |
| 278 paint.setAntiAlias(true); |
| 279 |
| 280 paint.setColor(SK_ColorRED); |
| 281 canvas->drawData(beforeStr, sizeof(beforeStr)); |
| 282 canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2
), SkIntToScalar(kBitmapSize/3), paint); |
| 283 canvas->drawData(afterStr, sizeof(afterStr)); |
| 284 paint.setColor(SK_ColorBLACK); |
| 285 paint.setTextSize(SkIntToScalar(kBitmapSize/3)); |
| 286 canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(k
BitmapSize/4), paint); |
| 287 |
| 288 return success; |
| 289 } |
| 290 |
232 DEF_TEST(Serialization, reporter) { | 291 DEF_TEST(Serialization, reporter) { |
233 // Test matrix serialization | 292 // Test matrix serialization |
234 { | 293 { |
235 SkMatrix matrix = SkMatrix::I(); | 294 SkMatrix matrix = SkMatrix::I(); |
236 TestObjectSerialization(&matrix, reporter); | 295 TestObjectSerialization(&matrix, reporter); |
237 } | 296 } |
238 | 297 |
239 // Test path serialization | 298 // Test path serialization |
240 { | 299 { |
241 SkPath path; | 300 SkPath path; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 344 } |
286 | 345 |
287 // Test readScalarArray | 346 // Test readScalarArray |
288 { | 347 { |
289 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; | 348 SkScalar data[kArraySize] = { SK_Scalar1, SK_ScalarHalf, SK_ScalarMax }; |
290 TestArraySerialization(data, reporter); | 349 TestArraySerialization(data, reporter); |
291 } | 350 } |
292 | 351 |
293 // Test invalid deserializations | 352 // Test invalid deserializations |
294 { | 353 { |
295 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); | 354 SkImageInfo info = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
296 | 355 |
297 SkBitmap validBitmap; | 356 SkBitmap validBitmap; |
298 validBitmap.setConfig(info); | 357 validBitmap.setConfig(info); |
299 | 358 |
300 // Create a bitmap with a really large height | 359 // Create a bitmap with a really large height |
301 info.fHeight = 1000000000; | 360 info.fHeight = 1000000000; |
302 SkBitmap invalidBitmap; | 361 SkBitmap invalidBitmap; |
303 invalidBitmap.setConfig(info); | 362 invalidBitmap.setConfig(info); |
304 | 363 |
305 // The deserialization should succeed, and the rendering shouldn't crash
, | 364 // The deserialization should succeed, and the rendering shouldn't crash
, |
306 // even when the device fails to initialize, due to its size | 365 // even when the device fails to initialize, due to its size |
307 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); | 366 TestBitmapSerialization(validBitmap, invalidBitmap, true, reporter); |
308 } | 367 } |
| 368 |
| 369 // Test simple SkPicture serialization |
| 370 { |
| 371 SkPicture* pict = new SkPicture; |
| 372 SkAutoUnref aur(pict); |
| 373 bool didDraw = drawSomething(pict->beginRecording(kBitmapSize, kBitmapSi
ze)); |
| 374 REPORTER_ASSERT(reporter, didDraw); |
| 375 pict->endRecording(); |
| 376 |
| 377 // Serialize picture |
| 378 SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); |
| 379 pict->flatten(writer); |
| 380 size_t size = writer.bytesWritten(); |
| 381 void* data = sk_malloc_throw(size); |
| 382 writer.writeToMemory(data); |
| 383 |
| 384 // Deserialize picture |
| 385 SkValidatingReadBuffer reader(data, size); |
| 386 SkPicture* readPict(SkPicture::CreateFromBuffer(reader)); |
| 387 REPORTER_ASSERT(reporter, NULL != readPict); |
| 388 } |
309 } | 389 } |
OLD | NEW |