michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkDrawBitmap.h" michael@0: #include "SkAnimateMaker.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkImageDecoder.h" michael@0: #include "SkPaint.h" michael@0: #include "SkStream.h" michael@0: michael@0: #if SK_USE_CONDENSED_INFO == 0 michael@0: michael@0: const SkMemberInfo SkBaseBitmap::fInfo[] = { michael@0: SK_MEMBER(x, Float), michael@0: SK_MEMBER(y, Float) michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: DEFINE_GET_MEMBER(SkBaseBitmap); michael@0: michael@0: SkBaseBitmap::SkBaseBitmap() : x(0), y(0) { michael@0: } michael@0: michael@0: SkBaseBitmap::~SkBaseBitmap() { michael@0: } michael@0: michael@0: bool SkBaseBitmap::draw(SkAnimateMaker& maker) { michael@0: SkBoundableAuto boundable(this, maker); michael@0: maker.fCanvas->drawBitmap(fBitmap, x, y, maker.fPaint); michael@0: return false; michael@0: } michael@0: michael@0: enum SkDrawBitmap_Properties { michael@0: SK_PROPERTY(erase) michael@0: }; michael@0: michael@0: #if SK_USE_CONDENSED_INFO == 0 michael@0: michael@0: const SkMemberInfo SkDrawBitmap::fInfo[] = { michael@0: SK_MEMBER_INHERITED, michael@0: SK_MEMBER_PROPERTY(erase, ARGB), michael@0: SK_MEMBER(format, BitmapFormat), michael@0: SK_MEMBER(height, Int), michael@0: SK_MEMBER(rowBytes, Int), michael@0: SK_MEMBER(width, Int), michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: DEFINE_GET_MEMBER(SkDrawBitmap); michael@0: michael@0: SkDrawBitmap::SkDrawBitmap() : format((SkBitmap::Config) -1), height(-1), michael@0: rowBytes(0), width(-1), fColor(0), fColorSet(false) { michael@0: } michael@0: michael@0: SkDrawBitmap::~SkDrawBitmap() { michael@0: } michael@0: michael@0: #ifdef SK_DUMP_ENABLED michael@0: void SkDrawBitmap::dump(SkAnimateMaker* maker) { michael@0: dumpBase(maker); michael@0: dumpAttrs(maker); michael@0: if (fColorSet) michael@0: SkDebugf("erase=\"argb(%d,%d,%d,%d)\" ", SkColorGetA(fColor)/255, SkColorGetR(fColor), michael@0: SkColorGetG(fColor), SkColorGetB(fColor)); michael@0: if (rowBytes > 0) michael@0: SkDebugf("rowBytes=\"%d\" ", rowBytes); michael@0: const char* formatName; michael@0: switch (format) { michael@0: case 0: formatName = "none"; break; michael@0: case 1: formatName = "A8"; break; michael@0: case 2: formatName = "Index8"; break; michael@0: case 3: formatName = "RGB16"; break; michael@0: case 4: formatName = "RGB32"; break; michael@0: } michael@0: SkDebugf("format=\"%s\" />\n", formatName); michael@0: } michael@0: #endif michael@0: michael@0: void SkDrawBitmap::onEndElement(SkAnimateMaker&) { michael@0: SkASSERT(width != -1); michael@0: SkASSERT(height != -1); michael@0: SkASSERT(rowBytes >= 0); michael@0: fBitmap.setConfig((SkBitmap::Config) format, width, height, rowBytes); michael@0: fBitmap.allocPixels(); michael@0: if (fColorSet) michael@0: fBitmap.eraseColor(fColor); michael@0: } michael@0: michael@0: bool SkDrawBitmap::setProperty(int index, SkScriptValue& value) michael@0: { michael@0: switch (index) { michael@0: case SK_PROPERTY(erase): michael@0: SkASSERT(value.fType == SkType_ARGB); michael@0: fColor = value.fOperand.fS32; michael@0: fColorSet = true; michael@0: break; michael@0: default: michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: michael@0: enum SkImageBaseBitmap_Properties { michael@0: SK_PROPERTY(height), michael@0: SK_PROPERTY(width) michael@0: }; michael@0: michael@0: #if SK_USE_CONDENSED_INFO == 0 michael@0: michael@0: const SkMemberInfo SkImageBaseBitmap::fInfo[] = { michael@0: SK_MEMBER_INHERITED, michael@0: SK_MEMBER(base64, Base64), michael@0: SK_MEMBER_PROPERTY(height, Int), michael@0: SK_MEMBER(src, String), michael@0: SK_MEMBER_PROPERTY(width, Int) michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: DEFINE_GET_MEMBER(SkImageBaseBitmap); michael@0: michael@0: SkImageBaseBitmap::SkImageBaseBitmap() : fDirty(true), fUriBase(NULL) { michael@0: base64.fData = NULL; michael@0: base64.fLength = 0; michael@0: } michael@0: michael@0: SkImageBaseBitmap::~SkImageBaseBitmap() { michael@0: delete[] base64.fData; michael@0: } michael@0: michael@0: SkDisplayable* SkImageBaseBitmap::deepCopy(SkAnimateMaker* maker) { michael@0: SkDisplayable* copy = INHERITED::deepCopy(maker); michael@0: ((SkImageBaseBitmap*) copy)->fUriBase = ((SkImageBaseBitmap*) this)->fUriBase; michael@0: return copy; michael@0: } michael@0: michael@0: void SkImageBaseBitmap::dirty() { michael@0: fDirty = true; michael@0: } michael@0: michael@0: bool SkImageBaseBitmap::draw(SkAnimateMaker& maker) { michael@0: if (fDirty) michael@0: resolve(); michael@0: return INHERITED::draw(maker); michael@0: } michael@0: michael@0: bool SkImageBaseBitmap::getProperty(int index, SkScriptValue* value) const { michael@0: if (fDirty) michael@0: resolve(); michael@0: switch (index) { michael@0: case SK_PROPERTY(height): michael@0: value->fOperand.fS32 = fBitmap.height(); michael@0: break; michael@0: case SK_PROPERTY(width): michael@0: value->fOperand.fS32 = fBitmap.width(); michael@0: break; michael@0: default: michael@0: SkASSERT(0); michael@0: return false; michael@0: } michael@0: value->fType = SkType_Int; michael@0: return true; michael@0: } michael@0: michael@0: void SkImageBaseBitmap::onEndElement(SkAnimateMaker& maker) { michael@0: fUriBase = maker.fPrefix.c_str(); michael@0: } michael@0: michael@0: void SkImageBaseBitmap::resolve() { michael@0: fDirty = false; michael@0: if (base64.fData) { michael@0: fBitmap.reset(); michael@0: SkImageDecoder::DecodeMemory(base64.fData, base64.fLength, &fBitmap); michael@0: } else if (src.size()) { michael@0: if (fLast.equals(src)) michael@0: return; michael@0: fLast.set(src); michael@0: fBitmap.reset(); michael@0: michael@0: //SkStream* stream = SkStream::GetURIStream(fUriBase, src.c_str()); michael@0: SkAutoTUnref stream(SkStream::NewFromFile(src.c_str())); michael@0: if (stream.get()) { michael@0: SkImageDecoder::DecodeStream(stream, &fBitmap); michael@0: } michael@0: } michael@0: }