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#ifndef SkBitmapFactory_DEFINED
9#define SkBitmapFactory_DEFINED
10
11#include "SkImage.h"
12#include "SkTypes.h"
13
14class SkBitmap;
15class SkData;
16class SkImageCache;
17
18/**
19 *  Factory for creating a bitmap from encoded data.
20 */
21class SkBitmapFactory {
22
23public:
24    /**
25     *  Struct containing information about a pixel destination.
26     */
27    struct Target {
28        /**
29         *  Pre-allocated memory.
30         */
31        void*  fAddr;
32
33        /**
34         *  Rowbytes of the allocated memory.
35         */
36        size_t fRowBytes;
37    };
38
39    /**
40     *  Signature for a function to decode an image from encoded data.
41     */
42    typedef bool (*DecodeProc)(const void* data, size_t length, SkImage::Info*, const Target*);
43
44    /**
45     *  Create a bitmap factory which uses DecodeProc for decoding.
46     *  @param DecodeProc Must not be NULL.
47     */
48    SkBitmapFactory(DecodeProc);
49
50    ~SkBitmapFactory();
51
52    /**
53     *  Set an image cache to use on pixelrefs provided by installPixelRef. Mutually exclusive
54     *  with fCacheSelector.
55     */
56    void setImageCache(SkImageCache* cache);
57
58    /**
59     *  Sets up an SkBitmap from encoded data. On success, the SkBitmap will have its Config,
60     *  width, height, rowBytes and pixelref set. If fImageCache is non-NULL, or if fCacheSelector
61     *  is set and returns non-NULL, the pixelref will lazily decode, and that SkImageCache will
62     *  handle the pixel memory. Otherwise installPixelRef will do an immediate decode.
63     *  @param SkData Encoded data.
64     *  @param SkBitmap to install the pixel ref on.
65     *  @return bool Whether or not a pixel ref was successfully installed.
66     */
67    bool installPixelRef(SkData*, SkBitmap*);
68
69    /**
70     *  An object for selecting an SkImageCache to use based on an SkImage::Info.
71     */
72    class CacheSelector : public SkRefCnt {
73
74    public:
75        SK_DECLARE_INST_COUNT(CacheSelector)
76        /**
77         *  Return an SkImageCache to use based on the provided SkImage::Info. If the caller decides
78         *  to hang on to the result, it will call ref, so the implementation should not add a ref
79         *  as a result of this call.
80         */
81        virtual SkImageCache* selectCache(const SkImage::Info&) = 0;
82
83    private:
84        typedef SkRefCnt INHERITED;
85    };
86
87    /**
88     *  Set the function to be used to select which SkImageCache to use. Mutually exclusive with
89     *  fImageCache.
90     */
91    void setCacheSelector(CacheSelector*);
92
93private:
94    DecodeProc     fDecodeProc;
95    SkImageCache*  fImageCache;
96    CacheSelector* fCacheSelector;
97};
98
99#endif // SkBitmapFactory_DEFINED
100