blob: e2d4d30ebff3a07aa07368262ba7b31f1427ca26 [file] [log] [blame]
reed92fc2ae2015-05-22 08:06:21 -07001/*
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
reed7aefe032015-06-08 10:22:22 -07008#include "SkColorPriv.h"
reed95d343f2015-05-23 13:21:06 -07009#include "SkConfig8888.h"
fmalita3a94c6c2016-02-04 13:09:59 -080010#include "SkData.h"
reed183b57f2015-06-05 14:33:17 -070011#include "SkMask.h"
reed92fc2ae2015-05-22 08:06:21 -070012#include "SkPixmap.h"
reed7aefe032015-06-08 10:22:22 -070013#include "SkUtils.h"
reed92fc2ae2015-05-22 08:06:21 -070014
15void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx) {
halcanary96fcdcc2015-08-27 07:41:13 -070016 SkASSERT(pm.addr() != nullptr);
reed92fc2ae2015-05-22 08:06:21 -070017
18 this->unlock();
19 fPixmap = pm;
20 fUnlockProc = unlock;
21 fUnlockContext = ctx;
22 fIsLocked = true;
23}
reed95d343f2015-05-23 13:21:06 -070024
25/////////////////////////////////////////////////////////////////////////////////////////////////
26
reed884e97c2015-05-26 11:31:54 -070027void SkPixmap::reset() {
halcanary96fcdcc2015-08-27 07:41:13 -070028 fPixels = nullptr;
29 fCTable = nullptr;
reed884e97c2015-05-26 11:31:54 -070030 fRowBytes = 0;
31 fInfo = SkImageInfo::MakeUnknown();
32}
33
34void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes, SkColorTable* ct) {
35 if (addr) {
36 SkASSERT(info.validRowBytes(rowBytes));
37 }
38 fPixels = addr;
39 fCTable = ct;
40 fRowBytes = rowBytes;
41 fInfo = info;
42}
43
reed183b57f2015-06-05 14:33:17 -070044bool SkPixmap::reset(const SkMask& src) {
45 if (SkMask::kA8_Format == src.fFormat) {
46 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
halcanary96fcdcc2015-08-27 07:41:13 -070047 src.fImage, src.fRowBytes, nullptr);
reed183b57f2015-06-05 14:33:17 -070048 return true;
49 }
50 this->reset();
51 return false;
52}
53
54bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
55 SkIRect srcRect, r;
56 srcRect.set(0, 0, this->width(), this->height());
57 if (!r.intersect(srcRect, subset)) {
58 return false; // r is empty (i.e. no intersection)
59 }
60
61 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
62 // exited above.
63 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
64 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
65
halcanary96fcdcc2015-08-27 07:41:13 -070066 const void* pixels = nullptr;
reed183b57f2015-06-05 14:33:17 -070067 if (fPixels) {
68 const size_t bpp = fInfo.bytesPerPixel();
69 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
70 }
71 result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTable);
72 return true;
73}
74
reed95d343f2015-05-23 13:21:06 -070075bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
76 int x, int y) const {
77 if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
78 return false;
79 }
halcanary96fcdcc2015-08-27 07:41:13 -070080 if (nullptr == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
reed95d343f2015-05-23 13:21:06 -070081 return false;
82 }
83 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
84 return false;
85 }
86
87 SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
88 if (!srcR.intersect(0, 0, this->width(), this->height())) {
89 return false;
90 }
91
92 // the intersect may have shrunk info's logical size
93 const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
94
95 // if x or y are negative, then we have to adjust pixels
96 if (x > 0) {
97 x = 0;
98 }
99 if (y > 0) {
100 y = 0;
101 }
102 // here x,y are either 0 or negative
103 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
104
105 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
106 const void* srcPixels = this->addr(srcR.x(), srcR.y());
107 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB,
108 srcInfo, srcPixels, this->rowBytes(), this->ctable());
109}
reed183b57f2015-06-05 14:33:17 -0700110
reed7aefe032015-06-08 10:22:22 -0700111static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
112 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
113 (SkR32To4444(r) << SK_R4444_SHIFT) |
114 (SkG32To4444(g) << SK_G4444_SHIFT) |
115 (SkB32To4444(b) << SK_B4444_SHIFT);
116 return SkToU16(pixel);
117}
118
119bool SkPixmap::erase(SkColor color, const SkIRect& inArea) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 if (nullptr == fPixels) {
reed7aefe032015-06-08 10:22:22 -0700121 return false;
122 }
123 SkIRect area;
124 if (!area.intersect(this->bounds(), inArea)) {
125 return false;
126 }
127
128 U8CPU a = SkColorGetA(color);
129 U8CPU r = SkColorGetR(color);
130 U8CPU g = SkColorGetG(color);
131 U8CPU b = SkColorGetB(color);
132
133 int height = area.height();
134 const int width = area.width();
135 const int rowBytes = this->rowBytes();
136
137 switch (this->colorType()) {
138 case kGray_8_SkColorType: {
139 if (255 != a) {
140 r = SkMulDiv255Round(r, a);
141 g = SkMulDiv255Round(g, a);
142 b = SkMulDiv255Round(b, a);
143 }
144 int gray = SkComputeLuminance(r, g, b);
145 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
146 while (--height >= 0) {
147 memset(p, gray, width);
148 p += rowBytes;
149 }
150 break;
151 }
152 case kAlpha_8_SkColorType: {
153 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
154 while (--height >= 0) {
155 memset(p, a, width);
156 p += rowBytes;
157 }
158 break;
159 }
160 case kARGB_4444_SkColorType:
161 case kRGB_565_SkColorType: {
162 uint16_t* p = this->writable_addr16(area.fLeft, area.fTop);
163 uint16_t v;
164
165 // make rgb premultiplied
166 if (255 != a) {
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800167 r = SkMulDiv255Round(r, a);
168 g = SkMulDiv255Round(g, a);
169 b = SkMulDiv255Round(b, a);
reed7aefe032015-06-08 10:22:22 -0700170 }
171
172 if (kARGB_4444_SkColorType == this->colorType()) {
173 v = pack_8888_to_4444(a, r, g, b);
174 } else {
175 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
176 g >> (8 - SK_G16_BITS),
177 b >> (8 - SK_B16_BITS));
178 }
179 while (--height >= 0) {
180 sk_memset16(p, v, width);
181 p = (uint16_t*)((char*)p + rowBytes);
182 }
183 break;
184 }
185 case kBGRA_8888_SkColorType:
186 case kRGBA_8888_SkColorType: {
187 uint32_t* p = this->writable_addr32(area.fLeft, area.fTop);
188
189 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800190 r = SkMulDiv255Round(r, a);
191 g = SkMulDiv255Round(g, a);
192 b = SkMulDiv255Round(b, a);
reed7aefe032015-06-08 10:22:22 -0700193 }
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800194 uint32_t v = kRGBA_8888_SkColorType == this->colorType()
195 ? SkPackARGB_as_RGBA(a, r, g, b)
196 : SkPackARGB_as_BGRA(a, r, g, b);
197
reed7aefe032015-06-08 10:22:22 -0700198 while (--height >= 0) {
199 sk_memset32(p, v, width);
200 p = (uint32_t*)((char*)p + rowBytes);
201 }
202 break;
203 }
204 default:
205 return false; // no change, so don't call notifyPixelsChanged()
206 }
207 return true;
208}
209
reed09553032015-11-23 12:32:16 -0800210#include "SkBitmap.h"
211#include "SkCanvas.h"
212#include "SkSurface.h"
213#include "SkXfermode.h"
214
215bool SkPixmap::scalePixels(const SkPixmap& dst, SkFilterQuality quality) const {
216 // Can't do anthing with empty src or dst
217 if (this->width() <= 0 || this->height() <= 0 || dst.width() <= 0 || dst.height() <= 0) {
218 return false;
219 }
220
221 // no scaling involved?
222 if (dst.width() == this->width() && dst.height() == this->height()) {
223 return this->readPixels(dst);
224 }
225
226 SkBitmap bitmap;
halcanarye36ec872015-12-09 11:36:59 -0800227 if (!bitmap.installPixels(*this)) {
reed09553032015-11-23 12:32:16 -0800228 return false;
229 }
230 bitmap.setIsVolatile(true); // so we don't try to cache it
231
232 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterDirect(dst.info(), dst.writable_addr(),
233 dst.rowBytes()));
234 if (!surface) {
235 return false;
236 }
237
238 SkPaint paint;
239 paint.setFilterQuality(quality);
240 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
241 surface->getCanvas()->drawBitmapRect(bitmap, SkRect::MakeIWH(dst.width(), dst.height()),
242 &paint);
243 return true;
244}
245
reed183b57f2015-06-05 14:33:17 -0700246//////////////////////////////////////////////////////////////////////////////////////////////////
247
halcanary96fcdcc2015-08-27 07:41:13 -0700248SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
reed183b57f2015-06-05 14:33:17 -0700249
250SkAutoPixmapStorage::~SkAutoPixmapStorage() {
reed7aefe032015-06-08 10:22:22 -0700251 this->freeStorage();
reed183b57f2015-06-05 14:33:17 -0700252}
253
254bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
reed7aefe032015-06-08 10:22:22 -0700255 this->freeStorage();
reed183b57f2015-06-05 14:33:17 -0700256
257 size_t rb = info.minRowBytes();
258 size_t size = info.getSafeSize(rb);
259 if (0 == size) {
260 return false;
261 }
reed7aefe032015-06-08 10:22:22 -0700262 void* pixels = sk_malloc_flags(size, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700263 if (nullptr == pixels) {
reed183b57f2015-06-05 14:33:17 -0700264 return false;
265 }
reed7aefe032015-06-08 10:22:22 -0700266 this->reset(info, pixels, rb);
267 fStorage = pixels;
reed183b57f2015-06-05 14:33:17 -0700268 return true;
269}
270
271void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
272 if (!this->tryAlloc(info)) {
273 sk_throw();
274 }
275}
fmalita3a94c6c2016-02-04 13:09:59 -0800276
277const SkData* SkAutoPixmapStorage::detachPixelsAsData() {
278 if (!fStorage) {
279 return nullptr;
280 }
281
282 const SkData* data = SkData::NewFromMalloc(fStorage, this->getSafeSize());
283 fStorage = nullptr;
284 this->INHERITED::reset();
285
286 return data;
287}