1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkColorPriv.h"
#include "SkConfig8888.h"
#include "SkMask.h"
#include "SkPixmap.h"
#include "SkUtils.h"
void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx) {
SkASSERT(pm.addr() != nullptr);
this->unlock();
fPixmap = pm;
fUnlockProc = unlock;
fUnlockContext = ctx;
fIsLocked = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void SkPixmap::reset() {
fPixels = nullptr;
fCTable = nullptr;
fRowBytes = 0;
fInfo = SkImageInfo::MakeUnknown();
}
void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes, SkColorTable* ct) {
if (addr) {
SkASSERT(info.validRowBytes(rowBytes));
}
fPixels = addr;
fCTable = ct;
fRowBytes = rowBytes;
fInfo = info;
}
bool SkPixmap::reset(const SkMask& src) {
if (SkMask::kA8_Format == src.fFormat) {
this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
src.fImage, src.fRowBytes, nullptr);
return true;
}
this->reset();
return false;
}
bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
SkIRect srcRect, r;
srcRect.set(0, 0, this->width(), this->height());
if (!r.intersect(srcRect, subset)) {
return false; // r is empty (i.e. no intersection)
}
// If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
// exited above.
SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
const void* pixels = nullptr;
if (fPixels) {
const size_t bpp = fInfo.bytesPerPixel();
pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
}
result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTable);
return true;
}
bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
int x, int y) const {
if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
return false;
}
if (nullptr == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
return false;
}
if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
return false;
}
SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
if (!srcR.intersect(0, 0, this->width(), this->height())) {
return false;
}
// the intersect may have shrunk info's logical size
const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
// if x or y are negative, then we have to adjust pixels
if (x > 0) {
x = 0;
}
if (y > 0) {
y = 0;
}
// here x,y are either 0 or negative
dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
const void* srcPixels = this->addr(srcR.x(), srcR.y());
return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB,
srcInfo, srcPixels, this->rowBytes(), this->ctable());
}
static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
(SkR32To4444(r) << SK_R4444_SHIFT) |
(SkG32To4444(g) << SK_G4444_SHIFT) |
(SkB32To4444(b) << SK_B4444_SHIFT);
return SkToU16(pixel);
}
bool SkPixmap::erase(SkColor color, const SkIRect& inArea) const {
if (nullptr == fPixels) {
return false;
}
SkIRect area;
if (!area.intersect(this->bounds(), inArea)) {
return false;
}
U8CPU a = SkColorGetA(color);
U8CPU r = SkColorGetR(color);
U8CPU g = SkColorGetG(color);
U8CPU b = SkColorGetB(color);
int height = area.height();
const int width = area.width();
const int rowBytes = this->rowBytes();
switch (this->colorType()) {
case kGray_8_SkColorType: {
if (255 != a) {
r = SkMulDiv255Round(r, a);
g = SkMulDiv255Round(g, a);
b = SkMulDiv255Round(b, a);
}
int gray = SkComputeLuminance(r, g, b);
uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
while (--height >= 0) {
memset(p, gray, width);
p += rowBytes;
}
break;
}
case kAlpha_8_SkColorType: {
uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
while (--height >= 0) {
memset(p, a, width);
p += rowBytes;
}
break;
}
case kARGB_4444_SkColorType:
case kRGB_565_SkColorType: {
uint16_t* p = this->writable_addr16(area.fLeft, area.fTop);
uint16_t v;
// make rgb premultiplied
if (255 != a) {
r = SkAlphaMul(r, a);
g = SkAlphaMul(g, a);
b = SkAlphaMul(b, a);
}
if (kARGB_4444_SkColorType == this->colorType()) {
v = pack_8888_to_4444(a, r, g, b);
} else {
v = SkPackRGB16(r >> (8 - SK_R16_BITS),
g >> (8 - SK_G16_BITS),
b >> (8 - SK_B16_BITS));
}
while (--height >= 0) {
sk_memset16(p, v, width);
p = (uint16_t*)((char*)p + rowBytes);
}
break;
}
case kBGRA_8888_SkColorType:
case kRGBA_8888_SkColorType: {
uint32_t* p = this->writable_addr32(area.fLeft, area.fTop);
if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
r = SkAlphaMul(r, a);
g = SkAlphaMul(g, a);
b = SkAlphaMul(b, a);
}
uint32_t v = kRGBA_8888_SkColorType == this->colorType() ?
SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b);
while (--height >= 0) {
sk_memset32(p, v, width);
p = (uint32_t*)((char*)p + rowBytes);
}
break;
}
default:
return false; // no change, so don't call notifyPixelsChanged()
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
SkAutoPixmapStorage::~SkAutoPixmapStorage() {
this->freeStorage();
}
bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
this->freeStorage();
size_t rb = info.minRowBytes();
size_t size = info.getSafeSize(rb);
if (0 == size) {
return false;
}
void* pixels = sk_malloc_flags(size, 0);
if (nullptr == pixels) {
return false;
}
this->reset(info, pixels, rb);
fStorage = pixels;
return true;
}
void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
if (!this->tryAlloc(info)) {
sk_throw();
}
}
|