1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawBitmap.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkAnimateMaker.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkImageDecoder.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStream.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if SK_USE_CONDENSED_INFO == 0
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkMemberInfo SkBaseBitmap::fInfo[] = {
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(x, Float),
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(y, Float)
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comDEFINE_GET_MEMBER(SkBaseBitmap);
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkBaseBitmap::SkBaseBitmap() : x(0), y(0) {
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkBaseBitmap::~SkBaseBitmap() {
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkBaseBitmap::draw(SkAnimateMaker& maker) {
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBoundableAuto boundable(this, maker);
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    maker.fCanvas->drawBitmap(fBitmap, x, y, maker.fPaint);
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum SkDrawBitmap_Properties {
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_PROPERTY(erase)
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if SK_USE_CONDENSED_INFO == 0
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst SkMemberInfo SkDrawBitmap::fInfo[] = {
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_INHERITED,
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_PROPERTY(erase, ARGB),
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(format, BitmapFormat),
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(height, Int),
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(rowBytes, Int),
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(width, Int),
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comDEFINE_GET_MEMBER(SkDrawBitmap);
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
59c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreedSkDrawBitmap::SkDrawBitmap() : format((SkColorType) -1), height(-1),
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    rowBytes(0),    width(-1), fColor(0), fColorSet(false) {
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkDrawBitmap::~SkDrawBitmap() {
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DUMP_ENABLED
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkDrawBitmap::dump(SkAnimateMaker* maker) {
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dumpBase(maker);
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dumpAttrs(maker);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fColorSet)
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDebugf("erase=\"argb(%d,%d,%d,%d)\" ", SkColorGetA(fColor)/255, SkColorGetR(fColor),
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkColorGetG(fColor), SkColorGetB(fColor));
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (rowBytes > 0)
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDebugf("rowBytes=\"%d\" ", rowBytes);
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* formatName;
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (format) {
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case 0: formatName = "none"; break;
78d6bab0238655dbab24dfe92bd0b16b464310a8c7rmistry@google.com        case 1: formatName = "A8"; break;
79d6bab0238655dbab24dfe92bd0b16b464310a8c7rmistry@google.com        case 2: formatName = "Index8"; break;
80d6bab0238655dbab24dfe92bd0b16b464310a8c7rmistry@google.com        case 3: formatName = "RGB16"; break;
81d6bab0238655dbab24dfe92bd0b16b464310a8c7rmistry@google.com        case 4: formatName = "RGB32"; break;
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDebugf("format=\"%s\" />\n", formatName);
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8793c7ee34dc5c8f6bfad65809f4b39f8d00d7f0d4sugoi@google.comvoid SkDrawBitmap::onEndElement(SkAnimateMaker&) {
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(width != -1);
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(height != -1);
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(rowBytes >= 0);
91c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed    SkColorType colorType = SkColorType(format);
92bae704b050491a8a98c67cb23eaccb10852d2bd5reed    fBitmap.allocPixels(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType),
93bae704b050491a8a98c67cb23eaccb10852d2bd5reed                        rowBytes);
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fColorSet)
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBitmap.eraseColor(fColor);
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkDrawBitmap::setProperty(int index, SkScriptValue& value)
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (index) {
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SK_PROPERTY(erase):
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(value.fType == SkType_ARGB);
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fColor = value.fOperand.fS32;
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fColorSet = true;
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(0);
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return false;
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
114bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comenum SkImageBaseBitmap_Properties {
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_PROPERTY(height),
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_PROPERTY(width)
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if SK_USE_CONDENSED_INFO == 0
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
121bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comconst SkMemberInfo SkImageBaseBitmap::fInfo[] = {
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_INHERITED,
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(base64, Base64),
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_PROPERTY(height, Int),
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER(src, String),
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SK_MEMBER_PROPERTY(width, Int)
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
131bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comDEFINE_GET_MEMBER(SkImageBaseBitmap);
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
133bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comSkImageBaseBitmap::SkImageBaseBitmap() : fDirty(true), fUriBase(NULL) {
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    base64.fData = NULL;
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    base64.fLength = 0;
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comSkImageBaseBitmap::~SkImageBaseBitmap() {
139d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    delete[] base64.fData;
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
142bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comSkDisplayable* SkImageBaseBitmap::deepCopy(SkAnimateMaker* maker) {
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDisplayable* copy = INHERITED::deepCopy(maker);
144bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.com    ((SkImageBaseBitmap*) copy)->fUriBase = ((SkImageBaseBitmap*) this)->fUriBase;
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return copy;
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
148bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comvoid SkImageBaseBitmap::dirty() {
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDirty = true;
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
152bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.combool SkImageBaseBitmap::draw(SkAnimateMaker& maker) {
153d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fDirty)
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        resolve();
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return INHERITED::draw(maker);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.combool SkImageBaseBitmap::getProperty(int index, SkScriptValue* value) const {
159d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    if (fDirty)
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        resolve();
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (index) {
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SK_PROPERTY(height):
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            value->fOperand.fS32 = fBitmap.height();
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case SK_PROPERTY(width):
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            value->fOperand.fS32 = fBitmap.width();
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(0);
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    value->fType = SkType_Int;
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return true;
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
176bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comvoid SkImageBaseBitmap::onEndElement(SkAnimateMaker& maker) {
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fUriBase = maker.fPrefix.c_str();
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
180bdae027bd86b7900d1874ad77f0ec9e31f6cbcabreed@google.comvoid SkImageBaseBitmap::resolve() {
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDirty = false;
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (base64.fData) {
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBitmap.reset();
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkImageDecoder::DecodeMemory(base64.fData, base64.fLength, &fBitmap);
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else if (src.size()) {
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fLast.equals(src))
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return;
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fLast.set(src);
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBitmap.reset();
190d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        //SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str());
192a1193e4b0e34a7e4e1bd33e9708d7341679f8321scroggo        SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(src.c_str()));
193f381162e5616daacdbcc06d693aca5111aeeebe8mike@reedtribe.org        if (stream.get()) {
194f381162e5616daacdbcc06d693aca5111aeeebe8mike@reedtribe.org            SkImageDecoder::DecodeStream(stream, &fBitmap);
195f381162e5616daacdbcc06d693aca5111aeeebe8mike@reedtribe.org        }
196d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
198