blob: cfd5110fe0f368a2e5263220e841109efea979d9 [file] [log] [blame]
djsollen@google.com64a0ec32012-06-12 15:17:27 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkColorTable_DEFINED
11#define SkColorTable_DEFINED
12
13#include "SkColor.h"
14#include "SkFlattenable.h"
reed@google.com3443fd82013-11-13 19:09:13 +000015#include "SkImageInfo.h"
djsollen@google.com64a0ec32012-06-12 15:17:27 +000016
17/** \class SkColorTable
18
19 SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
20 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
21*/
ch.dumez43f9d3e2014-08-05 12:20:19 -070022class SK_API SkColorTable : public SkRefCnt {
djsollen@google.com64a0ec32012-06-12 15:17:27 +000023public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000024 SK_DECLARE_INST_COUNT(SkColorTable)
25
djsollen@google.com64a0ec32012-06-12 15:17:27 +000026 /** Makes a deep copy of colors.
27 */
28 SkColorTable(const SkColorTable& src);
reedc5e15a12014-09-29 12:10:27 -070029 SkColorTable(const SkPMColor colors[], int count);
djsollen@google.com64a0ec32012-06-12 15:17:27 +000030 virtual ~SkColorTable();
31
djsollen@google.com64a0ec32012-06-12 15:17:27 +000032 /** Returns the number of colors in the table.
33 */
34 int count() const { return fCount; }
35
36 /** Returns the specified color from the table. In the debug build, this asserts that
37 the index is in range (0 <= index < count).
38 */
39 SkPMColor operator[](int index) const {
reedc5e15a12014-09-29 12:10:27 -070040 SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
djsollen@google.com64a0ec32012-06-12 15:17:27 +000041 return fColors[index];
42 }
43
reed@google.com0a6151d2013-10-10 14:44:56 +000044 /**
45 * Return the array of colors for reading. This must be balanced by a call
46 * to unlockColors().
47 */
48 const SkPMColor* lockColors() {
djsollen@google.comc9ab9872012-08-29 18:52:07 +000049 SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);)
djsollen@google.com64a0ec32012-06-12 15:17:27 +000050 return fColors;
51 }
reed@google.com0a6151d2013-10-10 14:44:56 +000052
53 /**
54 * Balancing call to lockColors().
55 */
56 void unlockColors();
djsollen@google.com64a0ec32012-06-12 15:17:27 +000057
58 /** Similar to lockColors(), lock16BitCache() returns the array of
59 RGB16 colors that mirror the 32bit colors. However, this function
60 will return null if kColorsAreOpaque_Flag is not set.
61 Also, unlike lockColors(), the returned array here cannot be modified.
62 */
63 const uint16_t* lock16BitCache();
64 /** Balancing call to lock16BitCache().
65 */
66 void unlock16BitCache() {
67 SkASSERT(f16BitCacheLockCount > 0);
robertphillips5b693772014-11-21 06:19:36 -080068 SkDEBUGCODE(sk_atomic_dec(&f16BitCacheLockCount);)
djsollen@google.com64a0ec32012-06-12 15:17:27 +000069 }
70
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000071 explicit SkColorTable(SkReadBuffer&);
72 void writeToBuffer(SkWriteBuffer&) const;
djsollen@google.com64a0ec32012-06-12 15:17:27 +000073
74private:
75 SkPMColor* fColors;
76 uint16_t* f16BitCache;
reedc5e15a12014-09-29 12:10:27 -070077 int fCount;
djsollen@google.com64a0ec32012-06-12 15:17:27 +000078 SkDEBUGCODE(int fColorLockCount;)
79 SkDEBUGCODE(int f16BitCacheLockCount;)
80
reedc5e15a12014-09-29 12:10:27 -070081 void init(const SkPMColor* colors, int count);
82
djsollen@google.com64a0ec32012-06-12 15:17:27 +000083 void inval16BitCache();
84
commit-bot@chromium.orge0e1da32013-10-15 20:36:16 +000085 typedef SkRefCnt INHERITED;
djsollen@google.com64a0ec32012-06-12 15:17:27 +000086};
87
88#endif