aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-20 16:58:16 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-20 16:58:16 +0000
commit67882ddc0efbe33070b5da16931f80ed7f21b1f1 (patch)
tree91950614c3795d6952f373a2f8633ba0c9755360 /src/images
parent1dd9baa6c8faeb4ce837c39d179ce9c9a09719ef (diff)
Make DecodeMemoryToTarget handle more configs.
In SkImageDecoder::DecodeMemoryToTarget, if SkBitmapToImageInfo fails due to an unsupported config, copy to 8888. Needed for https://codereview.chromium.org/15145004/ and https://codereview.chromium.org/12851012/ for all bitmaps to be decoded properly in render_pictures and bench_pictures. R=reed@google.com Review URL: https://codereview.chromium.org/14772035 git-svn-id: http://skia.googlecode.com/svn/trunk@9196 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/images')
-rw-r--r--src/images/SkImageDecoder.cpp143
1 files changed, 114 insertions, 29 deletions
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp
index bcea23bd59..52c43c83f1 100644
--- a/src/images/SkImageDecoder.cpp
+++ b/src/images/SkImageDecoder.cpp
@@ -280,14 +280,26 @@ bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
}
-class TargetAllocator : public SkBitmap::Allocator {
+/**
+ * Special allocator used by DecodeMemoryToTarget. Uses preallocated memory
+ * provided if the bm is 8888. Otherwise, uses a heap allocator. The same
+ * allocator will be used again for a copy to 8888, when the preallocated
+ * memory will be used.
+ */
+class TargetAllocator : public SkBitmap::HeapAllocator {
public:
TargetAllocator(void* target)
: fTarget(target) {}
virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE {
- // SkColorTable is not supported by Info/Target model.
+ // If the config is not 8888, allocate a pixelref using the heap.
+ // fTarget will be used to store the final pixels when copied to
+ // 8888.
+ if (bm->config() != SkBitmap::kARGB_8888_Config) {
+ return INHERITED::allocPixelRef(bm, ct);
+ }
+ // In kARGB_8888_Config, there is no colortable.
SkASSERT(NULL == ct);
bm->setPixels(fTarget);
return true;
@@ -295,51 +307,124 @@ public:
private:
void* fTarget;
+ typedef SkBitmap::HeapAllocator INHERITED;
};
+/**
+ * Helper function for DecodeMemoryToTarget. DecodeMemoryToTarget wants
+ * 8888, so set the config to it. All parameters must not be null.
+ * @param decoder Decoder appropriate for this stream.
+ * @param stream Rewound stream to the encoded data.
+ * @param bitmap On success, will have its bounds set to the bounds of the
+ * encoded data, and its config set to 8888.
+ * @return True if the bounds were decoded and the bitmap is 8888 or can be
+ * copied to 8888.
+ */
+static bool decode_bounds_to_8888(SkImageDecoder* decoder, SkStream* stream,
+ SkBitmap* bitmap) {
+ SkASSERT(decoder != NULL);
+ SkASSERT(stream != NULL);
+ SkASSERT(bitmap != NULL);
+
+ if (!decoder->decode(stream, bitmap, SkImageDecoder::kDecodeBounds_Mode)) {
+ return false;
+ }
+
+ if (bitmap->config() == SkBitmap::kARGB_8888_Config) {
+ return true;
+ }
+
+ if (!bitmap->canCopyTo(SkBitmap::kARGB_8888_Config)) {
+ return false;
+ }
+
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, bitmap->width(), bitmap->height());
+ return true;
+}
+
+/**
+ * Helper function for DecodeMemoryToTarget. Decodes the stream into bitmap, and if
+ * the bitmap is not 8888, then it is copied to 8888. Either way, the end result has
+ * its pixels stored in target. All parameters must not be null.
+ * @param decoder Decoder appropriate for this stream.
+ * @param stream Rewound stream to the encoded data.
+ * @param bitmap On success, will contain the decoded image, with its pixels stored
+ * at target.
+ * @param target Preallocated memory for storing pixels.
+ * @return bool Whether the decode (and copy, if necessary) succeeded.
+ */
+static bool decode_pixels_to_8888(SkImageDecoder* decoder, SkStream* stream,
+ SkBitmap* bitmap, void* target) {
+ SkASSERT(decoder != NULL);
+ SkASSERT(stream != NULL);
+ SkASSERT(bitmap != NULL);
+ SkASSERT(target != NULL);
+
+ TargetAllocator allocator(target);
+ decoder->setAllocator(&allocator);
+
+ bool success = decoder->decode(stream, bitmap, SkImageDecoder::kDecodePixels_Mode);
+ decoder->setAllocator(NULL);
+
+ if (!success) {
+ return false;
+ }
+
+ if (bitmap->config() == SkBitmap::kARGB_8888_Config) {
+ return true;
+ }
+
+ SkBitmap bm8888;
+ if (!bitmap->copyTo(&bm8888, SkBitmap::kARGB_8888_Config, &allocator)) {
+ return false;
+ }
+
+ bitmap->swap(bm8888);
+ return true;
+}
+
bool SkImageDecoder::DecodeMemoryToTarget(const void* buffer, size_t size,
SkImage::Info* info,
const SkBitmapFactory::Target* target) {
if (NULL == info) {
return false;
}
+
// FIXME: Just to get this working, implement in terms of existing
// ImageDecoder calls.
SkBitmap bm;
SkMemoryStream stream(buffer, size);
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
- if (decoder.get() != NULL && decoder->decode(&stream, &bm, kDecodeBounds_Mode)) {
- // Now set info properly
- if (!SkBitmapToImageInfo(bm, info)) {
- return false;
- }
+ if (NULL == decoder.get()) {
+ return false;
+ }
- // SkBitmapToImageInfo will return false if Index8 is used. kIndex8
- // is not supported by the Info/Target model, since kIndex8 requires
- // an SkColorTable, which this model does not keep track of.
- SkASSERT(bm.config() != SkBitmap::kIndex8_Config);
+ if (!decode_bounds_to_8888(decoder.get(), &stream, &bm)) {
+ return false;
+ }
- if (NULL == target) {
- return true;
- }
+ SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
- if (target->fRowBytes != SkToU32(bm.rowBytes())) {
- if (target->fRowBytes < SkImageMinRowBytes(*info)) {
- SkASSERT(!"Desired row bytes is too small");
- return false;
- }
- bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes);
- }
+ // Now set info properly.
+ // Since Config is SkBitmap::kARGB_8888_Config, SkBitmapToImageInfo
+ // will always succeed.
+ SkAssertResult(SkBitmapToImageInfo(bm, info));
- TargetAllocator allocator(target->fAddr);
- decoder->setAllocator(&allocator);
- stream.rewind();
- bool success = decoder->decode(&stream, &bm, kDecodePixels_Mode);
- // Remove the allocator, since it's on the stack.
- decoder->setAllocator(NULL);
- return success;
+ if (NULL == target) {
+ return true;
}
- return false;
+
+ if (target->fRowBytes != SkToU32(bm.rowBytes())) {
+ if (target->fRowBytes < SkImageMinRowBytes(*info)) {
+ SkASSERT(!"Desired row bytes is too small");
+ return false;
+ }
+ bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes);
+ }
+
+ // SkMemoryStream.rewind() will always return true.
+ SkAssertResult(stream.rewind());
+ return decode_pixels_to_8888(decoder.get(), &stream, &bm, target->fAddr);
}