1 /* libs/graphics/animator/SkDrawBitmap.cpp 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #include "SkDrawBitmap.h" 19 #include "SkAnimateMaker.h" 20 #include "SkCanvas.h" 21 #include "SkImageDecoder.h" 22 #include "SkPaint.h" 23 #include "SkStream.h" 24 25 #if SK_USE_CONDENSED_INFO == 0 26 27 const SkMemberInfo SkBaseBitmap::fInfo[] = { 28 SK_MEMBER(x, Float), 29 SK_MEMBER(y, Float) 30 }; 31 32 #endif 33 34 DEFINE_GET_MEMBER(SkBaseBitmap); 35 36 SkBaseBitmap::SkBaseBitmap() : x(0), y(0) { 37 } 38 39 SkBaseBitmap::~SkBaseBitmap() { 40 } 41 42 bool SkBaseBitmap::draw(SkAnimateMaker& maker) { 43 SkBoundableAuto boundable(this, maker); 44 maker.fCanvas->drawBitmap(fBitmap, x, y, maker.fPaint); 45 return false; 46 } 47 48 enum SkDrawBitmap_Properties { 49 SK_PROPERTY(erase) 50 }; 51 52 #if SK_USE_CONDENSED_INFO == 0 53 54 const SkMemberInfo SkDrawBitmap::fInfo[] = { 55 SK_MEMBER_INHERITED, 56 SK_MEMBER_PROPERTY(erase, ARGB), 57 SK_MEMBER(format, BitmapFormat), 58 SK_MEMBER(height, Int), 59 SK_MEMBER(rowBytes, Int), 60 SK_MEMBER(width, Int), 61 }; 62 63 #endif 64 65 DEFINE_GET_MEMBER(SkDrawBitmap); 66 67 SkDrawBitmap::SkDrawBitmap() : format((SkBitmap::Config) -1), height(-1), 68 rowBytes(0), width(-1), fColor(0), fColorSet(false) { 69 } 70 71 SkDrawBitmap::~SkDrawBitmap() { 72 } 73 74 #ifdef SK_DUMP_ENABLED 75 void SkDrawBitmap::dump(SkAnimateMaker* maker) { 76 dumpBase(maker); 77 dumpAttrs(maker); 78 if (fColorSet) 79 SkDebugf("erase=\"argb(%d,%d,%d,%d)\" ", SkColorGetA(fColor)/255, SkColorGetR(fColor), 80 SkColorGetG(fColor), SkColorGetB(fColor)); 81 if (rowBytes > 0) 82 SkDebugf("rowBytes=\"%d\" ", rowBytes); 83 const char* formatName; 84 switch (format) { 85 case 0: formatName = "none"; break; 86 case 1: formatName = "A1"; break; 87 case 2: formatName = "A8"; break; 88 case 3: formatName = "Index8"; break; 89 case 4: formatName = "RGB16"; break; 90 case 5: formatName = "RGB32"; break; 91 } 92 SkDebugf("format=\"%s\" />\n", formatName); 93 } 94 #endif 95 96 void SkDrawBitmap::onEndElement(SkAnimateMaker& maker) { 97 SkASSERT(format != (SkBitmap::Config) -1); 98 SkASSERT(width != -1); 99 SkASSERT(height != -1); 100 SkASSERT(rowBytes >= 0); 101 fBitmap.setConfig((SkBitmap::Config) format, width, height, rowBytes); 102 fBitmap.allocPixels(); 103 if (fColorSet) 104 fBitmap.eraseColor(fColor); 105 } 106 107 bool SkDrawBitmap::setProperty(int index, SkScriptValue& value) 108 { 109 switch (index) { 110 case SK_PROPERTY(erase): 111 SkASSERT(value.fType == SkType_ARGB); 112 fColor = value.fOperand.fS32; 113 fColorSet = true; 114 break; 115 default: 116 SkASSERT(0); 117 return false; 118 } 119 return true; 120 } 121 122 123 enum SkImage_Properties { 124 SK_PROPERTY(height), 125 SK_PROPERTY(width) 126 }; 127 128 #if SK_USE_CONDENSED_INFO == 0 129 130 const SkMemberInfo SkImage::fInfo[] = { 131 SK_MEMBER_INHERITED, 132 SK_MEMBER(base64, Base64), 133 SK_MEMBER_PROPERTY(height, Int), 134 SK_MEMBER(src, String), 135 SK_MEMBER_PROPERTY(width, Int) 136 }; 137 138 #endif 139 140 DEFINE_GET_MEMBER(SkImage); 141 142 SkImage::SkImage() : fDirty(true), fUriBase(NULL) { 143 base64.fData = NULL; 144 base64.fLength = 0; 145 } 146 147 SkImage::~SkImage() { 148 delete[] base64.fData; 149 } 150 151 SkDisplayable* SkImage::deepCopy(SkAnimateMaker* maker) { 152 SkDisplayable* copy = INHERITED::deepCopy(maker); 153 ((SkImage*) copy)->fUriBase = ((SkImage*) this)->fUriBase; 154 return copy; 155 } 156 157 void SkImage::dirty() { 158 fDirty = true; 159 } 160 161 bool SkImage::draw(SkAnimateMaker& maker) { 162 if (fDirty) 163 resolve(); 164 return INHERITED::draw(maker); 165 } 166 167 bool SkImage::getProperty(int index, SkScriptValue* value) const { 168 if (fDirty) 169 resolve(); 170 switch (index) { 171 case SK_PROPERTY(height): 172 value->fOperand.fS32 = fBitmap.height(); 173 break; 174 case SK_PROPERTY(width): 175 value->fOperand.fS32 = fBitmap.width(); 176 break; 177 default: 178 SkASSERT(0); 179 return false; 180 } 181 value->fType = SkType_Int; 182 return true; 183 } 184 185 void SkImage::onEndElement(SkAnimateMaker& maker) { 186 fUriBase = maker.fPrefix.c_str(); 187 } 188 189 void SkImage::resolve() { 190 fDirty = false; 191 if (base64.fData) { 192 fBitmap.reset(); 193 SkImageDecoder::DecodeMemory(base64.fData, base64.fLength, &fBitmap); 194 } else if (src.size()) { 195 if (fLast.equals(src)) 196 return; 197 fLast.set(src); 198 fBitmap.reset(); 199 200 //SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str()); 201 SkStream* stream = new SkFILEStream(src.c_str()); 202 203 SkAutoTDelete<SkStream> autoDel(stream); 204 SkImageDecoder::DecodeStream(stream, &fBitmap); 205 } 206 } 207