Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkMallocPixelRef.h
1 /*
2  * Copyright 2008 The Android Open Source Project
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
9 #ifndef SkMallocPixelRef_DEFINED
10 #define SkMallocPixelRef_DEFINED
11
12 #include "SkPixelRef.h"
13
14 /** We explicitly use the same allocator for our pixels that SkMask does,
15     so that we can freely assign memory allocated by one class to the other.
16 */
17 class SK_API SkMallocPixelRef : public SkPixelRef {
18 public:
19     SK_DECLARE_INST_COUNT(SkMallocPixelRef)
20     /**
21      *  Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
22      *  and optional colortable. The caller is responsible for managing the
23      *  lifetime of the pixel storage buffer, as this pixelref will not try
24      *  to delete it.
25      *
26      *  The pixelref will ref() the colortable (if not NULL).
27      *
28      *  Returns NULL on failure.
29      */
30     static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
31                                        size_t rowBytes, SkColorTable*);
32
33     /**
34      *  Return a new SkMallocPixelRef, automatically allocating storage for the
35      *  pixels. If rowBytes are 0, an optimal value will be chosen automatically.
36      *  If rowBytes is > 0, then it will be respected, or NULL will be returned
37      *  if rowBytes is invalid for the specified info.
38      *
39      *  This pixelref will ref() the specified colortable (if not NULL).
40      *
41      *  Returns NULL on failure.
42      */
43     static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
44                                          size_t rowBytes, SkColorTable*);
45
46     /**
47      *  Return a new SkMallocPixelRef with the provided pixel storage,
48      *  rowBytes, and optional colortable. On destruction, ReleaseProc
49      *  will be called.
50      *
51      *  This pixelref will ref() the specified colortable (if not NULL).
52      *
53      *  Returns NULL on failure.
54      */
55     typedef void (*ReleaseProc)(void* addr, void* context);
56     static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
57                                          size_t rowBytes, SkColorTable*,
58                                          void* addr, ReleaseProc proc,
59                                          void* context);
60
61     /**
62      *  Return a new SkMallocPixelRef that will use the provided
63      *  SkData, rowBytes, and optional colortable as pixel storage.
64      *  The SkData will be ref()ed and on destruction of the PielRef,
65      *  the SkData will be unref()ed.
66      *
67      *  @param offset (in bytes) into the provided SkData that the
68      *         first pixel is located at.
69      *
70      *  This pixelref will ref() the specified colortable (if not NULL).
71      *
72      *  Returns NULL on failure.
73      */
74     static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
75                                          size_t rowBytes,
76                                          SkColorTable* ctable,
77                                          SkData* data,
78                                          size_t offset = 0);
79
80     void* getAddr() const { return fStorage; }
81
82     class PRFactory : public SkPixelRefFactory {
83     public:
84         virtual SkPixelRef* create(const SkImageInfo&,
85                                    SkColorTable*) SK_OVERRIDE;
86     };
87
88     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkMallocPixelRef)
89
90 protected:
91     // The ownPixels version of this constructor is deprecated.
92     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
93                      bool ownPixels);
94     SkMallocPixelRef(SkReadBuffer& buffer);
95     virtual ~SkMallocPixelRef();
96
97     virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE;
98     virtual void onUnlockPixels() SK_OVERRIDE;
99     virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
100     virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
101
102 private:
103     void*           fStorage;
104     SkColorTable*   fCTable;
105     size_t          fRB;
106     ReleaseProc     fReleaseProc;
107     void*           fReleaseProcContext;
108
109     SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
110                      ReleaseProc proc, void* context);
111
112     typedef SkPixelRef INHERITED;
113 };
114
115
116 #endif