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 "SkColorPriv.h" |
8 #include "SkConfig8888.h" | 9 #include "SkConfig8888.h" |
9 #include "SkMask.h" | 10 #include "SkMask.h" |
10 #include "SkPixmap.h" | 11 #include "SkPixmap.h" |
| 12 #include "SkUtils.h" |
11 | 13 |
12 void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void*
ctx) { | 14 void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void*
ctx) { |
13 SkASSERT(pm.addr() != NULL); | 15 SkASSERT(pm.addr() != NULL); |
14 | 16 |
15 this->unlock(); | 17 this->unlock(); |
16 fPixmap = pm; | 18 fPixmap = pm; |
17 fUnlockProc = unlock; | 19 fUnlockProc = unlock; |
18 fUnlockContext = ctx; | 20 fUnlockContext = ctx; |
19 fIsLocked = true; | 21 fIsLocked = true; |
20 } | 22 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 100 } |
99 // here x,y are either 0 or negative | 101 // here x,y are either 0 or negative |
100 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); | 102 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); |
101 | 103 |
102 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.hei
ght()); | 104 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.hei
ght()); |
103 const void* srcPixels = this->addr(srcR.x(), srcR.y()); | 105 const void* srcPixels = this->addr(srcR.x(), srcR.y()); |
104 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, | 106 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, |
105 srcInfo, srcPixels, this->rowBytes(), this->c
table()); | 107 srcInfo, srcPixels, this->rowBytes(), this->c
table()); |
106 } | 108 } |
107 | 109 |
| 110 static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b
) { |
| 111 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) | |
| 112 (SkR32To4444(r) << SK_R4444_SHIFT) | |
| 113 (SkG32To4444(g) << SK_G4444_SHIFT) | |
| 114 (SkB32To4444(b) << SK_B4444_SHIFT); |
| 115 return SkToU16(pixel); |
| 116 } |
| 117 |
| 118 bool SkPixmap::erase(SkColor color, const SkIRect& inArea) const { |
| 119 if (NULL == fPixels) { |
| 120 return false; |
| 121 } |
| 122 SkIRect area; |
| 123 if (!area.intersect(this->bounds(), inArea)) { |
| 124 return false; |
| 125 } |
| 126 |
| 127 U8CPU a = SkColorGetA(color); |
| 128 U8CPU r = SkColorGetR(color); |
| 129 U8CPU g = SkColorGetG(color); |
| 130 U8CPU b = SkColorGetB(color); |
| 131 |
| 132 int height = area.height(); |
| 133 const int width = area.width(); |
| 134 const int rowBytes = this->rowBytes(); |
| 135 |
| 136 switch (this->colorType()) { |
| 137 case kGray_8_SkColorType: { |
| 138 if (255 != a) { |
| 139 r = SkMulDiv255Round(r, a); |
| 140 g = SkMulDiv255Round(g, a); |
| 141 b = SkMulDiv255Round(b, a); |
| 142 } |
| 143 int gray = SkComputeLuminance(r, g, b); |
| 144 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop); |
| 145 while (--height >= 0) { |
| 146 memset(p, gray, width); |
| 147 p += rowBytes; |
| 148 } |
| 149 break; |
| 150 } |
| 151 case kAlpha_8_SkColorType: { |
| 152 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop); |
| 153 while (--height >= 0) { |
| 154 memset(p, a, width); |
| 155 p += rowBytes; |
| 156 } |
| 157 break; |
| 158 } |
| 159 case kARGB_4444_SkColorType: |
| 160 case kRGB_565_SkColorType: { |
| 161 uint16_t* p = this->writable_addr16(area.fLeft, area.fTop); |
| 162 uint16_t v; |
| 163 |
| 164 // make rgb premultiplied |
| 165 if (255 != a) { |
| 166 r = SkAlphaMul(r, a); |
| 167 g = SkAlphaMul(g, a); |
| 168 b = SkAlphaMul(b, a); |
| 169 } |
| 170 |
| 171 if (kARGB_4444_SkColorType == this->colorType()) { |
| 172 v = pack_8888_to_4444(a, r, g, b); |
| 173 } else { |
| 174 v = SkPackRGB16(r >> (8 - SK_R16_BITS), |
| 175 g >> (8 - SK_G16_BITS), |
| 176 b >> (8 - SK_B16_BITS)); |
| 177 } |
| 178 while (--height >= 0) { |
| 179 sk_memset16(p, v, width); |
| 180 p = (uint16_t*)((char*)p + rowBytes); |
| 181 } |
| 182 break; |
| 183 } |
| 184 case kBGRA_8888_SkColorType: |
| 185 case kRGBA_8888_SkColorType: { |
| 186 uint32_t* p = this->writable_addr32(area.fLeft, area.fTop); |
| 187 |
| 188 if (255 != a && kPremul_SkAlphaType == this->alphaType()) { |
| 189 r = SkAlphaMul(r, a); |
| 190 g = SkAlphaMul(g, a); |
| 191 b = SkAlphaMul(b, a); |
| 192 } |
| 193 uint32_t v = kRGBA_8888_SkColorType == this->colorType() ? |
| 194 SkPackARGB_as_RGBA(a, r, g, b) : SkPackARGB_as_BGRA(a, r, g, b); |
| 195 |
| 196 while (--height >= 0) { |
| 197 sk_memset32(p, v, width); |
| 198 p = (uint32_t*)((char*)p + rowBytes); |
| 199 } |
| 200 break; |
| 201 } |
| 202 default: |
| 203 return false; // no change, so don't call notifyPixelsChanged() |
| 204 } |
| 205 return true; |
| 206 } |
| 207 |
108 ////////////////////////////////////////////////////////////////////////////////
////////////////// | 208 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
109 | 209 |
110 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(NULL) {} | 210 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(NULL) {} |
111 | 211 |
112 SkAutoPixmapStorage::~SkAutoPixmapStorage() { | 212 SkAutoPixmapStorage::~SkAutoPixmapStorage() { |
113 sk_free(fStorage); | 213 this->freeStorage(); |
114 } | 214 } |
115 | 215 |
116 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { | 216 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { |
117 if (fStorage) { | 217 this->freeStorage(); |
118 sk_free(fStorage); | |
119 fStorage = NULL; | |
120 } | |
121 | 218 |
122 size_t rb = info.minRowBytes(); | 219 size_t rb = info.minRowBytes(); |
123 size_t size = info.getSafeSize(rb); | 220 size_t size = info.getSafeSize(rb); |
124 if (0 == size) { | 221 if (0 == size) { |
125 return false; | 222 return false; |
126 } | 223 } |
127 fStorage = sk_malloc_flags(size, 0); | 224 void* pixels = sk_malloc_flags(size, 0); |
128 if (NULL == fStorage) { | 225 if (NULL == pixels) { |
129 return false; | 226 return false; |
130 } | 227 } |
131 this->reset(info, fStorage, rb); | 228 this->reset(info, pixels, rb); |
| 229 fStorage = pixels; |
132 return true; | 230 return true; |
133 } | 231 } |
134 | 232 |
135 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { | 233 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { |
136 if (!this->tryAlloc(info)) { | 234 if (!this->tryAlloc(info)) { |
137 sk_throw(); | 235 sk_throw(); |
138 } | 236 } |
139 } | 237 } |
OLD | NEW |