| 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 |
#include "include/core/SkBitmap.h" |
| 9 |
|
| 10 |
#include "include/core/SkColorSpace.h" // IWYU pragma: keep |
| 11 |
#include "include/core/SkColorType.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkMallocPixelRef.h" |
| 14 |
#include "include/core/SkMatrix.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPixelRef.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkShader.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/private/base/SkAlign.h" |
| 21 |
#include "include/private/base/SkTFitsIn.h" |
| 22 |
#include "include/private/base/SkTemplates.h" |
| 23 |
#include "include/private/base/SkTo.h" |
| 24 |
#include "src/core/SkConvertPixels.h" |
| 25 |
#include "src/core/SkImageInfoPriv.h" |
| 26 |
#include "src/core/SkMask.h" |
| 27 |
#include "src/core/SkMaskFilterBase.h" |
| 28 |
#include "src/core/SkMipmap.h" |
| 29 |
#include "src/core/SkPixelRefPriv.h" |
| 30 |
#include "src/core/SkWritePixelsRec.h" |
| 31 |
#include "src/image/SkImage_Raster.h" |
| 32 |
#include "src/shaders/SkImageShader.h" |
| 33 |
|
| 34 |
#include <cstring> |
| 35 |
#include <utility> |
| 36 |
class SkMaskFilter; |
| 37 |
|
| 38 |
static bool reset_return_false(SkBitmap* bm) { |
| 39 |
bm->reset(); |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
SkBitmap::SkBitmap() {} |
| 44 |
|
| 45 |
SkBitmap::SkBitmap(const SkBitmap& src) : fPixelRef(src.fPixelRef), fPixmap(src.fPixmap) { |
| 46 |
SkDEBUGCODE(src.validate();) |
| 47 |
SkDEBUGCODE(this->validate();) |
| 48 |
} |
| 49 |
|
| 50 |
SkBitmap::SkBitmap(SkBitmap&& other) |
| 51 |
: fPixelRef(std::move(other.fPixelRef)), fPixmap(std::move(other.fPixmap)) { |
| 52 |
SkASSERT(!other.fPixelRef); |
| 53 |
other.fPixmap.reset(); |
| 54 |
} |
| 55 |
|
| 56 |
SkBitmap::~SkBitmap() {} |
| 57 |
|
| 58 |
SkBitmap& SkBitmap::operator=(const SkBitmap& src) { |
| 59 |
if (this != &src) { |
| 60 |
fPixelRef = src.fPixelRef; |
| 61 |
fPixmap = src.fPixmap; |
| 62 |
} |
| 63 |
SkDEBUGCODE(this->validate();) |
| 64 |
return *this; |
| 65 |
} |
| 66 |
|
| 67 |
SkBitmap& SkBitmap::operator=(SkBitmap&& other) { |
| 68 |
if (this != &other) { |
| 69 |
fPixelRef = std::move(other.fPixelRef); |
| 70 |
fPixmap = std::move(other.fPixmap); |
| 71 |
SkASSERT(!other.fPixelRef); |
| 72 |
other.fPixmap.reset(); |
| 73 |
} |
| 74 |
return *this; |
| 75 |
} |
| 76 |
|
| 77 |
void SkBitmap::swap(SkBitmap& other) { |
| 78 |
using std::swap; |
| 79 |
swap(*this, other); |
| 80 |
SkDEBUGCODE(this->validate();) |
| 81 |
} |
| 82 |
|
| 83 |
void SkBitmap::reset() { |
| 84 |
fPixelRef = nullptr; // Free pixels. |
| 85 |
fPixmap.reset(); |
| 86 |
} |
| 87 |
|
| 88 |
void SkBitmap::getBounds(SkRect* bounds) const { |
| 89 |
SkASSERT(bounds); |
| 90 |
*bounds = SkRect::Make(this->dimensions()); |
| 91 |
} |
| 92 |
|
| 93 |
void SkBitmap::getBounds(SkIRect* bounds) const { |
| 94 |
SkASSERT(bounds); |
| 95 |
*bounds = fPixmap.bounds(); |
| 96 |
} |
| 97 |
|
| 98 |
SkColorSpace* SkBitmap::colorSpace() const { return fPixmap.colorSpace(); } |
| 99 |
|
| 100 |
sk_sp<SkColorSpace> SkBitmap::refColorSpace() const { return fPixmap.info().refColorSpace(); } |
| 101 |
|
| 102 |
/////////////////////////////////////////////////////////////////////////////// |
| 103 |
|
| 104 |
bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) { |
| 105 |
SkAlphaType newAT = info.alphaType(); |
| 106 |
if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) { |
| 107 |
return reset_return_false(this); |
| 108 |
} |
| 109 |
// don't look at info.alphaType(), since newAT is the real value... |
| 110 |
|
| 111 |
// require that rowBytes fit in 31bits |
| 112 |
int64_t mrb = info.minRowBytes64(); |
| 113 |
if (!SkTFitsIn<int32_t>(mrb)) { |
| 114 |
return reset_return_false(this); |
| 115 |
} |
| 116 |
if (!SkTFitsIn<int32_t>(rowBytes)) { |
| 117 |
return reset_return_false(this); |
| 118 |
} |
| 119 |
|
| 120 |
if (info.width() < 0 || info.height() < 0) { |
| 121 |
return reset_return_false(this); |
| 122 |
} |
| 123 |
|
| 124 |
if (kUnknown_SkColorType == info.colorType()) { |
| 125 |
rowBytes = 0; |
| 126 |
} else if (0 == rowBytes) { |
| 127 |
rowBytes = (size_t)mrb; |
| 128 |
} else if (!info.validRowBytes(rowBytes)) { |
| 129 |
return reset_return_false(this); |
| 130 |
} |
| 131 |
|
| 132 |
fPixelRef = nullptr; // Free pixels. |
| 133 |
fPixmap.reset(info.makeAlphaType(newAT), nullptr, SkToU32(rowBytes)); |
| 134 |
SkDEBUGCODE(this->validate();) |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) { |
| 139 |
if (!SkColorTypeValidateAlphaType(this->colorType(), newAlphaType, &newAlphaType)) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
if (this->alphaType() != newAlphaType) { |
| 143 |
auto newInfo = fPixmap.info().makeAlphaType(newAlphaType); |
| 144 |
fPixmap.reset(std::move(newInfo), fPixmap.addr(), fPixmap.rowBytes()); |
| 145 |
} |
| 146 |
SkDEBUGCODE(this->validate();) |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
void SkBitmap::setColorSpace(sk_sp<SkColorSpace> newColorSpace) { |
| 151 |
if (this->colorSpace() != newColorSpace.get()) { |
| 152 |
SkImageInfo newInfo = fPixmap.info().makeColorSpace(std::move(newColorSpace)); |
| 153 |
fPixmap.reset(std::move(newInfo), fPixmap.addr(), fPixmap.rowBytes()); |
| 154 |
} |
| 155 |
SkDEBUGCODE(this->validate();) |
| 156 |
} |
| 157 |
|
| 158 |
SkIPoint SkBitmap::pixelRefOrigin() const { |
| 159 |
const char* addr = (const char*)fPixmap.addr(); |
| 160 |
const char* pix = (const char*)(fPixelRef ? fPixelRef->pixels() : nullptr); |
| 161 |
size_t rb = this->rowBytes(); |
| 162 |
if (!pix || 0 == rb) { |
| 163 |
return {0, 0}; |
| 164 |
} |
| 165 |
SkASSERT(this->bytesPerPixel() > 0); |
| 166 |
SkASSERT(this->bytesPerPixel() == (1 << this->shiftPerPixel())); |
| 167 |
SkASSERT(addr >= pix); |
| 168 |
size_t off = addr - pix; |
| 169 |
return {SkToS32((off % rb) >> this->shiftPerPixel()), SkToS32(off / rb)}; |
| 170 |
} |
| 171 |
|
| 172 |
void SkBitmap::setPixelRef(sk_sp<SkPixelRef> pr, int dx, int dy) { |
| 173 |
#ifdef SK_DEBUG |
| 174 |
if (pr) { |
| 175 |
if (kUnknown_SkColorType != this->colorType()) { |
| 176 |
SkASSERT(dx >= 0 && this->width() + dx <= pr->width()); |
| 177 |
SkASSERT(dy >= 0 && this->height() + dy <= pr->height()); |
| 178 |
} |
| 179 |
} |
| 180 |
#endif |
| 181 |
fPixelRef = kUnknown_SkColorType != this->colorType() ? std::move(pr) : nullptr; |
| 182 |
void* p = nullptr; |
| 183 |
size_t rowBytes = this->rowBytes(); |
| 184 |
// ignore dx,dy if there is no pixelref |
| 185 |
if (fPixelRef) { |
| 186 |
rowBytes = fPixelRef->rowBytes(); |
| 187 |
// TODO(reed): Enforce that PixelRefs must have non-null pixels. |
| 188 |
p = fPixelRef->pixels(); |
| 189 |
if (p) { |
| 190 |
p = (char*)p + dy * rowBytes + dx * this->bytesPerPixel(); |
| 191 |
} |
| 192 |
} |
| 193 |
fPixmap.reset(fPixmap.info(), p, rowBytes); |
| 194 |
SkDEBUGCODE(this->validate();) |
| 195 |
} |
| 196 |
|
| 197 |
void SkBitmap::setPixels(void* p) { |
| 198 |
if (kUnknown_SkColorType == this->colorType()) { |
| 199 |
p = nullptr; |
| 200 |
} |
| 201 |
size_t rb = this->rowBytes(); |
| 202 |
fPixmap.reset(fPixmap.info(), p, rb); |
| 203 |
fPixelRef = p ? sk_make_sp<SkPixelRef>(this->width(), this->height(), p, rb) : nullptr; |
| 204 |
SkDEBUGCODE(this->validate();) |
| 205 |
} |
| 206 |
|
| 207 |
bool SkBitmap::tryAllocPixels(Allocator* allocator) { |
| 208 |
HeapAllocator stdalloc; |
| 209 |
|
| 210 |
if (nullptr == allocator) { |
| 211 |
allocator = &stdalloc; |
| 212 |
} |
| 213 |
return allocator->allocPixelRef(this); |
| 214 |
} |
| 215 |
|
| 216 |
bool SkBitmap::tryAllocN32Pixels(int width, int height, bool isOpaque) { |
| 217 |
SkImageInfo info = SkImageInfo::MakeN32(width, height, |
| 218 |
isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 219 |
return this->tryAllocPixels(info); |
| 220 |
} |
| 221 |
|
| 222 |
void SkBitmap::allocN32Pixels(int width, int height, bool isOpaque) { |
| 223 |
SkImageInfo info = SkImageInfo::MakeN32(width, height, |
| 224 |
isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 225 |
this->allocPixels(info); |
| 226 |
} |
| 227 |
|
| 228 |
void SkBitmap::allocPixels() { |
| 229 |
this->allocPixels((Allocator*)nullptr); |
| 230 |
} |
| 231 |
|
| 232 |
void SkBitmap::allocPixels(Allocator* allocator) { |
| 233 |
if (!this->tryAllocPixels(allocator)) { |
| 234 |
const SkImageInfo& info = this->info(); |
| 235 |
SK_ABORT("SkBitmap::tryAllocPixels failed " |
| 236 |
"ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu", |
| 237 |
info.colorType(), info.alphaType(), info.width(), info.height(), this->rowBytes()); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void SkBitmap::allocPixelsFlags(const SkImageInfo& info, uint32_t flags) { |
| 242 |
SkASSERTF_RELEASE(this->tryAllocPixelsFlags(info, flags), |
| 243 |
"ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu flags: 0x%x", |
| 244 |
info.colorType(), info.alphaType(), info.width(), info.height(), |
| 245 |
this->rowBytes(), flags); |
| 246 |
} |
| 247 |
|
| 248 |
void SkBitmap::allocPixels(const SkImageInfo& info, size_t rowBytes) { |
| 249 |
SkASSERTF_RELEASE(this->tryAllocPixels(info, rowBytes), |
| 250 |
"ColorType:%d AlphaType:%d [w:%d h:%d] rb:%zu", |
| 251 |
info.colorType(), info.alphaType(), info.width(), info.height(), |
| 252 |
this->rowBytes()); |
| 253 |
} |
| 254 |
|
| 255 |
void SkBitmap::allocPixels(const SkImageInfo& info) { |
| 256 |
this->allocPixels(info, info.minRowBytes()); |
| 257 |
} |
| 258 |
|
| 259 |
/////////////////////////////////////////////////////////////////////////////// |
| 260 |
|
| 261 |
bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { |
| 262 |
if (!this->setInfo(requestedInfo, rowBytes)) { |
| 263 |
return reset_return_false(this); |
| 264 |
} |
| 265 |
|
| 266 |
// setInfo may have corrected info (e.g. 565 is always opaque). |
| 267 |
const SkImageInfo& correctedInfo = this->info(); |
| 268 |
if (kUnknown_SkColorType == correctedInfo.colorType()) { |
| 269 |
return true; |
| 270 |
} |
| 271 |
// setInfo may have computed a valid rowbytes if 0 were passed in |
| 272 |
rowBytes = this->rowBytes(); |
| 273 |
|
| 274 |
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo, rowBytes); |
| 275 |
if (!pr) { |
| 276 |
return reset_return_false(this); |
| 277 |
} |
| 278 |
this->setPixelRef(std::move(pr), 0, 0); |
| 279 |
if (nullptr == this->getPixels()) { |
| 280 |
return reset_return_false(this); |
| 281 |
} |
| 282 |
SkDEBUGCODE(this->validate();) |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
bool SkBitmap::tryAllocPixelsFlags(const SkImageInfo& requestedInfo, uint32_t allocFlags) { |
| 287 |
if (!this->setInfo(requestedInfo)) { |
| 288 |
return reset_return_false(this); |
| 289 |
} |
| 290 |
|
| 291 |
// setInfo may have corrected info (e.g. 565 is always opaque). |
| 292 |
const SkImageInfo& correctedInfo = this->info(); |
| 293 |
|
| 294 |
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(correctedInfo, |
| 295 |
correctedInfo.minRowBytes()); |
| 296 |
if (!pr) { |
| 297 |
return reset_return_false(this); |
| 298 |
} |
| 299 |
this->setPixelRef(std::move(pr), 0, 0); |
| 300 |
if (nullptr == this->getPixels()) { |
| 301 |
return reset_return_false(this); |
| 302 |
} |
| 303 |
SkDEBUGCODE(this->validate();) |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
static void invoke_release_proc(void (*proc)(void* pixels, void* ctx), void* pixels, void* ctx) { |
| 308 |
if (proc) { |
| 309 |
proc(pixels, ctx); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
bool SkBitmap::installPixels(const SkImageInfo& requestedInfo, void* pixels, size_t rb, |
| 314 |
void (*releaseProc)(void* addr, void* context), void* context) { |
| 315 |
if (!this->setInfo(requestedInfo, rb)) { |
| 316 |
invoke_release_proc(releaseProc, pixels, context); |
| 317 |
this->reset(); |
| 318 |
return false; |
| 319 |
} |
| 320 |
if (nullptr == pixels) { |
| 321 |
invoke_release_proc(releaseProc, pixels, context); |
| 322 |
return true; // we behaved as if they called setInfo() |
| 323 |
} |
| 324 |
|
| 325 |
// setInfo may have corrected info (e.g. 565 is always opaque). |
| 326 |
const SkImageInfo& correctedInfo = this->info(); |
| 327 |
this->setPixelRef( |
| 328 |
SkMakePixelRefWithProc(correctedInfo.width(), correctedInfo.height(), |
| 329 |
rb, pixels, releaseProc, context), 0, 0); |
| 330 |
SkDEBUGCODE(this->validate();) |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
bool SkBitmap::installPixels(const SkPixmap& pixmap) { |
| 335 |
return this->installPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes(), |
| 336 |
nullptr, nullptr); |
| 337 |
} |
| 338 |
|
| 339 |
/////////////////////////////////////////////////////////////////////////////// |
| 340 |
|
| 341 |
uint32_t SkBitmap::getGenerationID() const { |
| 342 |
return fPixelRef ? fPixelRef->getGenerationID() : 0; |
| 343 |
} |
| 344 |
|
| 345 |
void SkBitmap::notifyPixelsChanged() const { |
| 346 |
SkASSERT(!this->isImmutable()); |
| 347 |
if (fPixelRef) { |
| 348 |
fPixelRef->notifyPixelsChanged(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/////////////////////////////////////////////////////////////////////////////// |
| 353 |
|
| 354 |
/** We explicitly use the same allocator for our pixels that SkMask does, |
| 355 |
so that we can freely assign memory allocated by one class to the other. |
| 356 |
*/ |
| 357 |
bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst) { |
| 358 |
const SkImageInfo& info = dst->info(); |
| 359 |
if (kUnknown_SkColorType == info.colorType()) { |
| 360 |
// SkDebugf("unsupported config for info %d\n", dst->config()); |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, dst->rowBytes()); |
| 365 |
if (!pr) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
dst->setPixelRef(std::move(pr), 0, 0); |
| 370 |
SkDEBUGCODE(dst->validate();) |
| 371 |
return true; |
| 372 |
} |
| 373 |
|
| 374 |
/////////////////////////////////////////////////////////////////////////////// |
| 375 |
|
| 376 |
bool SkBitmap::isImmutable() const { |
| 377 |
return fPixelRef ? fPixelRef->isImmutable() : false; |
| 378 |
} |
| 379 |
|
| 380 |
void SkBitmap::setImmutable() { |
| 381 |
if (fPixelRef) { |
| 382 |
fPixelRef->setImmutable(); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void* SkBitmap::getAddr(int x, int y) const { |
| 387 |
SkASSERT((unsigned)x < (unsigned)this->width()); |
| 388 |
SkASSERT((unsigned)y < (unsigned)this->height()); |
| 389 |
|
| 390 |
char* base = (char*)this->getPixels(); |
| 391 |
if (base) { |
| 392 |
base += (y * this->rowBytes()) + (x << this->shiftPerPixel()); |
| 393 |
} |
| 394 |
return base; |
| 395 |
} |
| 396 |
|
| 397 |
/////////////////////////////////////////////////////////////////////////////// |
| 398 |
/////////////////////////////////////////////////////////////////////////////// |
| 399 |
|
| 400 |
void SkBitmap::erase(SkColor4f c, const SkIRect& area) const { |
| 401 |
SkDEBUGCODE(this->validate();) |
| 402 |
|
| 403 |
if (kUnknown_SkColorType == this->colorType()) { |
| 404 |
// TODO: can we ASSERT that we never get here? |
| 405 |
return; // can't erase. Should we bzero so the memory is not uninitialized? |
| 406 |
} |
| 407 |
|
| 408 |
SkPixmap result; |
| 409 |
if (!this->peekPixels(&result)) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
if (result.erase(c, &area)) { |
| 414 |
this->notifyPixelsChanged(); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
void SkBitmap::erase(SkColor c, const SkIRect& area) const { |
| 419 |
this->erase(SkColor4f::FromColor(c), area); |
| 420 |
} |
| 421 |
|
| 422 |
void SkBitmap::eraseColor(SkColor4f c) const { |
| 423 |
this->erase(c, SkIRect::MakeWH(this->width(), this->height())); |
| 424 |
} |
| 425 |
|
| 426 |
void SkBitmap::eraseColor(SkColor c) const { |
| 427 |
this->erase(SkColor4f::FromColor(c), SkIRect::MakeWH(this->width(), this->height())); |
| 428 |
} |
| 429 |
|
| 430 |
////////////////////////////////////////////////////////////////////////////////////// |
| 431 |
////////////////////////////////////////////////////////////////////////////////////// |
| 432 |
|
| 433 |
bool SkBitmap::extractSubset(SkBitmap* result, const SkIRect& subset) const { |
| 434 |
SkDEBUGCODE(this->validate();) |
| 435 |
|
| 436 |
if (nullptr == result || !fPixelRef) { |
| 437 |
return false; // no src pixels |
| 438 |
} |
| 439 |
|
| 440 |
SkIRect srcRect, r; |
| 441 |
srcRect.setWH(this->width(), this->height()); |
| 442 |
if (!r.intersect(srcRect, subset)) { |
| 443 |
return false; // r is empty (i.e. no intersection) |
| 444 |
} |
| 445 |
|
| 446 |
// If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 447 |
// exited above. |
| 448 |
SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 449 |
SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 450 |
|
| 451 |
SkBitmap dst; |
| 452 |
dst.setInfo(this->info().makeDimensions(r.size()), this->rowBytes()); |
| 453 |
|
| 454 |
if (fPixelRef) { |
| 455 |
SkIPoint origin = this->pixelRefOrigin(); |
| 456 |
// share the pixelref with a custom offset |
| 457 |
dst.setPixelRef(fPixelRef, origin.x() + r.fLeft, origin.y() + r.fTop); |
| 458 |
} |
| 459 |
SkDEBUGCODE(dst.validate();) |
| 460 |
|
| 461 |
// we know we're good, so commit to result |
| 462 |
result->swap(dst); |
| 463 |
return true; |
| 464 |
} |
| 465 |
|
| 466 |
/////////////////////////////////////////////////////////////////////////////// |
| 467 |
|
| 468 |
bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, |
| 469 |
int x, int y) const { |
| 470 |
SkPixmap src; |
| 471 |
if (!this->peekPixels(&src)) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
return src.readPixels(requestedDstInfo, dstPixels, dstRB, x, y); |
| 475 |
} |
| 476 |
|
| 477 |
bool SkBitmap::readPixels(const SkPixmap& dst, int srcX, int srcY) const { |
| 478 |
return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY); |
| 479 |
} |
| 480 |
|
| 481 |
bool SkBitmap::writePixels(const SkPixmap& src, int dstX, int dstY) { |
| 482 |
if (!SkImageInfoValidConversion(this->info(), src.info())) { |
| 483 |
return false; |
| 484 |
} |
| 485 |
|
| 486 |
SkWritePixelsRec rec(src.info(), src.addr(), src.rowBytes(), dstX, dstY); |
| 487 |
if (!rec.trim(this->width(), this->height())) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
|
| 491 |
void* dstPixels = this->getAddr(rec.fX, rec.fY); |
| 492 |
const SkImageInfo dstInfo = this->info().makeDimensions(rec.fInfo.dimensions()); |
| 493 |
if (!SkConvertPixels(dstInfo, dstPixels, this->rowBytes(), |
| 494 |
rec.fInfo, rec.fPixels, rec.fRowBytes)) { |
| 495 |
return false; |
| 496 |
} |
| 497 |
this->notifyPixelsChanged(); |
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
/////////////////////////////////////////////////////////////////////////////// |
| 502 |
|
| 503 |
static bool GetBitmapAlpha(const SkBitmap& src, uint8_t* SK_RESTRICT alpha, int alphaRowBytes) { |
| 504 |
SkASSERT(alpha != nullptr); |
| 505 |
SkASSERT(alphaRowBytes >= src.width()); |
| 506 |
|
| 507 |
SkPixmap pmap; |
| 508 |
if (!src.peekPixels(&pmap)) { |
| 509 |
for (int y = 0; y < src.height(); ++y) { |
| 510 |
memset(alpha, 0, src.width()); |
| 511 |
alpha += alphaRowBytes; |
| 512 |
} |
| 513 |
return false; |
| 514 |
} |
| 515 |
return SkConvertPixels(SkImageInfo::MakeA8(pmap.width(), pmap.height()), alpha, alphaRowBytes, |
| 516 |
pmap.info(), pmap.addr(), pmap.rowBytes()); |
| 517 |
} |
| 518 |
|
| 519 |
bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, |
| 520 |
Allocator *allocator, SkIPoint* offset) const { |
| 521 |
SkDEBUGCODE(this->validate();) |
| 522 |
|
| 523 |
SkBitmap tmpBitmap; |
| 524 |
SkMatrix identity; |
| 525 |
SkMaskBuilder srcM, dstM; |
| 526 |
|
| 527 |
if (this->width() == 0 || this->height() == 0) { |
| 528 |
return false; |
| 529 |
} |
| 530 |
srcM.bounds().setWH(this->width(), this->height()); |
| 531 |
srcM.rowBytes() = SkAlign4(this->width()); |
| 532 |
srcM.format() = SkMask::kA8_Format; |
| 533 |
|
| 534 |
SkMaskFilter* filter = paint ? paint->getMaskFilter() : nullptr; |
| 535 |
|
| 536 |
// compute our (larger?) dst bounds if we have a filter |
| 537 |
if (filter) { |
| 538 |
identity.reset(); |
| 539 |
if (!as_MFB(filter)->filterMask(&dstM, srcM, identity, nullptr)) { |
| 540 |
goto NO_FILTER_CASE; |
| 541 |
} |
| 542 |
dstM.rowBytes() = SkAlign4(dstM.fBounds.width()); |
| 543 |
} else { |
| 544 |
NO_FILTER_CASE: |
| 545 |
tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes); |
| 546 |
if (!tmpBitmap.tryAllocPixels(allocator)) { |
| 547 |
// Allocation of pixels for alpha bitmap failed. |
| 548 |
SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 549 |
tmpBitmap.width(), tmpBitmap.height()); |
| 550 |
return false; |
| 551 |
} |
| 552 |
GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
| 553 |
if (offset) { |
| 554 |
offset->set(0, 0); |
| 555 |
} |
| 556 |
tmpBitmap.swap(*dst); |
| 557 |
return true; |
| 558 |
} |
| 559 |
srcM.image() = SkMaskBuilder::AllocImage(srcM.computeImageSize()); |
| 560 |
SkAutoMaskFreeImage srcCleanup(srcM.image()); |
| 561 |
|
| 562 |
GetBitmapAlpha(*this, srcM.image(), srcM.fRowBytes); |
| 563 |
if (!as_MFB(filter)->filterMask(&dstM, srcM, identity, nullptr)) { |
| 564 |
goto NO_FILTER_CASE; |
| 565 |
} |
| 566 |
SkAutoMaskFreeImage dstCleanup(dstM.image()); |
| 567 |
|
| 568 |
tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 569 |
dstM.fRowBytes); |
| 570 |
if (!tmpBitmap.tryAllocPixels(allocator)) { |
| 571 |
// Allocation of pixels for alpha bitmap failed. |
| 572 |
SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
| 573 |
tmpBitmap.width(), tmpBitmap.height()); |
| 574 |
return false; |
| 575 |
} |
| 576 |
memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
| 577 |
if (offset) { |
| 578 |
offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
| 579 |
} |
| 580 |
SkDEBUGCODE(tmpBitmap.validate();) |
| 581 |
|
| 582 |
tmpBitmap.swap(*dst); |
| 583 |
return true; |
| 584 |
} |
| 585 |
|
| 586 |
/////////////////////////////////////////////////////////////////////////////// |
| 587 |
|
| 588 |
#ifdef SK_DEBUG |
| 589 |
void SkBitmap::validate() const { |
| 590 |
this->info().validate(); |
| 591 |
|
| 592 |
SkASSERT(this->info().validRowBytes(this->rowBytes())); |
| 593 |
|
| 594 |
if (fPixelRef && fPixelRef->pixels()) { |
| 595 |
SkASSERT(this->getPixels()); |
| 596 |
} else { |
| 597 |
SkASSERT(!this->getPixels()); |
| 598 |
} |
| 599 |
|
| 600 |
if (this->getPixels()) { |
| 601 |
SkASSERT(fPixelRef); |
| 602 |
SkASSERT(fPixelRef->rowBytes() == this->rowBytes()); |
| 603 |
SkIPoint origin = this->pixelRefOrigin(); |
| 604 |
SkASSERT(origin.fX >= 0); |
| 605 |
SkASSERT(origin.fY >= 0); |
| 606 |
SkASSERT(fPixelRef->width() >= (int)this->width() + origin.fX); |
| 607 |
SkASSERT(fPixelRef->height() >= (int)this->height() + origin.fY); |
| 608 |
SkASSERT(fPixelRef->rowBytes() >= this->info().minRowBytes()); |
| 609 |
} |
| 610 |
} |
| 611 |
#endif |
| 612 |
|
| 613 |
/////////////////////////////////////////////////////////////////////////////// |
| 614 |
|
| 615 |
bool SkBitmap::peekPixels(SkPixmap* pmap) const { |
| 616 |
if (this->getPixels()) { |
| 617 |
if (pmap) { |
| 618 |
*pmap = fPixmap; |
| 619 |
} |
| 620 |
return true; |
| 621 |
} |
| 622 |
return false; |
| 623 |
} |
| 624 |
|
| 625 |
sk_sp<SkImage> SkBitmap::asImage() const { return SkImages::RasterFromBitmap(*this); } |
| 626 |
|
| 627 |
sk_sp<SkShader> SkBitmap::makeShader(const SkSamplingOptions& sampling, |
| 628 |
const SkMatrix& lm) const { |
| 629 |
return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, |
| 630 |
sampling, &lm); |
| 631 |
} |
| 632 |
|
| 633 |
sk_sp<SkShader> SkBitmap::makeShader(const SkSamplingOptions& sampling, |
| 634 |
const SkMatrix* lm) const { |
| 635 |
return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, |
| 636 |
sampling, lm); |
| 637 |
} |
| 638 |
|
| 639 |
sk_sp<SkShader> SkBitmap::makeShader(SkTileMode tmx, SkTileMode tmy, |
| 640 |
const SkSamplingOptions& sampling, |
| 641 |
const SkMatrix& lm) const { |
| 642 |
if (!lm.invert()) { |
| 643 |
return nullptr; |
| 644 |
} |
| 645 |
return SkImageShader::Make(SkImage_Raster::MakeFromBitmap(*this, SkCopyPixelsMode::kIfMutable), |
| 646 |
tmx, |
| 647 |
tmy, |
| 648 |
sampling, |
| 649 |
&lm); |
| 650 |
} |
| 651 |
|
| 652 |
sk_sp<SkShader> SkBitmap::makeShader(SkTileMode tmx, SkTileMode tmy, |
| 653 |
const SkSamplingOptions& sampling, |
| 654 |
const SkMatrix* lm) const { |
| 655 |
if (lm && !lm->invert()) { |
| 656 |
return nullptr; |
| 657 |
} |
| 658 |
return SkImageShader::Make(SkImage_Raster::MakeFromBitmap(*this, SkCopyPixelsMode::kIfMutable), |
| 659 |
tmx, |
| 660 |
tmy, |
| 661 |
sampling, |
| 662 |
lm); |
| 663 |
} |