Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: include/core/SkBitmap.h

Issue 510423005: make allocPixels throw on failure (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/skia_for_chromium_defines.gypi ('k') | samplecode/SampleApp.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
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 #ifndef SkBitmap_DEFINED 8 #ifndef SkBitmap_DEFINED
9 #define SkBitmap_DEFINED 9 #define SkBitmap_DEFINED
10 10
11 #include "SkColor.h" 11 #include "SkColor.h"
12 #include "SkColorTable.h" 12 #include "SkColorTable.h"
13 #include "SkImageInfo.h" 13 #include "SkImageInfo.h"
14 #include "SkPoint.h" 14 #include "SkPoint.h"
15 #include "SkRefCnt.h" 15 #include "SkRefCnt.h"
16 16
17 #ifdef SK_SUPPORT_LEGACY_ALLOCPIXELS_BOOL
18 #define SK_ALLOCPIXELS_RETURN_TYPE bool
19 #define SK_ALLOCPIXELS_RETURN_TRUE return true
20 #define SK_ALLOCPIXELS_RETURN_FAIL return false
21 #else
22 #define SK_ALLOCPIXELS_RETURN_TYPE void
23 #define SK_ALLOCPIXELS_RETURN_TRUE return
24 #define SK_ALLOCPIXELS_RETURN_FAIL sk_throw()
25 #endif
26
17 struct SkMask; 27 struct SkMask;
18 struct SkIRect; 28 struct SkIRect;
19 struct SkRect; 29 struct SkRect;
20 class SkPaint; 30 class SkPaint;
21 class SkPixelRef; 31 class SkPixelRef;
22 class SkPixelRefFactory; 32 class SkPixelRefFactory;
23 class SkRegion; 33 class SkRegion;
24 class SkString; 34 class SkString;
25 class GrTexture; 35 class GrTexture;
26 36
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 void getBounds(SkIRect* bounds) const; 221 void getBounds(SkIRect* bounds) const;
212 222
213 bool setInfo(const SkImageInfo&, size_t rowBytes = 0); 223 bool setInfo(const SkImageInfo&, size_t rowBytes = 0);
214 224
215 /** 225 /**
216 * Allocate the bitmap's pixels to match the requested image info. If the F actory 226 * Allocate the bitmap's pixels to match the requested image info. If the F actory
217 * is non-null, call it to allcoate the pixelref. If the ImageInfo requires 227 * is non-null, call it to allcoate the pixelref. If the ImageInfo requires
218 * a colortable, then ColorTable must be non-null, and will be ref'd. 228 * a colortable, then ColorTable must be non-null, and will be ref'd.
219 * On failure, the bitmap will be set to empty and return false. 229 * On failure, the bitmap will be set to empty and return false.
220 */ 230 */
221 bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*); 231 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFact ory*, SkColorTable*);
232
233 SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info, SkPixelRefFa ctory* factory,
234 SkColorTable* ctable) {
235 if (!this->tryAllocPixels(info, factory, ctable)) {
236 SK_ALLOCPIXELS_RETURN_FAIL;
237 }
238 SK_ALLOCPIXELS_RETURN_TRUE;
239 }
222 240
223 /** 241 /**
224 * Allocate the bitmap's pixels to match the requested image info and 242 * Allocate the bitmap's pixels to match the requested image info and
225 * rowBytes. If the request cannot be met (e.g. the info is invalid or 243 * rowBytes. If the request cannot be met (e.g. the info is invalid or
226 * the requested rowBytes are not compatible with the info 244 * the requested rowBytes are not compatible with the info
227 * (e.g. rowBytes < info.minRowBytes() or rowBytes is not aligned with 245 * (e.g. rowBytes < info.minRowBytes() or rowBytes is not aligned with
228 * the pixel size specified by info.colorType()) then false is returned 246 * the pixel size specified by info.colorType()) then false is returned
229 * and the bitmap is set to empty. 247 * and the bitmap is set to empty.
230 */ 248 */
231 bool allocPixels(const SkImageInfo& info, size_t rowBytes); 249 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t ro wBytes);
232 250
233 /** 251 SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info, size_t rowBy tes) {
234 * Allocate a pixelref to match the specified image info, using the default 252 if (!this->tryAllocPixels(info, rowBytes)) {
235 * allocator. 253 SK_ALLOCPIXELS_RETURN_FAIL;
236 * On success, the bitmap's pixels will be "locked", and return true. 254 }
237 * On failure, the bitmap will be set to empty and return false. 255 SK_ALLOCPIXELS_RETURN_TRUE;
238 */ 256 }
239 bool allocPixels(const SkImageInfo& info) { 257
258 bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
259 return this->tryAllocPixels(info, info.minRowBytes());
260 }
261
262 SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info) {
240 return this->allocPixels(info, info.minRowBytes()); 263 return this->allocPixels(info, info.minRowBytes());
241 } 264 }
242 265
243 bool allocN32Pixels(int width, int height, bool isOpaque = false) { 266 bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isO paque = false) {
267 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
268 if (isOpaque) {
269 info.fAlphaType = kOpaque_SkAlphaType;
270 }
271 return this->tryAllocPixels(info);
272 }
273
274 SK_ALLOCPIXELS_RETURN_TYPE allocN32Pixels(int width, int height, bool isOpaq ue = false) {
244 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); 275 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
245 if (isOpaque) { 276 if (isOpaque) {
246 info.fAlphaType = kOpaque_SkAlphaType; 277 info.fAlphaType = kOpaque_SkAlphaType;
247 } 278 }
248 return this->allocPixels(info); 279 return this->allocPixels(info);
249 } 280 }
250 281
251 /** 282 /**
252 * Install a pixelref that wraps the specified pixels and rowBytes, and 283 * Install a pixelref that wraps the specified pixels and rowBytes, and
253 * optional ReleaseProc and context. When the pixels are no longer 284 * optional ReleaseProc and context. When the pixels are no longer
254 * referenced, if releaseProc is not null, it will be called with the 285 * referenced, if releaseProc is not null, it will be called with the
255 * pixels and context as parameters. 286 * pixels and context as parameters.
256 * On failure, the bitmap will be set to empty and return false. 287 * On failure, the bitmap will be set to empty and return false.
257 */ 288 */
258 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColo rTable*, 289 bool installPixels(const SkImageInfo&, void* pixels, size_t rowBytes, SkColo rTable*,
259 void (*releaseProc)(void* addr, void* context), void* con text); 290 void (*releaseProc)(void* addr, void* context), void* con text);
260 291
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 each time. 344 each time.
314 345
315 If the bitmap retains a reference to the colortable (assuming it is 346 If the bitmap retains a reference to the colortable (assuming it is
316 not null) it will take care of incrementing the reference count. 347 not null) it will take care of incrementing the reference count.
317 348
318 @param ctable ColorTable (or null) to use with the pixels that will 349 @param ctable ColorTable (or null) to use with the pixels that will
319 be allocated. Only used if colortype == kIndex_8_SkColor Type 350 be allocated. Only used if colortype == kIndex_8_SkColor Type
320 @return true if the allocation succeeds. If not the pixelref field of 351 @return true if the allocation succeeds. If not the pixelref field of
321 the bitmap will be unchanged. 352 the bitmap will be unchanged.
322 */ 353 */
323 bool allocPixels(SkColorTable* ctable = NULL) { 354 bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) {
355 return this->tryAllocPixels(NULL, ctable);
356 }
357
358 SK_ALLOCPIXELS_RETURN_TYPE allocPixels(SkColorTable* ctable = NULL) {
324 return this->allocPixels(NULL, ctable); 359 return this->allocPixels(NULL, ctable);
325 } 360 }
326 361
327 /** Use the specified Allocator to create the pixelref that manages the 362 /** Use the specified Allocator to create the pixelref that manages the
328 pixel memory. It will be sized based on the current ImageInfo. 363 pixel memory. It will be sized based on the current ImageInfo.
329 If this is called multiple times, a new pixelref object will be created 364 If this is called multiple times, a new pixelref object will be created
330 each time. 365 each time.
331 366
332 If the bitmap retains a reference to the colortable (assuming it is 367 If the bitmap retains a reference to the colortable (assuming it is
333 not null) it will take care of incrementing the reference count. 368 not null) it will take care of incrementing the reference count.
334 369
335 @param allocator The Allocator to use to create a pixelref that can 370 @param allocator The Allocator to use to create a pixelref that can
336 manage the pixel memory for the current ImageInfo. 371 manage the pixel memory for the current ImageInfo.
337 If allocator is NULL, the standard HeapAllocator will b e used. 372 If allocator is NULL, the standard HeapAllocator will b e used.
338 @param ctable ColorTable (or null) to use with the pixels that will 373 @param ctable ColorTable (or null) to use with the pixels that will
339 be allocated. Only used if colortype == kIndex_8_SkColor Type. 374 be allocated. Only used if colortype == kIndex_8_SkColor Type.
340 If it is non-null and the colortype is not indexed, it w ill 375 If it is non-null and the colortype is not indexed, it w ill
341 be ignored. 376 be ignored.
342 @return true if the allocation succeeds. If not the pixelref field of 377 @return true if the allocation succeeds. If not the pixelref field of
343 the bitmap will be unchanged. 378 the bitmap will be unchanged.
344 */ 379 */
345 bool allocPixels(Allocator* allocator, SkColorTable* ctable); 380 bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable * ctable);
381
382 SK_ALLOCPIXELS_RETURN_TYPE allocPixels(Allocator* allocator, SkColorTable* c table) {
383 if (!this->tryAllocPixels(allocator, ctable)) {
384 SK_ALLOCPIXELS_RETURN_FAIL;
385 }
386 SK_ALLOCPIXELS_RETURN_TRUE;
387 }
346 388
347 /** 389 /**
348 * Return the current pixelref object or NULL if there is none. This does 390 * Return the current pixelref object or NULL if there is none. This does
349 * not affect the refcount of the pixelref. 391 * not affect the refcount of the pixelref.
350 */ 392 */
351 SkPixelRef* pixelRef() const { return fPixelRef; } 393 SkPixelRef* pixelRef() const { return fPixelRef; }
352 394
353 /** 395 /**
354 * A bitmap can reference a subset of a pixelref's pixels. That means the 396 * A bitmap can reference a subset of a pixelref's pixels. That means the
355 * bitmap's width/height can be <= the dimensions of the pixelref. The 397 * bitmap's width/height can be <= the dimensions of the pixelref. The
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 878
837 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const { 879 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
838 SkASSERT(fPixels); 880 SkASSERT(fPixels);
839 SkASSERT(kIndex_8_SkColorType == this->colorType()); 881 SkASSERT(kIndex_8_SkColorType == this->colorType());
840 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th is->height()); 882 SkASSERT((unsigned)x < (unsigned)this->width() && (unsigned)y < (unsigned)th is->height());
841 SkASSERT(fColorTable); 883 SkASSERT(fColorTable);
842 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)]; 884 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
843 } 885 }
844 886
845 #endif 887 #endif
OLDNEW
« no previous file with comments | « gyp/skia_for_chromium_defines.gypi ('k') | samplecode/SampleApp.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698