| halcanary@google.com | 1bed687 | 2014-01-02 17:29:28 +0000 | [diff] [blame^] | 1 | /* | 
 | 2 |  * Copyright 2013 Google Inc. | 
 | 3 |  * | 
 | 4 |  * Use of this source code is governed by a BSD-style license that can be | 
 | 5 |  * found in the LICENSE file. | 
 | 6 |  */ | 
 | 7 |  | 
 | 8 | #include "SkData.h" | 
 | 9 | #include "SkMallocPixelRef.h" | 
 | 10 | #include "Test.h" | 
 | 11 | #include "TestClassDef.h" | 
 | 12 |  | 
 | 13 | static void delete_uint8_proc(void* ptr, void*) { | 
 | 14 |     delete[] static_cast<uint8_t*>(ptr); | 
 | 15 | } | 
 | 16 |  | 
 | 17 | static void set_to_one_proc(void*, void* context) { | 
 | 18 |     *(static_cast<int*>(context)) = 1; | 
 | 19 | } | 
 | 20 |  | 
 | 21 | /** | 
 | 22 |  *  This test contains basic sanity checks concerning SkMallocPixelRef. | 
 | 23 |  */ | 
 | 24 | DEF_TEST(MallocPixelRef, reporter) { | 
 | 25 |     REPORTER_ASSERT(reporter, true); | 
 | 26 |     SkImageInfo info = {10, 13, kPMColor_SkColorType, kPremul_SkAlphaType}; | 
 | 27 |     { | 
 | 28 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 29 |             SkMallocPixelRef::NewAllocate(info, info.minRowBytes() - 1, NULL)); | 
 | 30 |         // rowbytes too small. | 
 | 31 |         REPORTER_ASSERT(reporter, NULL == pr.get()); | 
 | 32 |     } | 
 | 33 |     { | 
 | 34 |         size_t rowBytes = info.minRowBytes() - 1; | 
 | 35 |         size_t size = info.getSafeSize(rowBytes); | 
 | 36 |         void* addr = sk_malloc_throw(size); | 
 | 37 |         SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); | 
 | 38 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 39 |             SkMallocPixelRef::NewWithData(info, rowBytes, | 
 | 40 |                                           NULL, data.get())); | 
 | 41 |         // rowbytes too small. | 
 | 42 |         REPORTER_ASSERT(reporter, NULL == pr.get()); | 
 | 43 |     } | 
 | 44 |     { | 
 | 45 |         size_t rowBytes = info.minRowBytes() + 2; | 
 | 46 |         size_t size = info.getSafeSize(rowBytes) - 1; | 
 | 47 |         void* addr = sk_malloc_throw(size); | 
 | 48 |         SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); | 
 | 49 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 50 |             SkMallocPixelRef::NewWithData(info, rowBytes, NULL, | 
 | 51 |                                           data.get())); | 
 | 52 |         // data too small. | 
 | 53 |         REPORTER_ASSERT(reporter, NULL == pr.get()); | 
 | 54 |     } | 
 | 55 |     size_t rowBytes = info.minRowBytes() + 7; | 
 | 56 |     size_t size = info.getSafeSize(rowBytes) + 9; | 
 | 57 |     { | 
 | 58 |         SkAutoMalloc memory(size); | 
 | 59 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 60 |             SkMallocPixelRef::NewDirect(info, memory.get(), rowBytes, NULL)); | 
 | 61 |         REPORTER_ASSERT(reporter, pr.get() != NULL); | 
 | 62 |         REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); | 
 | 63 |     } | 
 | 64 |     { | 
 | 65 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 66 |             SkMallocPixelRef::NewAllocate(info, rowBytes, NULL)); | 
 | 67 |         REPORTER_ASSERT(reporter, pr.get() != NULL); | 
 | 68 |         REPORTER_ASSERT(reporter, NULL != pr->pixels()); | 
 | 69 |     } | 
 | 70 |     { | 
 | 71 |         void* addr = static_cast<void*>(new uint8_t[size]); | 
 | 72 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 73 |             SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr, | 
 | 74 |                                           delete_uint8_proc, NULL)); | 
 | 75 |         REPORTER_ASSERT(reporter, pr.get() != NULL); | 
 | 76 |         REPORTER_ASSERT(reporter, addr == pr->pixels()); | 
 | 77 |     } | 
 | 78 |     { | 
 | 79 |         int x = 0; | 
 | 80 |         SkAutoMalloc memory(size); | 
 | 81 |         REPORTER_ASSERT(reporter, memory.get() != NULL); | 
 | 82 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 83 |             SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, | 
 | 84 |                                           memory.get(), set_to_one_proc, | 
 | 85 |                                           static_cast<void*>(&x))); | 
 | 86 |         REPORTER_ASSERT(reporter, pr.get() != NULL); | 
 | 87 |         REPORTER_ASSERT(reporter, memory.get() == pr->pixels()); | 
 | 88 |         REPORTER_ASSERT(reporter, 0 == x); | 
 | 89 |         pr.reset(NULL); | 
 | 90 |         // make sure that set_to_one_proc was called. | 
 | 91 |         REPORTER_ASSERT(reporter, 1 == x); | 
 | 92 |     } | 
 | 93 |     { | 
 | 94 |         void* addr = static_cast<void*>(new uint8_t[size]); | 
 | 95 |         REPORTER_ASSERT(reporter, addr != NULL); | 
 | 96 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 97 |             SkMallocPixelRef::NewWithProc(info, rowBytes, NULL, addr, | 
 | 98 |                                           delete_uint8_proc, NULL)); | 
 | 99 |         REPORTER_ASSERT(reporter, addr == pr->pixels()); | 
 | 100 |     } | 
 | 101 |     { | 
 | 102 |         void* addr = sk_malloc_throw(size); | 
 | 103 |         SkAutoDataUnref data(SkData::NewFromMalloc(addr, size)); | 
 | 104 |         REPORTER_ASSERT(reporter, data.get() != NULL); | 
 | 105 |         SkData* dataPtr = data.get(); | 
 | 106 |         REPORTER_ASSERT(reporter, dataPtr->unique()); | 
 | 107 |         SkAutoTUnref<SkMallocPixelRef> pr( | 
 | 108 |             SkMallocPixelRef::NewWithData(info, rowBytes, NULL, data.get(), 4)); | 
 | 109 |         REPORTER_ASSERT(reporter, !(dataPtr->unique())); | 
 | 110 |         data.reset(NULL); | 
 | 111 |         REPORTER_ASSERT(reporter, dataPtr->unique()); | 
 | 112 |         REPORTER_ASSERT(reporter, | 
 | 113 |             static_cast<const void*>(dataPtr->bytes() + 4) == pr->pixels()); | 
 | 114 |     } | 
 | 115 | } | 
 | 116 |  |