summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/graphics/android/ImageAndroid.cpp
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2008-12-17 18:05:15 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2008-12-17 18:05:15 -0800
commit1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353 (patch)
tree4457a7306ea5acb43fe05bfe0973b1f7faf97ba2 /WebCore/platform/graphics/android/ImageAndroid.cpp
parent9364f22aed35e1a1e9d07c121510f80be3ab0502 (diff)
downloadexternal_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.zip
external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.gz
external_webkit-1cbdecfa9fc428ac2d8aca0fa91c9580b3d57353.tar.bz2
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'WebCore/platform/graphics/android/ImageAndroid.cpp')
-rw-r--r--WebCore/platform/graphics/android/ImageAndroid.cpp182
1 files changed, 112 insertions, 70 deletions
diff --git a/WebCore/platform/graphics/android/ImageAndroid.cpp b/WebCore/platform/graphics/android/ImageAndroid.cpp
index 00145af..a54f440 100644
--- a/WebCore/platform/graphics/android/ImageAndroid.cpp
+++ b/WebCore/platform/graphics/android/ImageAndroid.cpp
@@ -37,17 +37,27 @@
#include "SkBitmapRef.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
+#include "SkImageDecoder.h"
#include "SkShader.h"
#include "SkString.h"
+#include "SkTemplates.h"
#include <utils/AssetManager.h>
//#define TRACE_SUBSAMPLED_BITMAPS
+//#define TRACE_SKIPPED_BITMAPS
-android::AssetManager* gGlobalAssetMgr;
+android::AssetManager* globalAssetManager() {
+ static android::AssetManager* gGlobalAssetMgr;
+ if (!gGlobalAssetMgr) {
+ gGlobalAssetMgr = new android::AssetManager();
+ gGlobalAssetMgr->addDefaultAssets();
+ }
+ return gGlobalAssetMgr;
+}
namespace WebCore {
-
+
void FrameData::clear()
{
if (m_frame) {
@@ -58,63 +68,83 @@ void FrameData::clear()
}
}
-SkBitmapRef* BitmapImage::getBitmap()
+BitmapImage::BitmapImage(SkBitmapRef* ref, ImageObserver* observer)
+ : Image(observer)
+ , m_currentFrame(0)
+ , m_frames(0)
+ , m_frameTimer(0)
+ , m_repetitionCount(0)
+ , m_repetitionsComplete(0)
+ , m_isSolidColor(false)
+ , m_animationFinished(true)
+ , m_allDataReceived(true)
+ , m_haveSize(true)
+ , m_sizeAvailable(true)
+ , m_decodedSize(0)
+ , m_haveFrameCount(true)
+ , m_frameCount(1)
{
- return m_bitmapRef;
+ initPlatformData();
+
+ m_size = IntSize(ref->bitmap().width(), ref->bitmap().height());
+
+ m_frames.grow(1);
+ m_frames[0].m_frame = ref;
+ m_frames[0].m_hasAlpha = !ref->bitmap().isOpaque();
+ checkForSolidColor();
+ ref->ref();
}
+
void BitmapImage::initPlatformData()
{
- m_bitmapRef = NULL;
m_source.clearURL();
}
void BitmapImage::invalidatePlatformData()
{
- if (m_bitmapRef) {
- m_bitmapRef->unref();
- m_bitmapRef = NULL;
- }
}
void BitmapImage::checkForSolidColor()
{
m_isSolidColor = false;
- if (this->frameCount() > 1) {
- if (!m_bitmapRef) {
- return;
+ if (frameCount() == 1) {
+ SkBitmapRef* ref = frameAtIndex(0);
+ if (!ref) {
+ return; // keep solid == false
}
- const SkBitmap& bm = m_bitmapRef->bitmap();
+ const SkBitmap& bm = ref->bitmap();
+ if (bm.width() != 1 || bm.height() != 1) {
+ return; // keep solid == false
+ }
- if (bm.width() == 1 && bm.height() == 1) {
- SkAutoLockPixels alp(bm);
- if (bm.getPixels() == NULL) {
- return;
- }
+ SkAutoLockPixels alp(bm);
+ if (!bm.readyToDraw()) {
+ return; // keep solid == false
+ }
- SkPMColor color;
- switch (bm.getConfig()) {
- case SkBitmap::kARGB_8888_Config:
- color = *bm.getAddr32(0, 0);
- break;
- case SkBitmap::kRGB_565_Config:
- color = SkPixel16ToPixel32(*bm.getAddr16(0, 0));
- break;
- case SkBitmap::kIndex8_Config: {
- SkColorTable* ctable = bm.getColorTable();
- if (!ctable) {
- return;
- }
- color = (*ctable)[*bm.getAddr8(0, 0)];
- break;
- }
- default: // don't check other configs
- return;
+ SkPMColor color;
+ switch (bm.getConfig()) {
+ case SkBitmap::kARGB_8888_Config:
+ color = *bm.getAddr32(0, 0);
+ break;
+ case SkBitmap::kRGB_565_Config:
+ color = SkPixel16ToPixel32(*bm.getAddr16(0, 0));
+ break;
+ case SkBitmap::kIndex8_Config: {
+ SkColorTable* ctable = bm.getColorTable();
+ if (!ctable) {
+ return;
+ }
+ color = (*ctable)[*bm.getAddr8(0, 0)];
+ break;
}
- m_isSolidColor = true;
- m_solidColor = android_SkPMColorToWebCoreColor(color);
+ default:
+ return; // keep solid == false
}
+ m_isSolidColor = true;
+ m_solidColor = android_SkPMColorToWebCoreColor(color);
}
}
@@ -125,36 +155,46 @@ void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect,
if (!image) { // If it's too early we won't have an image yet.
return;
}
-
+
// in case we get called with an incomplete bitmap
const SkBitmap& bitmap = image->bitmap();
if (bitmap.getPixels() == NULL && bitmap.pixelRef() == NULL) {
+#ifdef TRACE_SKIPPED_BITMAPS
+ SkDebugf("----- skip bitmapimage: [%d %d] pixels %p pixelref %p\n",
+ bitmap.width(), bitmap.height(),
+ bitmap.getPixels(), bitmap.pixelRef());
+#endif
return;
}
-
+
SkIRect srcR;
SkRect dstR;
float invScaleX = (float)bitmap.width() / image->origWidth();
float invScaleY = (float)bitmap.height() / image->origHeight();
-
+
android_setrect(&dstR, dstRect);
android_setrect_scaled(&srcR, srcRect, invScaleX, invScaleY);
if (srcR.isEmpty() || dstR.isEmpty()) {
+#ifdef TRACE_SKIPPED_BITMAPS
+ SkDebugf("----- skip bitmapimage: [%d %d] src-empty %d dst-empty %d\n",
+ bitmap.width(), bitmap.height(),
+ srcR.isEmpty(), dstR.isEmpty());
+#endif
return;
}
SkCanvas* canvas = ctxt->platformContext()->mCanvas;
SkPaint paint;
-
+
paint.setFilterBitmap(true);
paint.setPorterDuffXfermode(android_convert_compositeOp(compositeOp));
canvas->drawBitmapRect(bitmap, &srcR, dstR, &paint);
-
+
startAnimation();
-
+
#ifdef TRACE_SUBSAMPLED_BITMAPS
if (bitmap.width() != image->origWidth() ||
- bitmap.height() != image->origHeight()) {
+ bitmap.height() != image->origHeight()) {
SkDebugf("--- BitmapImage::draw [%d %d] orig [%d %d]\n",
bitmap.width(), bitmap.height(),
image->origWidth(), image->origHeight());
@@ -184,16 +224,16 @@ void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect,
if (bitmap.getPixels() == NULL && bitmap.pixelRef() == NULL) {
return;
}
-
+
SkRect dstR;
android_setrect(&dstR, destRect);
if (dstR.isEmpty()) {
return;
}
-
+
SkCanvas* canvas = ctxt->platformContext()->mCanvas;
SkPaint paint;
-
+
SkShader* shader = SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
@@ -201,21 +241,21 @@ void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect,
// now paint is the only owner of shader
paint.setPorterDuffXfermode(android_convert_compositeOp(compositeOp));
paint.setFilterBitmap(true);
-
+
SkMatrix matrix(patternTransform);
-
+
float scaleX = (float)image->origWidth() / bitmap.width();
float scaleY = (float)image->origHeight() / bitmap.height();
matrix.preScale(SkFloatToScalar(scaleX), SkFloatToScalar(scaleY));
-
+
matrix.postTranslate(SkFloatToScalar(phase.x()),
SkFloatToScalar(phase.y()));
shader->setLocalMatrix(matrix);
canvas->drawRect(dstR, paint);
-
+
#ifdef TRACE_SUBSAMPLED_BITMAPS
if (bitmap.width() != image->origWidth() ||
- bitmap.height() != image->origHeight()) {
+ bitmap.height() != image->origHeight()) {
SkDebugf("--- Image::drawPattern [%d %d] orig [%d %d] dst [%g %g]\n",
bitmap.width(), bitmap.height(),
image->origWidth(), image->origHeight(),
@@ -225,30 +265,32 @@ void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect,
}
// missingImage, textAreaResizeCorner
-Image* Image::loadPlatformResource(const char *name)
+PassRefPtr<Image> Image::loadPlatformResource(const char *name)
{
- if (NULL == gGlobalAssetMgr) {
- gGlobalAssetMgr = new android::AssetManager();
- gGlobalAssetMgr->addDefaultAssets();
- }
-
+ android::AssetManager* am = globalAssetManager();
+
SkString path("webkit/");
path.append(name);
path.append(".png");
-
- android::Asset* a = gGlobalAssetMgr->open(path.c_str(),
- android::Asset::ACCESS_BUFFER);
+
+ android::Asset* a = am->open(path.c_str(),
+ android::Asset::ACCESS_BUFFER);
if (a == NULL) {
SkDebugf("---------------- failed to open image asset %s\n", name);
return NULL;
}
-
- Image* image = new BitmapImage;
- RefPtr<SharedBuffer> buffer =
- new SharedBuffer((const char*)a->getBuffer(false), a->getLength());
- image->setData(buffer, true);
- delete a;
- return image;
+
+ SkAutoTDelete<android::Asset> ad(a);
+
+ SkBitmap bm;
+ if (SkImageDecoder::DecodeMemory(a->getBuffer(false), a->getLength(), &bm)) {
+ SkBitmapRef* ref = new SkBitmapRef(bm);
+ // create will call ref(), so we need aur() to release ours upon return
+ SkAutoUnref aur(ref);
+ return BitmapImage::create(ref, 0);
+ }
+ return Image::nullImage();
}
-}
+} // namespace
+