blob: ab337b9241ec72a9364b0d35cbbe70f257b45fc0 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#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*/
commit-bot@chromium.org3aa7a022014-01-16 14:46:16 +000017class SK_API SkMallocPixelRef : public SkPixelRef {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018public:
reed@google.combf790232013-12-13 19:45:58 +000019 /**
20 * Return a new SkMallocPixelRef with the provided pixel storage, rowBytes,
21 * and optional colortable. The caller is responsible for managing the
22 * lifetime of the pixel storage buffer, as this pixelref will not try
23 * to delete it.
24 *
25 * The pixelref will ref() the colortable (if not NULL).
26 *
27 * Returns NULL on failure.
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 */
reed@google.combf790232013-12-13 19:45:58 +000029 static SkMallocPixelRef* NewDirect(const SkImageInfo&, void* addr,
30 size_t rowBytes, SkColorTable*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031
reed@google.combf790232013-12-13 19:45:58 +000032 /**
33 * Return a new SkMallocPixelRef, automatically allocating storage for the
reed@google.comd0419b12014-01-06 17:08:27 +000034 * pixels. If rowBytes are 0, an optimal value will be chosen automatically.
35 * If rowBytes is > 0, then it will be respected, or NULL will be returned
36 * if rowBytes is invalid for the specified info.
reed@google.combf790232013-12-13 19:45:58 +000037 *
38 * This pixelref will ref() the specified colortable (if not NULL).
39 *
40 * Returns NULL on failure.
41 */
42 static SkMallocPixelRef* NewAllocate(const SkImageInfo& info,
43 size_t rowBytes, SkColorTable*);
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000044
halcanary@google.com1bed6872014-01-02 17:29:28 +000045 /**
mtkleincd495412015-11-05 09:46:23 -080046 * Identical to NewAllocate, except all pixel bytes are zeroed.
47 */
48 static SkMallocPixelRef* NewZeroed(const SkImageInfo& info,
49 size_t rowBytes, SkColorTable*);
50
51 /**
halcanary@google.com1bed6872014-01-02 17:29:28 +000052 * Return a new SkMallocPixelRef with the provided pixel storage,
53 * rowBytes, and optional colortable. On destruction, ReleaseProc
54 * will be called.
55 *
56 * This pixelref will ref() the specified colortable (if not NULL).
57 *
scroggo527b2872015-01-29 13:52:13 -080058 * If ReleaseProc is NULL, the pixels will never be released. This
59 * can be useful if the pixels were stack allocated. However, such an
60 * SkMallocPixelRef must not live beyond its pixels (e.g. by copying
61 * an SkBitmap pointing to it, or drawing to an SkPicture).
62 *
halcanary@google.com1bed6872014-01-02 17:29:28 +000063 * Returns NULL on failure.
64 */
65 typedef void (*ReleaseProc)(void* addr, void* context);
66 static SkMallocPixelRef* NewWithProc(const SkImageInfo& info,
67 size_t rowBytes, SkColorTable*,
68 void* addr, ReleaseProc proc,
69 void* context);
70
71 /**
72 * Return a new SkMallocPixelRef that will use the provided
73 * SkData, rowBytes, and optional colortable as pixel storage.
74 * The SkData will be ref()ed and on destruction of the PielRef,
75 * the SkData will be unref()ed.
76 *
halcanary@google.com1bed6872014-01-02 17:29:28 +000077 * This pixelref will ref() the specified colortable (if not NULL).
78 *
79 * Returns NULL on failure.
80 */
81 static SkMallocPixelRef* NewWithData(const SkImageInfo& info,
82 size_t rowBytes,
83 SkColorTable* ctable,
commit-bot@chromium.org2c4e75c2014-04-21 21:08:14 +000084 SkData* data);
halcanary@google.com1bed6872014-01-02 17:29:28 +000085
reed@android.comf2b98d62010-12-20 18:26:13 +000086 void* getAddr() const { return fStorage; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
reed@google.com9ebcac52014-01-24 18:53:42 +000088 class PRFactory : public SkPixelRefFactory {
89 public:
mtkleincd495412015-11-05 09:46:23 -080090 SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override;
91 };
92
93 class ZeroedPRFactory : public SkPixelRefFactory {
94 public:
95 SkPixelRef* create(const SkImageInfo&, size_t rowBytes, SkColorTable*) override;
reed@google.com9ebcac52014-01-24 18:53:42 +000096 };
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098protected:
halcanary@google.com1bed6872014-01-02 17:29:28 +000099 // The ownPixels version of this constructor is deprecated.
reed@google.combf790232013-12-13 19:45:58 +0000100 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
101 bool ownPixels);
reed@google.combf790232013-12-13 19:45:58 +0000102 virtual ~SkMallocPixelRef();
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +0000103
mtklein36352bf2015-03-25 18:17:31 -0700104 bool onNewLockPixels(LockRec*) override;
105 void onUnlockPixels() override;
106 size_t getAllocatedSizeInBytes() const override;
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108private:
mtkleincd495412015-11-05 09:46:23 -0800109 // Uses alloc to implement NewAllocate or NewZeroed.
110 static SkMallocPixelRef* NewUsing(void*(*alloc)(size_t),
111 const SkImageInfo&,
112 size_t rowBytes,
113 SkColorTable*);
114
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +0000115 void* fStorage;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +0000116 SkColorTable* fCTable;
reed@google.combf790232013-12-13 19:45:58 +0000117 size_t fRB;
halcanary@google.com1bed6872014-01-02 17:29:28 +0000118 ReleaseProc fReleaseProc;
119 void* fReleaseProcContext;
120
121 SkMallocPixelRef(const SkImageInfo&, void* addr, size_t rb, SkColorTable*,
122 ReleaseProc proc, void* context);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
124 typedef SkPixelRef INHERITED;
125};
126
reed@android.comf2b98d62010-12-20 18:26:13 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128#endif