summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLeon Scroggins III <scroggo@google.com>2013-08-20 17:59:39 -0400
committerLeon Scroggins III <scroggo@google.com>2013-08-27 15:28:58 -0400
commitca32021b43f326af7d3f4ae041f8db297f98a518 (patch)
tree95c04e5b699d34181a2fc2f4e866bc2759250fdb /graphics
parentfead1290d74492af5fdf3dcb6739fbd1898dc239 (diff)
downloadframeworks_base-ca32021b43f326af7d3f4ae041f8db297f98a518.zip
frameworks_base-ca32021b43f326af7d3f4ae041f8db297f98a518.tar.gz
frameworks_base-ca32021b43f326af7d3f4ae041f8db297f98a518.tar.bz2
Replace stream wrap-function w/ more specific ones
The current stream wrapper returns a potentially incorrect value for a call to getLength(), is typically copied into another stream (not always in the same way), and doesn't always take advantage of its underlying data (like when it is an Asset). The overall goal of this CL is to provide the caller with something that is ready to use, depending on what is asked for. If a copy is desired, the copy is made before being returned to the caller. core/jni/android/graphics/Bitmap.cpp: Include SkStream.h, since it is no longer included by CreateJavaOutputStreamAdaptor's header file. core/jni/android/graphics/BitmapFactory.cpp: Pass an SkStreamRewindable to decoding functions, as Skia decoders will be updated to only take an SkStreamRewindable (which makes more sense because they require rewinding). Call the more specific GetRewindableStream to get a rewindable stream. Remove copyAssetToStream which has been moved to Utils. In nativeDecodeAsset, pass forcePurgeable as allowPurgeable in doDecode. Technically the old code worked, but it checked the BitmapOptions again. Remove getFDSize, which is no longer used. core/jni/android/graphics/BitmapRegionDecoder.cpp: Remove redundant buildSkMemoryStream. nativeNewInstanceFromStream now calls CopyJavaInputStream, which handles the copy. Copy the Asset directly, using common code, rather than creating an AssetStreamAdaptor to copy. core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp: core/jni/android/graphics/CreateJavaOutputStreamAdaptor.h: Provide new interfaces to access data from a Java InputStream. The new interfaces are more specific about what type of stream is desired. Use forward declarations where possible. Remove doSize, which gives a misleading answer to the question of how long the entire stream is. TODO: Only call FindClass etc once. core/jni/android/graphics/Movie.cpp: Check for an asset stream, and use it if possible. Then call GetRewindableStream if there is not an asset. Remove the memory leak. Call DeleteLocalRef to delete the allocated memory. core/jni/android/graphics/Picture.cpp: Call the new interface. core/jni/android/graphics/Utils.cpp: core/jni/android/graphics/Utils.h: Make AssetStreamAdaptor inherit from SkStreamRewindable so it can be passed to Skia decoding functions once they require it. Add CopyAssetToStream (moved from BitmapFactory.cpp) so it can be used by multiple files. graphics/java/android/graphics/BitmapFactory.java: Remove the call to mark, which is now done natively. Remove the BufferedInputStream. Mark/reset is now handled by native code. Allow decodeStream to handle a FileInputStream by using the FileDescriptor, if it is seekable. In decodeFileDescriptor, call nativeDecodeStream instead of decodeStream so this new functionality will not loop. Call setDensityFromOptions in decodeFileDescriptor. graphics/java/android/graphics/BitmapRegionDecoder.java: Remove the BufferedInputStream. Mark/reset is now handled by native code. TODO: ADD TESTS! Requires https://googleplex-android-review.googlesource.com/#/c/344317/ BUG=https://b.corp.google.com/issue?id=8432093 Change-Id: I4419b70b3482325c98ecc673dbfc4613f1b18581
Diffstat (limited to 'graphics')
-rw-r--r--graphics/java/android/graphics/BitmapFactory.java74
-rw-r--r--graphics/java/android/graphics/BitmapRegionDecoder.java7
2 files changed, 44 insertions, 37 deletions
diff --git a/graphics/java/android/graphics/BitmapFactory.java b/graphics/java/android/graphics/BitmapFactory.java
index 1c426fd..1721bee 100644
--- a/graphics/java/android/graphics/BitmapFactory.java
+++ b/graphics/java/android/graphics/BitmapFactory.java
@@ -23,7 +23,6 @@ import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
-import java.io.BufferedInputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
@@ -545,28 +544,28 @@ public class BitmapFactory {
return null;
}
- Bitmap bm;
+ Bitmap bm = null;
Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
try {
- // we need mark/reset to work properly
- if (!is.markSupported()) {
- is = new BufferedInputStream(is, DECODE_BUFFER_SIZE);
- }
-
- // so we can call reset() if a given codec gives up after reading up to
- // this many bytes. FIXME: need to find out from the codecs what this
- // value should be.
- is.mark(1024);
-
+ boolean decodeGenericStream = true;
if (is instanceof AssetManager.AssetInputStream) {
final int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
bm = nativeDecodeAsset(asset, outPadding, opts);
- } else {
- // pass some temp storage down to the native code. 1024 is made up,
- // but should be large enough to avoid too many small calls back
- // into is.read(...) This number is not related to the value passed
- // to mark(...) above.
+ // Do not follow the normal case.
+ decodeGenericStream = false;
+ } else if (is instanceof FileInputStream) {
+ try {
+ FileDescriptor fd = ((FileInputStream) is).getFD();
+ // decodeFileDescriptor will take care of throwing the IAE and
+ // calling setDensityFromOptions.
+ return decodeFileDescriptor(fd, outPadding, opts);
+ } catch (IOException e) {
+ // Fall through to nativeDecodeStream.
+ }
+ }
+
+ if (decodeGenericStream) {
byte [] tempStorage = null;
if (opts != null) tempStorage = opts.inTempStorage;
if (tempStorage == null) tempStorage = new byte[DECODE_BUFFER_SIZE];
@@ -610,26 +609,41 @@ public class BitmapFactory {
* no bitmap is returned (null) then padding is
* unchanged.
* @param opts null-ok; Options that control downsampling and whether the
- * image should be completely decoded, or just is size returned.
+ * image should be completely decoded, or just its size returned.
* @return the decoded bitmap, or null
*/
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) {
- if (nativeIsSeekable(fd)) {
- Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
+ Bitmap bm;
+
+ Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeFileDescriptor");
+ try {
+ if (nativeIsSeekable(fd)) {
+ bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
+ } else {
+ FileInputStream fis = new FileInputStream(fd);
+ // FIXME: If nativeDecodeStream grabbed the pointer to tempStorage
+ // from Options, this code would not need to be duplicated.
+ byte [] tempStorage = null;
+ if (opts != null) tempStorage = opts.inTempStorage;
+ if (tempStorage == null) tempStorage = new byte[DECODE_BUFFER_SIZE];
+ try {
+ bm = nativeDecodeStream(fis, tempStorage, outPadding, opts);
+ } finally {
+ try {
+ fis.close();
+ } catch (Throwable t) {/* ignore */}
+ }
+ }
+
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
- return bm;
- } else {
- FileInputStream fis = new FileInputStream(fd);
- try {
- return decodeStream(fis, outPadding, opts);
- } finally {
- try {
- fis.close();
- } catch (Throwable t) {/* ignore */}
- }
+
+ setDensityFromOptions(bm, opts);
+ } finally {
+ Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
+ return bm;
}
/**
diff --git a/graphics/java/android/graphics/BitmapRegionDecoder.java b/graphics/java/android/graphics/BitmapRegionDecoder.java
index b38d107..3524b25 100644
--- a/graphics/java/android/graphics/BitmapRegionDecoder.java
+++ b/graphics/java/android/graphics/BitmapRegionDecoder.java
@@ -17,7 +17,6 @@ package android.graphics;
import android.content.res.AssetManager;
-import java.io.BufferedInputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
@@ -108,12 +107,6 @@ public final class BitmapRegionDecoder {
*/
public static BitmapRegionDecoder newInstance(InputStream is,
boolean isShareable) throws IOException {
- // we need mark/reset to work properly in JNI
-
- if (!is.markSupported()) {
- is = new BufferedInputStream(is, 16 * 1024);
- }
-
if (is instanceof AssetManager.AssetInputStream) {
return nativeNewInstance(
((AssetManager.AssetInputStream) is).getAssetInt(),