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
unref_data_proc(void * info,const void * addr,size_t size)13 static void unref_data_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
get_bytes_proc(void * info,void * buffer,size_t bytes)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
skip_forward_proc(void * info,off_t bytes)25 static off_t skip_forward_proc(void* info, off_t bytes) {
26 return ((SkStream*)info)->skip((size_t) bytes);
27 }
28
rewind_proc(void * info)29 static void rewind_proc(void* info) {
30 SkASSERT(info);
31 ((SkStream*)info)->rewind();
32 }
33
release_info_proc(void * info)34 static void release_info_proc(void* info) {
35 SkASSERT(info);
36 ((SkStream*)info)->unref();
37 }
38
SkCreateDataProviderFromStream(SkStream * stream)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_data_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