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 "SkImageDecoder.h" | 8 #include "SkImageDecoder.h" |
9 #include "SkImage_Base.h" | 9 #include "SkImage_Base.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 | 13 |
14 class SkImage_Codec : public SkImage_Base { | 14 class SkImage_Codec : public SkImage_Base { |
15 public: | 15 public: |
16 static SkImage* NewEmpty(); | 16 static SkImage* NewEmpty(); |
17 | 17 |
18 SkImage_Codec(SkData* encodedData, int width, int height); | 18 SkImage_Codec(SkData* encodedData, int width, int height); |
19 virtual ~SkImage_Codec(); | 19 virtual ~SkImage_Codec(); |
20 | 20 |
21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI
DE; | 21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_
OVERRIDE; |
22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const
SkPaint*) SK_OVERRIDE; | 22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, |
| 23 const SkPaint*) const SK_OVERRIDE; |
23 | 24 |
24 private: | 25 private: |
25 SkData* fEncodedData; | 26 SkData* fEncodedData; |
26 SkBitmap fBitmap; | 27 SkBitmap fBitmap; |
27 | 28 |
28 typedef SkImage_Base INHERITED; | 29 typedef SkImage_Base INHERITED; |
29 }; | 30 }; |
30 | 31 |
31 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
32 | 33 |
33 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { | 34 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { |
34 fEncodedData = data; | 35 fEncodedData = data; |
35 fEncodedData->ref(); | 36 fEncodedData->ref(); |
36 } | 37 } |
37 | 38 |
38 SkImage_Codec::~SkImage_Codec() { | 39 SkImage_Codec::~SkImage_Codec() { |
39 fEncodedData->unref(); | 40 fEncodedData->unref(); |
40 } | 41 } |
41 | 42 |
42 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) { | 43 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) const { |
43 if (!fBitmap.pixelRef()) { | 44 if (!fBitmap.pixelRef()) { |
44 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), &fBitmap)) { | 45 // todo: this needs to be thread-safe |
| 46 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap); |
| 47 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), bitmap)) { |
45 return; | 48 return; |
46 } | 49 } |
47 } | 50 } |
48 canvas->drawBitmap(fBitmap, x, y, paint); | 51 canvas->drawBitmap(fBitmap, x, y, paint); |
49 } | 52 } |
50 | 53 |
51 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, | 54 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const
SkRect& dst, |
52 const SkRect& dst, const SkPaint* paint) { | 55 const SkPaint* paint) const { |
53 if (!fBitmap.pixelRef()) { | 56 if (!fBitmap.pixelRef()) { |
54 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), &fBitmap)) { | 57 // todo: this needs to be thread-safe |
| 58 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap); |
| 59 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), bitmap)) { |
55 return; | 60 return; |
56 } | 61 } |
57 } | 62 } |
58 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); | 63 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
59 } | 64 } |
60 | 65 |
61 /////////////////////////////////////////////////////////////////////////////// | 66 /////////////////////////////////////////////////////////////////////////////// |
62 | 67 |
63 SkImage* SkImage::NewEncodedData(SkData* data) { | 68 SkImage* SkImage::NewEncodedData(SkData* data) { |
64 if (NULL == data) { | 69 if (NULL == data) { |
65 return NULL; | 70 return NULL; |
66 } | 71 } |
67 | 72 |
68 SkBitmap bitmap; | 73 SkBitmap bitmap; |
69 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnk
nown_SkColorType, | 74 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnk
nown_SkColorType, |
70 SkImageDecoder::kDecodeBounds_Mode)) { | 75 SkImageDecoder::kDecodeBounds_Mode)) { |
71 return NULL; | 76 return NULL; |
72 } | 77 } |
73 | 78 |
74 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); | 79 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); |
75 } | 80 } |
OLD | NEW |