1 /* 2 * Copyright 2012 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 "SkCGUtils.h" 9 #include "SkStream.h" 10 11 // This is used by CGDataProviderCreateWithData 12 13 static void unref_proc(void* info, const void* addr, size_t size) { 14 SkASSERT(info); 15 ((SkRefCnt*)info)->unref(); 16 } 17 18 // These are used by CGDataProviderSequentialCallbacks 19 20 static size_t get_bytes_proc(void* info, void* buffer, size_t bytes) { 21 SkASSERT(info); 22 return ((SkStream*)info)->read(buffer, bytes); 23 } 24 25 static off_t skip_forward_proc(void* info, off_t bytes) { 26 return ((SkStream*)info)->skip((size_t) bytes); 27 } 28 29 static void rewind_proc(void* info) { 30 SkASSERT(info); 31 ((SkStream*)info)->rewind(); 32 } 33 34 static void release_info_proc(void* info) { 35 SkASSERT(info); 36 ((SkStream*)info)->unref(); 37 } 38 39 CGDataProviderRef SkCreateDataProviderFromStream(SkStream* stream) { 40 stream->ref(); // unref will be called when the provider is deleted 41 42 const void* addr = stream->getMemoryBase(); 43 if (addr) { 44 // special-case when the stream is just a block of ram 45 return CGDataProviderCreateWithData(stream, addr, stream->getLength(), 46 unref_proc); 47 } 48 49 CGDataProviderSequentialCallbacks rec; 50 sk_bzero(&rec, sizeof(rec)); 51 rec.version = 0; 52 rec.getBytes = get_bytes_proc; 53 rec.skipForward = skip_forward_proc; 54 rec.rewind = rewind_proc; 55 rec.releaseInfo = release_info_proc; 56 return CGDataProviderCreateSequential(stream, &rec); 57 } 58 59 /////////////////////////////////////////////////////////////////////////////// 60 61 #include "SkData.h" 62 63 CGDataProviderRef SkCreateDataProviderFromData(SkData* data) { 64 data->ref(); 65 return CGDataProviderCreateWithData(data, data->data(), data->size(), 66 unref_proc); 67 } 68