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*) const SK_
OVERRIDE; | 21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_
OVERRIDE; |
22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, | 22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, |
23 const SkPaint*) const SK_OVERRIDE; | 23 const SkPaint*) const SK_OVERRIDE; |
24 | 24 |
25 virtual bool isOpaque() const SK_OVERRIDE; | 25 virtual bool isOpaque() const SK_OVERRIDE; |
26 | 26 |
27 private: | 27 private: |
| 28 bool ensureBitmapDecoded() const; |
| 29 |
28 SkData* fEncodedData; | 30 SkData* fEncodedData; |
29 SkBitmap fBitmap; | 31 SkBitmap fBitmap; |
30 | 32 |
31 typedef SkImage_Base INHERITED; | 33 typedef SkImage_Base INHERITED; |
32 }; | 34 }; |
33 | 35 |
34 /////////////////////////////////////////////////////////////////////////////// | 36 /////////////////////////////////////////////////////////////////////////////// |
35 | 37 |
36 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { | 38 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { |
37 fEncodedData = data; | 39 fEncodedData = data; |
38 fEncodedData->ref(); | 40 fEncodedData->ref(); |
39 } | 41 } |
40 | 42 |
41 SkImage_Codec::~SkImage_Codec() { | 43 SkImage_Codec::~SkImage_Codec() { |
42 fEncodedData->unref(); | 44 fEncodedData->unref(); |
43 } | 45 } |
44 | 46 |
45 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) const { | 47 bool SkImage_Codec::ensureBitmapDecoded() const { |
46 if (!fBitmap.pixelRef()) { | 48 if (!fBitmap.pixelRef()) { |
47 // todo: this needs to be thread-safe | 49 // todo: this needs to be thread-safe |
48 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap); | 50 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap); |
49 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), bitmap)) { | 51 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), bitmap)) { |
50 return; | 52 return false; |
51 } | 53 } |
52 } | 54 } |
| 55 return true; |
| 56 } |
| 57 |
| 58 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) const { |
| 59 if(!this->ensureBitmapDecoded()) { |
| 60 return; |
| 61 } |
| 62 |
53 canvas->drawBitmap(fBitmap, x, y, paint); | 63 canvas->drawBitmap(fBitmap, x, y, paint); |
54 } | 64 } |
55 | 65 |
56 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const
SkRect& dst, | 66 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const
SkRect& dst, |
57 const SkPaint* paint) const { | 67 const SkPaint* paint) const { |
58 if (!fBitmap.pixelRef()) { | 68 if(!this->ensureBitmapDecoded()) { |
59 // todo: this needs to be thread-safe | 69 return; |
60 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap); | |
61 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), bitmap)) { | |
62 return; | |
63 } | |
64 } | 70 } |
| 71 |
65 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); | 72 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
66 } | 73 } |
67 | 74 |
68 /////////////////////////////////////////////////////////////////////////////// | 75 /////////////////////////////////////////////////////////////////////////////// |
69 | 76 |
70 SkImage* SkImage::NewEncodedData(SkData* data) { | 77 SkImage* SkImage::NewEncodedData(SkData* data) { |
71 if (NULL == data) { | 78 if (NULL == data) { |
72 return NULL; | 79 return NULL; |
73 } | 80 } |
74 | 81 |
75 SkBitmap bitmap; | 82 SkBitmap bitmap; |
76 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnk
nown_SkColorType, | 83 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnk
nown_SkColorType, |
77 SkImageDecoder::kDecodeBounds_Mode)) { | 84 SkImageDecoder::kDecodeBounds_Mode)) { |
78 return NULL; | 85 return NULL; |
79 } | 86 } |
80 | 87 |
81 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); | 88 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); |
82 } | 89 } |
83 | 90 |
84 | 91 |
85 bool SkImage_Codec::isOpaque() const { | 92 bool SkImage_Codec::isOpaque() const { |
86 return fBitmap.isOpaque(); | 93 return fBitmap.isOpaque(); |
87 } | 94 } |
OLD | NEW |