1 2 /* 3 * Copyright 2006 The Android Open Source Project 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 #include "SkDrawBitmap.h" 11 #include "SkAnimateMaker.h" 12 #include "SkCanvas.h" 13 #include "SkImageDecoder.h" 14 #include "SkPaint.h" 15 #include "SkStream.h" 16 17 #if SK_USE_CONDENSED_INFO == 0 18 19 const SkMemberInfo SkBaseBitmap::fInfo[] = { 20 SK_MEMBER(x, Float), 21 SK_MEMBER(y, Float) 22 }; 23 24 #endif 25 26 DEFINE_GET_MEMBER(SkBaseBitmap); 27 28 SkBaseBitmap::SkBaseBitmap() : x(0), y(0) { 29 } 30 31 SkBaseBitmap::~SkBaseBitmap() { 32 } 33 34 bool SkBaseBitmap::draw(SkAnimateMaker& maker) { 35 SkBoundableAuto boundable(this, maker); 36 maker.fCanvas->drawBitmap(fBitmap, x, y, maker.fPaint); 37 return false; 38 } 39 40 enum SkDrawBitmap_Properties { 41 SK_PROPERTY(erase) 42 }; 43 44 #if SK_USE_CONDENSED_INFO == 0 45 46 const SkMemberInfo SkDrawBitmap::fInfo[] = { 47 SK_MEMBER_INHERITED, 48 SK_MEMBER_PROPERTY(erase, ARGB), 49 SK_MEMBER(format, BitmapFormat), 50 SK_MEMBER(height, Int), 51 SK_MEMBER(rowBytes, Int), 52 SK_MEMBER(width, Int), 53 }; 54 55 #endif 56 57 DEFINE_GET_MEMBER(SkDrawBitmap); 58 59 SkDrawBitmap::SkDrawBitmap() : format((SkBitmap::Config) -1), height(-1), 60 rowBytes(0), width(-1), fColor(0), fColorSet(false) { 61 } 62 63 SkDrawBitmap::~SkDrawBitmap() { 64 } 65 66 #ifdef SK_DUMP_ENABLED 67 void SkDrawBitmap::dump(SkAnimateMaker* maker) { 68 dumpBase(maker); 69 dumpAttrs(maker); 70 if (fColorSet) 71 SkDebugf("erase=\"argb(%d,%d,%d,%d)\" ", SkColorGetA(fColor)/255, SkColorGetR(fColor), 72 SkColorGetG(fColor), SkColorGetB(fColor)); 73 if (rowBytes > 0) 74 SkDebugf("rowBytes=\"%d\" ", rowBytes); 75 const char* formatName; 76 switch (format) { 77 case 0: formatName = "none"; break; 78 case 1: formatName = "A8"; break; 79 case 2: formatName = "Index8"; break; 80 case 3: formatName = "RGB16"; break; 81 case 4: formatName = "RGB32"; break; 82 } 83 SkDebugf("format=\"%s\" />\n", formatName); 84 } 85 #endif 86 87 void SkDrawBitmap::onEndElement(SkAnimateMaker&) { 88 SkASSERT(width != -1); 89 SkASSERT(height != -1); 90 SkASSERT(rowBytes >= 0); 91 fBitmap.setConfig((SkBitmap::Config) format, width, height, rowBytes); 92 fBitmap.allocPixels(); 93 if (fColorSet) 94 fBitmap.eraseColor(fColor); 95 } 96 97 bool SkDrawBitmap::setProperty(int index, SkScriptValue& value) 98 { 99 switch (index) { 100 case SK_PROPERTY(erase): 101 SkASSERT(value.fType == SkType_ARGB); 102 fColor = value.fOperand.fS32; 103 fColorSet = true; 104 break; 105 default: 106 SkASSERT(0); 107 return false; 108 } 109 return true; 110 } 111 112 113 enum SkImageBaseBitmap_Properties { 114 SK_PROPERTY(height), 115 SK_PROPERTY(width) 116 }; 117 118 #if SK_USE_CONDENSED_INFO == 0 119 120 const SkMemberInfo SkImageBaseBitmap::fInfo[] = { 121 SK_MEMBER_INHERITED, 122 SK_MEMBER(base64, Base64), 123 SK_MEMBER_PROPERTY(height, Int), 124 SK_MEMBER(src, String), 125 SK_MEMBER_PROPERTY(width, Int) 126 }; 127 128 #endif 129 130 DEFINE_GET_MEMBER(SkImageBaseBitmap); 131 132 SkImageBaseBitmap::SkImageBaseBitmap() : fDirty(true), fUriBase(NULL) { 133 base64.fData = NULL; 134 base64.fLength = 0; 135 } 136 137 SkImageBaseBitmap::~SkImageBaseBitmap() { 138 delete[] base64.fData; 139 } 140 141 SkDisplayable* SkImageBaseBitmap::deepCopy(SkAnimateMaker* maker) { 142 SkDisplayable* copy = INHERITED::deepCopy(maker); 143 ((SkImageBaseBitmap*) copy)->fUriBase = ((SkImageBaseBitmap*) this)->fUriBase; 144 return copy; 145 } 146 147 void SkImageBaseBitmap::dirty() { 148 fDirty = true; 149 } 150 151 bool SkImageBaseBitmap::draw(SkAnimateMaker& maker) { 152 if (fDirty) 153 resolve(); 154 return INHERITED::draw(maker); 155 } 156 157 bool SkImageBaseBitmap::getProperty(int index, SkScriptValue* value) const { 158 if (fDirty) 159 resolve(); 160 switch (index) { 161 case SK_PROPERTY(height): 162 value->fOperand.fS32 = fBitmap.height(); 163 break; 164 case SK_PROPERTY(width): 165 value->fOperand.fS32 = fBitmap.width(); 166 break; 167 default: 168 SkASSERT(0); 169 return false; 170 } 171 value->fType = SkType_Int; 172 return true; 173 } 174 175 void SkImageBaseBitmap::onEndElement(SkAnimateMaker& maker) { 176 fUriBase = maker.fPrefix.c_str(); 177 } 178 179 void SkImageBaseBitmap::resolve() { 180 fDirty = false; 181 if (base64.fData) { 182 fBitmap.reset(); 183 SkImageDecoder::DecodeMemory(base64.fData, base64.fLength, &fBitmap); 184 } else if (src.size()) { 185 if (fLast.equals(src)) 186 return; 187 fLast.set(src); 188 fBitmap.reset(); 189 190 //SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str()); 191 SkAutoTUnref<SkStreamAsset> stream(SkStream::NewFromFile(src.c_str())); 192 if (stream.get()) { 193 SkImageDecoder::DecodeStream(stream, &fBitmap); 194 } 195 } 196 } 197