michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkCGUtils.h" michael@0: #include "SkStream.h" michael@0: michael@0: // This is used by CGDataProviderCreateWithData michael@0: michael@0: static void unref_proc(void* info, const void* addr, size_t size) { michael@0: SkASSERT(info); michael@0: ((SkRefCnt*)info)->unref(); michael@0: } michael@0: michael@0: // These are used by CGDataProviderSequentialCallbacks michael@0: michael@0: static size_t get_bytes_proc(void* info, void* buffer, size_t bytes) { michael@0: SkASSERT(info); michael@0: return ((SkStream*)info)->read(buffer, bytes); michael@0: } michael@0: michael@0: static off_t skip_forward_proc(void* info, off_t bytes) { michael@0: return ((SkStream*)info)->skip((size_t) bytes); michael@0: } michael@0: michael@0: static void rewind_proc(void* info) { michael@0: SkASSERT(info); michael@0: ((SkStream*)info)->rewind(); michael@0: } michael@0: michael@0: static void release_info_proc(void* info) { michael@0: SkASSERT(info); michael@0: ((SkStream*)info)->unref(); michael@0: } michael@0: michael@0: CGDataProviderRef SkCreateDataProviderFromStream(SkStream* stream) { michael@0: stream->ref(); // unref will be called when the provider is deleted michael@0: michael@0: const void* addr = stream->getMemoryBase(); michael@0: if (addr) { michael@0: // special-case when the stream is just a block of ram michael@0: return CGDataProviderCreateWithData(stream, addr, stream->getLength(), michael@0: unref_proc); michael@0: } michael@0: michael@0: CGDataProviderSequentialCallbacks rec; michael@0: sk_bzero(&rec, sizeof(rec)); michael@0: rec.version = 0; michael@0: rec.getBytes = get_bytes_proc; michael@0: rec.skipForward = skip_forward_proc; michael@0: rec.rewind = rewind_proc; michael@0: rec.releaseInfo = release_info_proc; michael@0: return CGDataProviderCreateSequential(stream, &rec); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkData.h" michael@0: michael@0: CGDataProviderRef SkCreateDataProviderFromData(SkData* data) { michael@0: data->ref(); michael@0: return CGDataProviderCreateWithData(data, data->data(), data->size(), michael@0: unref_proc); michael@0: }