OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkImage_Base.h" | 8 #include "SkImage_Base.h" |
9 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 #include "SkDecodingImageGenerator.h" | 13 #include "SkDecodingImageGenerator.h" |
14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
15 | 15 |
16 class SkImage_Raster : public SkImage_Base { | 16 class SkImage_Raster : public SkImage_Base { |
17 public: | 17 public: |
18 static bool ValidArgs(const Info& info, size_t rowBytes) { | 18 static bool ValidArgs(const Info& info, size_t rowBytes) { |
19 const int maxDimension = SK_MaxS32 >> 2; | 19 const int maxDimension = SK_MaxS32 >> 2; |
20 const size_t kMaxPixelByteSize = SK_MaxS32; | 20 const size_t kMaxPixelByteSize = SK_MaxS32; |
21 | 21 |
22 if (info.width() < 0 || info.height() < 0) { | 22 if (info.width() <= 0 || info.height() <= 0) { |
23 return false; | 23 return false; |
24 } | 24 } |
25 if (info.width() > maxDimension || info.height() > maxDimension) { | 25 if (info.width() > maxDimension || info.height() > maxDimension) { |
26 return false; | 26 return false; |
27 } | 27 } |
28 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { | 28 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { |
29 return false; | 29 return false; |
30 } | 30 } |
31 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { | 31 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { |
32 return false; | 32 return false; |
33 } | 33 } |
34 | 34 |
35 if (kUnknown_SkColorType == info.colorType()) { | 35 if (kUnknown_SkColorType == info.colorType()) { |
36 return false; | 36 return false; |
37 } | 37 } |
38 | 38 |
39 // TODO: check colorspace | 39 // TODO: check colorspace |
40 | 40 |
41 if (rowBytes < SkImageMinRowBytes(info)) { | 41 if (rowBytes < SkImageMinRowBytes(info)) { |
42 return false; | 42 return false; |
43 } | 43 } |
44 | 44 |
45 int64_t size = (int64_t)info.height() * rowBytes; | 45 int64_t size = (int64_t)info.height() * rowBytes; |
46 if (size > (int64_t)kMaxPixelByteSize) { | 46 if (size > (int64_t)kMaxPixelByteSize) { |
47 return false; | 47 return false; |
48 } | 48 } |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 static SkImage* NewEmpty(); | |
53 | |
54 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, const SkSurfaceProps*
); | 52 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, const SkSurfaceProps*
); |
55 virtual ~SkImage_Raster(); | 53 virtual ~SkImage_Raster(); |
56 | 54 |
57 void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE
; | 55 void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE
; |
58 void onDrawRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) con
st SK_OVERRIDE; | 56 void onDrawRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) con
st SK_OVERRIDE; |
59 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const SK_
OVERRIDE; | 57 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const SK_
OVERRIDE; |
60 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) con
st SK_OVERRIDE; | 58 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) con
st SK_OVERRIDE; |
61 const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const SK_OVERRI
DE; | 59 const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const SK_OVERRI
DE; |
62 bool getROPixels(SkBitmap*) const SK_OVERRIDE; | 60 bool getROPixels(SkBitmap*) const SK_OVERRIDE; |
63 | 61 |
(...skipping 15 matching lines...) Expand all Loading... |
79 private: | 77 private: |
80 SkImage_Raster() : INHERITED(0, 0, NULL) {} | 78 SkImage_Raster() : INHERITED(0, 0, NULL) {} |
81 | 79 |
82 SkBitmap fBitmap; | 80 SkBitmap fBitmap; |
83 | 81 |
84 typedef SkImage_Base INHERITED; | 82 typedef SkImage_Base INHERITED; |
85 }; | 83 }; |
86 | 84 |
87 /////////////////////////////////////////////////////////////////////////////// | 85 /////////////////////////////////////////////////////////////////////////////// |
88 | 86 |
89 SkImage* SkImage_Raster::NewEmpty() { | |
90 // Returns lazily created singleton | |
91 static SkImage* gEmpty; | |
92 if (NULL == gEmpty) { | |
93 gEmpty = SkNEW(SkImage_Raster); | |
94 } | |
95 gEmpty->ref(); | |
96 return gEmpty; | |
97 } | |
98 | |
99 static void release_data(void* addr, void* context) { | 87 static void release_data(void* addr, void* context) { |
100 SkData* data = static_cast<SkData*>(context); | 88 SkData* data = static_cast<SkData*>(context); |
101 data->unref(); | 89 data->unref(); |
102 } | 90 } |
103 | 91 |
104 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes, | 92 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes, |
105 const SkSurfaceProps* props) | 93 const SkSurfaceProps* props) |
106 : INHERITED(info.width(), info.height(), props) | 94 : INHERITED(info.width(), info.height(), props) |
107 { | 95 { |
108 data->ref(); | 96 data->ref(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 150 } |
163 | 151 |
164 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { | 152 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { |
165 *dst = fBitmap; | 153 *dst = fBitmap; |
166 return true; | 154 return true; |
167 } | 155 } |
168 | 156 |
169 /////////////////////////////////////////////////////////////////////////////// | 157 /////////////////////////////////////////////////////////////////////////////// |
170 | 158 |
171 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes) { | 159 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes) { |
172 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { | 160 if (!SkImage_Raster::ValidArgs(info, rowBytes) || !pixels) { |
173 return NULL; | |
174 } | |
175 if (0 == info.width() && 0 == info.height()) { | |
176 return SkImage_Raster::NewEmpty(); | |
177 } | |
178 // check this after empty-check | |
179 if (NULL == pixels) { | |
180 return NULL; | 161 return NULL; |
181 } | 162 } |
182 | 163 |
183 // Here we actually make a copy of the caller's pixel data | 164 // Here we actually make a copy of the caller's pixel data |
184 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.height() * rowBytes)); | 165 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.height() * rowBytes)); |
185 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); | 166 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); |
186 } | 167 } |
187 | 168 |
188 | 169 |
189 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { | 170 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { |
190 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { | 171 if (!SkImage_Raster::ValidArgs(info, rowBytes) || !data) { |
191 return NULL; | |
192 } | |
193 if (0 == info.width() && 0 == info.height()) { | |
194 return SkImage_Raster::NewEmpty(); | |
195 } | |
196 // check this after empty-check | |
197 if (NULL == data) { | |
198 return NULL; | 172 return NULL; |
199 } | 173 } |
200 | 174 |
201 // did they give us enough data? | 175 // did they give us enough data? |
202 size_t size = info.height() * rowBytes; | 176 size_t size = info.height() * rowBytes; |
203 if (data->size() < size) { | 177 if (data->size() < size) { |
204 return NULL; | 178 return NULL; |
205 } | 179 } |
206 | 180 |
207 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); | 181 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); |
(...skipping 12 matching lines...) Expand all Loading... |
220 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes, props)); | 194 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes, props)); |
221 } | 195 } |
222 | 196 |
223 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { | 197 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { |
224 return ((const SkImage_Raster*)image)->getPixelRef(); | 198 return ((const SkImage_Raster*)image)->getPixelRef(); |
225 } | 199 } |
226 | 200 |
227 bool SkImage_Raster::isOpaque() const { | 201 bool SkImage_Raster::isOpaque() const { |
228 return fBitmap.isOpaque(); | 202 return fBitmap.isOpaque(); |
229 } | 203 } |
OLD | NEW |