blob: 1f6f5a6b03236ef6c517e420ce9811705adfb72d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkBitmap_DEFINED
18#define SkBitmap_DEFINED
19
20#include "Sk64.h"
21#include "SkColor.h"
22#include "SkPoint.h"
23#include "SkRefCnt.h"
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025struct SkIRect;
26class SkColorTable;
27class SkPaint;
28class SkPixelRef;
29class SkRegion;
30class SkFlattenableReadBuffer;
31class SkFlattenableWriteBuffer;
32
reed@android.comce4e53a2010-09-09 16:01:26 +000033// This is an opaque class, not interpreted by skia
34class SkGpuTexture;
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036/** \class SkBitmap
37
38 The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
39 and height, and a format (config), and a pointer to the actual pixels.
40 Bitmaps can be drawn into a SkCanvas, but they are also used to specify the target
41 of a SkCanvas' drawing operations.
42*/
43class SkBitmap {
44public:
45 class Allocator;
46
47 enum Config {
48 kNo_Config, //!< bitmap has not been configured
49 kA1_Config, //!< 1-bit per pixel, (0 is transparent, 1 is opaque)
50 kA8_Config, //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
51 kIndex8_Config, //!< 8-bits per pixel, using SkColorTable to specify the colors
52 kRGB_565_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing)
53 kARGB_4444_Config, //!< 16-bits per pixel, (see SkColorPriv.h for packing)
54 kARGB_8888_Config, //!< 32-bits per pixel, (see SkColorPriv.h for packing)
55 kRLE_Index8_Config,
56
57 kConfigCount
58 };
59
60 /** Default construct creates a bitmap with zero width and height, and no pixels.
61 Its config is set to kNo_Config.
62 */
63 SkBitmap();
64 /** Constructor initializes the new bitmap by copying the src bitmap. All fields are copied,
65 but ownership of the pixels remains with the src bitmap.
66 */
67 SkBitmap(const SkBitmap& src);
68 /** Decrements our (shared) pixel ownership if needed.
69 */
70 ~SkBitmap();
71
72 /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
73 with the src bitmap.
74 */
75 SkBitmap& operator=(const SkBitmap& src);
76 /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
77 */
78 // This method is not exported to java.
79 void swap(SkBitmap& other);
weita@google.comf9ab99a2009-05-03 18:23:30 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 /** Return true iff the bitmap has empty dimensions.
82 */
83 bool empty() const { return 0 == fWidth || 0 == fHeight; }
weita@google.comf9ab99a2009-05-03 18:23:30 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 /** Return true iff the bitmap has no pixels nor a pixelref. Note: this can
86 return true even if the dimensions of the bitmap are > 0 (see empty()).
87 */
88 bool isNull() const { return NULL == fPixels && NULL == fPixelRef; }
89
90 /** Return the config for the bitmap.
91 */
92 Config config() const { return (Config)fConfig; }
93 /** DEPRECATED, use config()
94 */
95 Config getConfig() const { return this->config(); }
96 /** Return the bitmap's width, in pixels.
97 */
98 int width() const { return fWidth; }
99 /** Return the bitmap's height, in pixels.
100 */
101 int height() const { return fHeight; }
102 /** Return the number of bytes between subsequent rows of the bitmap.
103 */
104 int rowBytes() const { return fRowBytes; }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for
107 2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0
108 for configs that are not at least 1-byte per pixel (e.g. kA1_Config
109 or kNo_Config)
110 */
111 int shiftPerPixel() const { return fBytesPerPixel >> 1; }
112
113 /** Return the number of bytes per pixel based on the config. If the config
114 does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned.
115 */
116 int bytesPerPixel() const { return fBytesPerPixel; }
117
118 /** Return the rowbytes expressed as a number of pixels (like width and
119 height). Note, for 1-byte per pixel configs like kA8_Config, this will
120 return the same as rowBytes(). Is undefined for configs that are less
121 than 1-byte per pixel (e.g. kA1_Config)
122 */
123 int rowBytesAsPixels() const { return fRowBytes >> (fBytesPerPixel >> 1); }
124
125 /** Return the address of the pixels for this SkBitmap.
126 */
127 void* getPixels() const { return fPixels; }
128
129 /** Return the byte size of the pixels, based on the height and rowBytes.
130 Note this truncates the result to 32bits. Call getSize64() to detect
131 if the real size exceeds 32bits.
132 */
133 size_t getSize() const { return fHeight * fRowBytes; }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 /** Return the byte size of the pixels, based on the height and rowBytes.
136 This routine is slightly slower than getSize(), but does not truncate
137 the answer to 32bits.
138 */
139 Sk64 getSize64() const {
140 Sk64 size;
141 size.setMul(fHeight, fRowBytes);
142 return size;
143 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
146 */
147 bool isOpaque() const;
148 /** Specify if this bitmap's pixels are all opaque or not. Is only meaningful for configs
149 that support per-pixel alpha (RGB32, A1, A8).
150 */
151 void setIsOpaque(bool);
152
153 /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
154 owner of the pixels, that ownership is decremented.
155 */
156 void reset();
157
158 /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically
159 if you pass 0 for rowBytes to setConfig().
160 */
161 static int ComputeRowBytes(Config c, int width);
162
163 /** Return the bytes-per-pixel for the specified config. If the config is
164 not at least 1-byte per pixel, return 0, including for kNo_Config.
165 */
166 static int ComputeBytesPerPixel(Config c);
167
168 /** Return the shift-per-pixel for the specified config. If the config is
169 not at least 1-byte per pixel, return 0, including for kNo_Config.
170 */
171 static int ComputeShiftPerPixel(Config c) {
172 return ComputeBytesPerPixel(c) >> 1;
173 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 static Sk64 ComputeSize64(Config, int width, int height);
176 static size_t ComputeSize(Config, int width, int height);
177
178 /** Set the bitmap's config and dimensions. If rowBytes is 0, then
179 ComputeRowBytes() is called to compute the optimal value. This resets
180 any pixel/colortable ownership, just like reset().
181 */
182 void setConfig(Config, int width, int height, int rowBytes = 0);
183 /** Use this to assign a new pixel address for an existing bitmap. This
184 will automatically release any pixelref previously installed. Only call
185 this if you are handling ownership/lifetime of the pixel memory.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 If the bitmap retains a reference to the colortable (assuming it is
188 not null) it will take care of incrementing the reference count.
189
190 @param pixels Address for the pixels, managed by the caller.
191 @param ctable ColorTable (or null) that matches the specified pixels
192 */
193 void setPixels(void* p, SkColorTable* ctable = NULL);
194
195 /** Use the standard HeapAllocator to create the pixelref that manages the
196 pixel memory. It will be sized based on the current width/height/config.
197 If this is called multiple times, a new pixelref object will be created
198 each time.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 If the bitmap retains a reference to the colortable (assuming it is
201 not null) it will take care of incrementing the reference count.
202
203 @param ctable ColorTable (or null) to use with the pixels that will
204 be allocated. Only used if config == Index8_Config
205 @return true if the allocation succeeds. If not the pixelref field of
206 the bitmap will be unchanged.
207 */
208 bool allocPixels(SkColorTable* ctable = NULL) {
209 return this->allocPixels(NULL, ctable);
210 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 /** Use the specified Allocator to create the pixelref that manages the
213 pixel memory. It will be sized based on the current width/height/config.
214 If this is called multiple times, a new pixelref object will be created
215 each time.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000216
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 If the bitmap retains a reference to the colortable (assuming it is
218 not null) it will take care of incrementing the reference count.
weita@google.comf9ab99a2009-05-03 18:23:30 +0000219
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 @param allocator The Allocator to use to create a pixelref that can
221 manage the pixel memory for the current
222 width/height/config. If allocator is NULL, the standard
223 HeapAllocator will be used.
224 @param ctable ColorTable (or null) to use with the pixels that will
225 be allocated. Only used if config == Index8_Config.
226 If it is non-null and the config is not Index8, it will
227 be ignored.
228 @return true if the allocation succeeds. If not the pixelref field of
229 the bitmap will be unchanged.
230 */
231 bool allocPixels(Allocator* allocator, SkColorTable* ctable);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 /** Return the current pixelref object, of any
234 */
235 SkPixelRef* pixelRef() const { return fPixelRef; }
236 /** Return the offset into the pixelref, if any. Will return 0 if there is
237 no pixelref installed.
238 */
239 size_t pixelRefOffset() const { return fPixelRefOffset; }
240 /** Assign a pixelref and optional offset. Pixelrefs are reference counted,
241 so the existing one (if any) will be unref'd and the new one will be
242 ref'd.
243 */
244 SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset = 0);
weita@google.comf9ab99a2009-05-03 18:23:30 +0000245
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 /** Call this to ensure that the bitmap points to the current pixel address
247 in the pixelref. Balance it with a call to unlockPixels(). These calls
248 are harmless if there is no pixelref.
249 */
250 void lockPixels() const;
251 /** When you are finished access the pixel memory, call this to balance a
252 previous call to lockPixels(). This allows pixelrefs that implement
253 cached/deferred image decoding to know when there are active clients of
254 a given image.
255 */
256 void unlockPixels() const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
259 it has non-null pixels, and if required by its config, it has a
260 non-null colortable. Returns true if all of the above are met.
261 */
262 bool readyToDraw() const {
263 return this->getPixels() != NULL &&
264 ((this->config() != kIndex8_Config && this->config() != kRLE_Index8_Config) ||
265 fColorTable != NULL);
266 }
267
reed@android.comce4e53a2010-09-09 16:01:26 +0000268 /** Returns the pixelRef's texture, or NULL
269 */
270 SkGpuTexture* getTexture() const;
271
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 /** Return the bitmap's colortable (if any). Does not affect the colortable's
273 reference count.
274 */
275 SkColorTable* getColorTable() const { return fColorTable; }
276
277 /** Returns a non-zero, unique value corresponding to the pixels in our
278 pixelref, or 0 if we do not have a pixelref. Each time the pixels are
279 changed (and notifyPixelsChanged is called), a different generation ID
280 will be returned.
281 */
282 uint32_t getGenerationID() const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000283
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 /** Call this if you have changed the contents of the pixels. This will in-
285 turn cause a different generation ID value to be returned from
286 getGenerationID().
287 */
288 void notifyPixelsChanged() const;
289
290 /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format
291 for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is ignored.
292 If the config is kA8_Config, then the r,g,b parameters are ignored.
293 */
294 void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
295 /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format
296 for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is presumed
297 to be 0xFF. If the config is kA8_Config, then the r,g,b parameters are ignored and the
298 pixels are all set to 0xFF.
299 */
300 void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
301 this->eraseARGB(0xFF, r, g, b);
302 }
303 /** Initialize the bitmap's pixels with the specified color, automatically converting into the correct format
304 for the bitmap's config. If the config is kRGB_565_Config, then the color's alpha value is presumed
305 to be 0xFF. If the config is kA8_Config, then only the color's alpha value is used.
306 */
307 void eraseColor(SkColor c) const {
308 this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
309 SkColorGetB(c));
310 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000311
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
313 no pixels allocated (i.e. getPixels() returns null) the method will
314 still update the inval region (if present).
315
316 @param subset The subset of the bitmap to scroll/move. To scroll the
317 entire contents, specify [0, 0, width, height] or just
318 pass null.
319 @param dx The amount to scroll in X
320 @param dy The amount to scroll in Y
321 @param inval Optional (may be null). Returns the area of the bitmap that
322 was scrolled away. E.g. if dx = dy = 0, then inval would
323 be set to empty. If dx >= width or dy >= height, then
324 inval would be set to the entire bounds of the bitmap.
325 @return true if the scroll was doable. Will return false if the bitmap
326 uses an unsupported config for scrolling (only kA8,
327 kIndex8, kRGB_565, kARGB_4444, kARGB_8888 are supported).
328 If no pixels are present (i.e. getPixels() returns false)
329 inval will still be updated, and true will be returned.
330 */
331 bool scrollRect(const SkIRect* subset, int dx, int dy,
332 SkRegion* inval = NULL) const;
333
334 /** Returns the address of the specified pixel. This performs a runtime
335 check to know the size of the pixels, and will return the same answer
336 as the corresponding size-specific method (e.g. getAddr16). Since the
337 check happens at runtime, it is much slower than using a size-specific
338 version. Unlike the size-specific methods, this routine also checks if
339 getPixels() returns null, and returns that. The size-specific routines
340 perform a debugging assert that getPixels() is not null, but they do
341 not do any runtime checks.
342 */
343 void* getAddr(int x, int y) const;
344
345 /** Returns the address of the pixel specified by x,y for 32bit pixels.
346 */
347 inline uint32_t* getAddr32(int x, int y) const;
348 /** Returns the address of the pixel specified by x,y for 16bit pixels.
349 */
350 inline uint16_t* getAddr16(int x, int y) const;
351 /** Returns the address of the pixel specified by x,y for 8bit pixels.
352 */
353 inline uint8_t* getAddr8(int x, int y) const;
354 /** Returns the address of the byte containing the pixel specified by x,y
355 for 1bit pixels.
356 */
357 inline uint8_t* getAddr1(int x, int y) const;
358
359 /** Returns the color corresponding to the pixel specified by x,y for
360 colortable based bitmaps.
361 */
362 inline SkPMColor getIndex8Color(int x, int y) const;
363
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 /** Set dst to be a setset of this bitmap. If possible, it will share the
365 pixel memory, and just point into a subset of it. However, if the config
366 does not support this, a local copy will be made and associated with
367 the dst bitmap. If the subset rectangle, intersected with the bitmap's
368 dimensions is empty, or if there is an unsupported config, false will be
369 returned and dst will be untouched.
370 @param dst The bitmap that will be set to a subset of this bitmap
371 @param subset The rectangle of pixels in this bitmap that dst will
372 reference.
373 @return true if the subset copy was successfully made.
374 */
375 bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
376
reed@android.comfbaa88d2009-05-06 17:44:34 +0000377 /** Makes a deep copy of this bitmap, respecting the requested config.
378 Returns false if either there is an error (i.e. the src does not have
379 pixels) or the request cannot be satisfied (e.g. the src has per-pixel
380 alpha, and the requested config does not support alpha).
381 @param dst The bitmap to be sized and allocated
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382 @param c The desired config for dst
383 @param allocator Allocator used to allocate the pixelref for the dst
384 bitmap. If this is null, the standard HeapAllocator
385 will be used.
386 @return true if the copy could be made.
387 */
388 bool copyTo(SkBitmap* dst, Config c, Allocator* allocator = NULL) const;
reed@android.com89bb83a2009-05-29 21:30:42 +0000389
reed@android.comfbaa88d2009-05-06 17:44:34 +0000390 /** Returns true if this bitmap can be deep copied into the requested config
391 by calling copyTo().
392 */
393 bool canCopyTo(Config newConfig) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394
395 bool hasMipMap() const;
396 void buildMipMap(bool forceRebuild = false);
397 void freeMipMap();
398
399 /** Given scale factors sx, sy, determine the miplevel available in the
400 bitmap, and return it (this is the amount to shift matrix iterators
401 by). If dst is not null, it is set to the correct level.
402 */
403 int extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy);
404
405 void extractAlpha(SkBitmap* dst) const {
406 this->extractAlpha(dst, NULL, NULL);
407 }
408
409 void extractAlpha(SkBitmap* dst, const SkPaint* paint,
410 SkIPoint* offset) const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 void flatten(SkFlattenableWriteBuffer&) const;
413 void unflatten(SkFlattenableReadBuffer&);
414
415 SkDEBUGCODE(void validate() const;)
416
417 class Allocator : public SkRefCnt {
418 public:
419 /** Allocate the pixel memory for the bitmap, given its dimensions and
420 config. Return true on success, where success means either setPixels
421 or setPixelRef was called. The pixels need not be locked when this
422 returns. If the config requires a colortable, it also must be
423 installed via setColorTable. If false is returned, the bitmap and
424 colortable should be left unchanged.
425 */
426 virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
427 };
428
429 /** Subclass of Allocator that returns a pixelref that allocates its pixel
430 memory from the heap. This is the default Allocator invoked by
431 allocPixels().
432 */
433 class HeapAllocator : public Allocator {
434 public:
435 virtual bool allocPixelRef(SkBitmap*, SkColorTable*);
436 };
437
438 class RLEPixels {
439 public:
440 RLEPixels(int width, int height);
441 virtual ~RLEPixels();
weita@google.comf9ab99a2009-05-03 18:23:30 +0000442
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 uint8_t* packedAtY(int y) const {
444 SkASSERT((unsigned)y < (unsigned)fHeight);
445 return fYPtrs[y];
446 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000447
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 // called by subclasses during creation
449 void setPackedAtY(int y, uint8_t* addr) {
450 SkASSERT((unsigned)y < (unsigned)fHeight);
451 fYPtrs[y] = addr;
452 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000453
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 private:
455 uint8_t** fYPtrs;
456 int fHeight;
457 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000458
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 struct MipMap;
461 mutable MipMap* fMipMap;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462
463 mutable SkPixelRef* fPixelRef;
464 mutable size_t fPixelRefOffset;
465 mutable int fPixelLockCount;
466 // either user-specified (in which case it is not treated as mutable)
467 // or a cache of the returned value from fPixelRef->lockPixels()
468 mutable void* fPixels;
469 mutable SkColorTable* fColorTable; // only meaningful for kIndex8
470
471 enum Flags {
472 kImageIsOpaque_Flag = 0x01
473 };
474
475 uint32_t fRowBytes;
reed@android.comf459a492009-03-27 12:33:50 +0000476 uint32_t fWidth;
477 uint32_t fHeight;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 uint8_t fConfig;
479 uint8_t fFlags;
480 uint8_t fBytesPerPixel; // based on config
481
482 /* Unreference any pixelrefs or colortables
483 */
484 void freePixels();
485 void updatePixelsFromRef() const;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000486
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487 static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy);
488};
489
490/** \class SkColorTable
491
492 SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
493 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
494*/
495class SkColorTable : public SkRefCnt {
496public:
weita@google.comf9ab99a2009-05-03 18:23:30 +0000497 /** Makes a deep copy of colors.
498 */
499 SkColorTable(const SkColorTable& src);
500 /** Preallocates the colortable to have 'count' colors, which
501 * are initially set to 0.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502 */
503 explicit SkColorTable(int count);
504 explicit SkColorTable(SkFlattenableReadBuffer&);
505 SkColorTable(const SkPMColor colors[], int count);
506 virtual ~SkColorTable();
507
508 enum Flags {
509 kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the table are opaque (alpha==0xFF)
510 };
511 /** Returns the flag bits for the color table. These can be changed with setFlags().
512 */
513 unsigned getFlags() const { return fFlags; }
514 /** Set the flags for the color table. See the Flags enum for possible values.
515 */
516 void setFlags(unsigned flags);
517
reed@android.comcafc9f92009-08-22 03:44:57 +0000518 bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; }
519 void setIsOpaque(bool isOpaque);
520
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 /** Returns the number of colors in the table.
522 */
523 int count() const { return fCount; }
524
525 /** Returns the specified color from the table. In the debug build, this asserts that
526 the index is in range (0 <= index < count).
527 */
528 SkPMColor operator[](int index) const {
529 SkASSERT(fColors != NULL && (unsigned)index < fCount);
530 return fColors[index];
531 }
532
533 /** Specify the number of colors in the color table. This does not initialize the colors
534 to any value, just allocates memory for them. To initialize the values, either call
535 setColors(array, count), or follow setCount(count) with a call to
536 lockColors()/{set the values}/unlockColors(true).
537 */
538// void setColors(int count) { this->setColors(NULL, count); }
539// void setColors(const SkPMColor[], int count);
540
541 /** Return the array of colors for reading and/or writing. This must be
542 balanced by a call to unlockColors(changed?), telling the colortable if
543 the colors were changed during the lock.
544 */
545 SkPMColor* lockColors() {
546 SkDEBUGCODE(fColorLockCount += 1;)
547 return fColors;
548 }
549 /** Balancing call to lockColors(). If the colors have been changed, pass true.
550 */
551 void unlockColors(bool changed);
552
553 /** Similar to lockColors(), lock16BitCache() returns the array of
554 RGB16 colors that mirror the 32bit colors. However, this function
555 will return null if kColorsAreOpaque_Flag is not set.
556 Also, unlike lockColors(), the returned array here cannot be modified.
557 */
558 const uint16_t* lock16BitCache();
559 /** Balancing call to lock16BitCache().
560 */
561 void unlock16BitCache() {
562 SkASSERT(f16BitCacheLockCount > 0);
563 SkDEBUGCODE(f16BitCacheLockCount -= 1);
564 }
565
566 void flatten(SkFlattenableWriteBuffer&) const;
567
568private:
569 SkPMColor* fColors;
570 uint16_t* f16BitCache;
571 uint16_t fCount;
572 uint8_t fFlags;
573 SkDEBUGCODE(int fColorLockCount;)
574 SkDEBUGCODE(int f16BitCacheLockCount;)
575
576 void inval16BitCache();
577};
578
579class SkAutoLockPixels {
580public:
581 SkAutoLockPixels(const SkBitmap& bitmap) : fBitmap(bitmap) {
582 bitmap.lockPixels();
583 }
584 ~SkAutoLockPixels() {
585 fBitmap.unlockPixels();
586 }
587
588private:
589 const SkBitmap& fBitmap;
590};
591
592/** Helper class that performs the lock/unlockColors calls on a colortable.
593 The destructor will call unlockColors(false) if it has a bitmap's colortable
594*/
595class SkAutoLockColors : public SkNoncopyable {
596public:
597 /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
598 colortable
599 */
600 SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
601 /** Initialize with bitmap, locking its colortable if present
602 */
603 explicit SkAutoLockColors(const SkBitmap& bm) {
604 fCTable = bm.getColorTable();
605 fColors = fCTable ? fCTable->lockColors() : NULL;
606 }
607 /** Initialize with a colortable (may be null)
608 */
609 explicit SkAutoLockColors(SkColorTable* ctable) {
610 fCTable = ctable;
611 fColors = ctable ? ctable->lockColors() : NULL;
612 }
613 ~SkAutoLockColors() {
614 if (fCTable) {
615 fCTable->unlockColors(false);
616 }
617 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000618
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619 /** Return the currently locked colors, or NULL if no bitmap's colortable
620 is currently locked.
621 */
622 const SkPMColor* colors() const { return fColors; }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000623
reed@android.com11344262009-07-08 20:09:23 +0000624 /** Locks the table and returns is colors (assuming ctable is not null) and
625 unlocks the previous table if one was present
626 */
627 const SkPMColor* lockColors(SkColorTable* ctable) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 if (fCTable) {
629 fCTable->unlockColors(false);
630 }
reed@android.com11344262009-07-08 20:09:23 +0000631 fCTable = ctable;
632 fColors = ctable ? ctable->lockColors() : NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 return fColors;
634 }
635
reed@android.com11344262009-07-08 20:09:23 +0000636 const SkPMColor* lockColors(const SkBitmap& bm) {
637 return this->lockColors(bm.getColorTable());
638 }
639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640private:
641 SkColorTable* fCTable;
642 const SkPMColor* fColors;
643};
644
645///////////////////////////////////////////////////////////////////////////////
646
647inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
648 SkASSERT(fPixels);
649 SkASSERT(fConfig == kARGB_8888_Config);
650 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
651 return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
652}
653
654inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
655 SkASSERT(fPixels);
656 SkASSERT(fConfig == kRGB_565_Config || fConfig == kARGB_4444_Config);
657 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
658 return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
659}
660
661inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
662 SkASSERT(fPixels);
663 SkASSERT(fConfig == kA8_Config || fConfig == kIndex8_Config);
664 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
665 return (uint8_t*)fPixels + y * fRowBytes + x;
666}
667
668inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
669 SkASSERT(fPixels);
670 SkASSERT(fConfig == kIndex8_Config);
671 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
672 SkASSERT(fColorTable);
673 return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
674}
675
676// returns the address of the byte that contains the x coordinate
677inline uint8_t* SkBitmap::getAddr1(int x, int y) const {
678 SkASSERT(fPixels);
679 SkASSERT(fConfig == kA1_Config);
680 SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
681 return (uint8_t*)fPixels + y * fRowBytes + (x >> 3);
682}
683
684#endif
685