Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: include/core/SkPixmap.h

Issue 1074983003: add SkPixmap and external locking to bitmaps (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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 SkPixmap_DEFINED
9 #define SkPixmap_DEFINED
10
11 #include "SkImageInfo.h"
12
13 class SkColorTable;
14
15 /**
16 * Pairs SkImageInfo with actual pixels and rowbytes. This class does not try t o manage the
17 * lifetime of the pixel memory (nor the colortable if provided).
18 */
19 class SkPixmap {
20 public:
21 SkPixmap()
22 : fPixels(NULL), fCTable(NULL), fRowBytes(0), fInfo(SkImageInfo::MakeUnk nown(0, 0))
23 {}
24
25 SkPixmap(const SkImageInfo& info, const void* addr, size_t rowBytes,
26 SkColorTable* ctable = NULL)
27 : fPixels(addr), fCTable(ctable), fRowBytes(rowBytes), fInfo(info)
28 {
29 if (kIndex_8_SkColorType == info.colorType()) {
30 SkASSERT(ctable);
31 } else {
32 SkASSERT(NULL == ctable);
33 }
34 }
35
36 const SkImageInfo& info() const { return fInfo; }
37 size_t rowBytes() const { return fRowBytes; }
38 const void* addr() const { return fPixels; }
39 SkColorTable* ctable() const { return fCTable; }
40
41 int width() const { return fInfo.width(); }
42 int height() const { return fInfo.height(); }
43 SkColorType colorType() const { return fInfo.colorType(); }
44 SkAlphaType alphaType() const { return fInfo.alphaType(); }
45 bool isOpaque() const { return fInfo.isOpaque(); }
46
47 int64_t getSafeSize64() const { return fInfo.getSafeSize64(fRowBytes); }
48 size_t getSafeSize() const { return fInfo.getSafeSize(fRowBytes); }
49
50 const uint32_t* addr32() const {
51 SkASSERT(4 == SkColorTypeBytesPerPixel(fInfo.colorType()));
52 return reinterpret_cast<const uint32_t*>(fPixels);
53 }
54
55 const uint16_t* addr16() const {
56 SkASSERT(2 == SkColorTypeBytesPerPixel(fInfo.colorType()));
57 return reinterpret_cast<const uint16_t*>(fPixels);
58 }
59
60 const uint8_t* addr8() const {
61 SkASSERT(1 == SkColorTypeBytesPerPixel(fInfo.colorType()));
62 return reinterpret_cast<const uint8_t*>(fPixels);
63 }
64
65 const uint32_t* addr32(int x, int y) const {
66 SkASSERT((unsigned)x < (unsigned)fInfo.width());
67 SkASSERT((unsigned)y < (unsigned)fInfo.height());
68 return (const uint32_t*)((const char*)this->addr32() + y * fRowBytes + ( x << 2));
69 }
70 const uint16_t* addr16(int x, int y) const {
71 SkASSERT((unsigned)x < (unsigned)fInfo.width());
72 SkASSERT((unsigned)y < (unsigned)fInfo.height());
73 return (const uint16_t*)((const char*)this->addr16() + y * fRowBytes + ( x << 1));
74 }
75 const uint8_t* addr8(int x, int y) const {
76 SkASSERT((unsigned)x < (unsigned)fInfo.width());
77 SkASSERT((unsigned)y < (unsigned)fInfo.height());
78 return (const uint8_t*)((const char*)this->addr8() + y * fRowBytes + (x << 0));
79 }
80 const void* addr(int x, int y) const {
81 return (const char*)fPixels + fInfo.computeOffset(x, y, fRowBytes);
82 }
83
84 // Writable versions
85
86 void* writable_addr() const { return const_cast<void*>(fPixels); }
87 uint32_t* writable_addr32(int x, int y) const {
88 return const_cast<uint32_t*>(this->addr32(x, y));
89 }
90 uint16_t* writable_addr16(int x, int y) const {
91 return const_cast<uint16_t*>(this->addr16(x, y));
92 }
93 uint8_t* writable_addr8(int x, int y) const {
94 return const_cast<uint8_t*>(this->addr8(x, y));
95 }
96
97 private:
98 const void* fPixels;
99 SkColorTable* fCTable;
100 size_t fRowBytes;
101 SkImageInfo fInfo;
102 };
103
104 //////////////////////////////////////////////////////////////////////////////// /////////////
105
106 class SkAutoPixmapUnlock : ::SkNoncopyable {
107 public:
108 SkAutoPixmapUnlock() : fUnlockProc(NULL), fIsLocked(false) {}
109 SkAutoPixmapUnlock(const SkPixmap& pm, void (*unlock)(void*), void* ctx)
110 : fUnlockProc(unlock), fUnlockContext(ctx), fPixmap(pm), fIsLocked(true)
111 {}
112 ~SkAutoPixmapUnlock() { this->unlock(); }
113
114 /**
115 * Return the currently locked pixmap. Undefined if it has been unlocked.
116 */
117 const SkPixmap& pixmap() const {
118 SkASSERT(this->isLocked());
119 return fPixmap;
120 }
121
122 bool isLocked() const { return fIsLocked; }
123
124 /**
125 * Unlocks the pixmap. Can safely be called more than once as it will only call the underlying
126 * unlock-proc once.
127 */
128 void unlock() {
129 if (fUnlockProc) {
130 SkASSERT(fIsLocked);
131 fUnlockProc(fUnlockContext);
132 fUnlockProc = NULL;
133 fIsLocked = false;
134 }
135 }
136
137 /**
138 * If there is a currently locked pixmap, unlock it, then copy the specifie d pixmap
139 * and (optional) unlock proc/context.
140 */
141 void reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx);
142
143 private:
144 void (*fUnlockProc)(void*);
145 void* fUnlockContext;
146 SkPixmap fPixmap;
147 bool fIsLocked;
148
149 friend class SkBitmap;
150 };
scroggo 2015/05/22 18:42:33 Should this be SK_REQUIRE_LOCAL_VAR?
151
152 #endif
OLDNEW
« no previous file with comments | « include/core/SkPixelRef.h ('k') | src/core/SkBitmap.cpp » ('j') | src/core/SkPixelRef.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698