OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkConfig8888.h" | 8 #include "SkConfig8888.h" |
| 9 #include "SkMask.h" |
9 #include "SkPixmap.h" | 10 #include "SkPixmap.h" |
10 | 11 |
11 void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void*
ctx) { | 12 void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void*
ctx) { |
12 SkASSERT(pm.addr() != NULL); | 13 SkASSERT(pm.addr() != NULL); |
13 | 14 |
14 this->unlock(); | 15 this->unlock(); |
15 fPixmap = pm; | 16 fPixmap = pm; |
16 fUnlockProc = unlock; | 17 fUnlockProc = unlock; |
17 fUnlockContext = ctx; | 18 fUnlockContext = ctx; |
18 fIsLocked = true; | 19 fIsLocked = true; |
(...skipping 11 matching lines...) Expand all Loading... |
30 void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes,
SkColorTable* ct) { | 31 void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes,
SkColorTable* ct) { |
31 if (addr) { | 32 if (addr) { |
32 SkASSERT(info.validRowBytes(rowBytes)); | 33 SkASSERT(info.validRowBytes(rowBytes)); |
33 } | 34 } |
34 fPixels = addr; | 35 fPixels = addr; |
35 fCTable = ct; | 36 fCTable = ct; |
36 fRowBytes = rowBytes; | 37 fRowBytes = rowBytes; |
37 fInfo = info; | 38 fInfo = info; |
38 } | 39 } |
39 | 40 |
| 41 bool SkPixmap::reset(const SkMask& src) { |
| 42 if (SkMask::kA8_Format == src.fFormat) { |
| 43 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height(
)), |
| 44 src.fImage, src.fRowBytes, NULL); |
| 45 return true; |
| 46 } |
| 47 this->reset(); |
| 48 return false; |
| 49 } |
| 50 |
| 51 bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const { |
| 52 SkIRect srcRect, r; |
| 53 srcRect.set(0, 0, this->width(), this->height()); |
| 54 if (!r.intersect(srcRect, subset)) { |
| 55 return false; // r is empty (i.e. no intersection) |
| 56 } |
| 57 |
| 58 // If the upper left of the rectangle was outside the bounds of this SkBitma
p, we should have |
| 59 // exited above. |
| 60 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width(
))); |
| 61 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height(
))); |
| 62 |
| 63 const void* pixels = NULL; |
| 64 if (fPixels) { |
| 65 const size_t bpp = fInfo.bytesPerPixel(); |
| 66 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp; |
| 67 } |
| 68 result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTabl
e); |
| 69 return true; |
| 70 } |
| 71 |
40 bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels,
size_t dstRB, | 72 bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels,
size_t dstRB, |
41 int x, int y) const { | 73 int x, int y) const { |
42 if (kUnknown_SkColorType == requestedDstInfo.colorType()) { | 74 if (kUnknown_SkColorType == requestedDstInfo.colorType()) { |
43 return false; | 75 return false; |
44 } | 76 } |
45 if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { | 77 if (NULL == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { |
46 return false; | 78 return false; |
47 } | 79 } |
48 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { | 80 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { |
49 return false; | 81 return false; |
(...skipping 15 matching lines...) Expand all Loading... |
65 y = 0; | 97 y = 0; |
66 } | 98 } |
67 // here x,y are either 0 or negative | 99 // here x,y are either 0 or negative |
68 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); | 100 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); |
69 | 101 |
70 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.hei
ght()); | 102 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.hei
ght()); |
71 const void* srcPixels = this->addr(srcR.x(), srcR.y()); | 103 const void* srcPixels = this->addr(srcR.x(), srcR.y()); |
72 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, | 104 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, |
73 srcInfo, srcPixels, this->rowBytes(), this->c
table()); | 105 srcInfo, srcPixels, this->rowBytes(), this->c
table()); |
74 } | 106 } |
| 107 |
| 108 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
| 109 |
| 110 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(NULL) {} |
| 111 |
| 112 SkAutoPixmapStorage::~SkAutoPixmapStorage() { |
| 113 sk_free(fStorage); |
| 114 } |
| 115 |
| 116 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { |
| 117 if (fStorage) { |
| 118 sk_free(fStorage); |
| 119 fStorage = NULL; |
| 120 } |
| 121 |
| 122 size_t rb = info.minRowBytes(); |
| 123 size_t size = info.getSafeSize(rb); |
| 124 if (0 == size) { |
| 125 return false; |
| 126 } |
| 127 fStorage = sk_malloc_flags(size, 0); |
| 128 if (NULL == fStorage) { |
| 129 return false; |
| 130 } |
| 131 this->reset(info, fStorage, rb); |
| 132 return true; |
| 133 } |
| 134 |
| 135 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { |
| 136 if (!this->tryAlloc(info)) { |
| 137 sk_throw(); |
| 138 } |
| 139 } |
OLD | NEW |