Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(528)

Unified Diff: src/images/SkImageDecoder.cpp

Issue 14772035: Make DecodeMemoryToTarget handle more configs. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/images/SkImageDecoder.cpp
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp
index bcea23bd59aa0c755842aca07516bda879c0154e..52c43c83f17e8616397bb34c5875a6c14f8226f5 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);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698