Commit Graph

24 Commits

Author SHA1 Message Date
Leon Scroggins III
2f1b06b58f Reland "Use ImageDecoder for NinePatchDrawable and BitmapDrawable"
This reverts commit ac9c8f7c1e.

Bug: 73083791
Bug: 73364985

Don't scale NinePatchDrawable if bitmap has no density (including fix
for 73364985).
Fixed bugs around density and input streams.

Update PointerIcon to account for the fact that BitmapDrawable no longer
scales its Bitmap up at decode time. PointerIcon now handles the
scaling. This is necessary because PointerIcon never draws its Bitmap.
Instead, native code uses the Bitmap's internal SkBitmap without
accounting for density.

Test: Ran CTS:
- CtsUiRenderingTestCases
- CtsGraphicsTestCases
  - I2d3976061d164ab4d58209db1320917f272a1958
- CtsViewTestCases
- ThemeHostTests

Change-Id: I3e0c11195622d65f084ce79dad887504630177ca
2018-02-15 14:14:25 -05:00
Leon Scroggins III
ac9c8f7c1e Revert "Use ImageDecoder for NinePatchDrawable and BitmapDrawable"
Test: CTS tests

This reverts commit 5aec67ca1b.

Reason for revert: b/73166619

Bug: 73166619
Change-Id: Ic8b2a83abf5567119c8b6af1ec00445a5a6a9cc2
2018-02-12 12:36:49 -05:00
TreeHugger Robot
25d7f46aa9 Merge "Revert "Call ImageDecoder directly in ResourcesImpl"" 2018-02-09 03:47:26 +00:00
Leon Scroggins
0f05b4895d Revert "Call ImageDecoder directly in ResourcesImpl"
This reverts commit 40c59fdef0.

Reason for revert: b/73083791

Change-Id: I044e534be07c5fd18c99dcc65a14b72eaf1fecd5
2018-02-08 21:14:32 +00:00
TreeHugger Robot
bc38a217b9 Merge "Call ImageDecoder directly in ResourcesImpl" 2018-02-06 18:34:40 +00:00
Leon Scroggins III
40c59fdef0 Call ImageDecoder directly in ResourcesImpl
Test: Existing tests

Add a new (hidden) ImageDecoder.Source that accepts an AssetInputStream.
This allows us to create an AnimatedImageDrawable without fear of the
client closing the stream.

Call it from ResourcesImpl instead of Drawable.createFromResourceStream.

Change-Id: I07e00ca60c97538335a6310e830b73211fd8e7bb
2018-02-06 10:21:36 -05:00
TreeHugger Robot
fdd755df14 Merge "Use ImageDecoder in ImageView.getDrawableFromUri" 2018-02-02 19:51:41 +00:00
Stan Iliev
5aec67ca1b Use ImageDecoder for NinePatchDrawable and BitmapDrawable
Don't scale NinePatchDrawable if bitmap has no density.
Fixed bugs around density and input streams.

Update PointerIcon to account for the fact that BitmapDrawable no longer
scales its Bitmap up at decode time. PointerIcon now handles the
scaling. This is necessary because PointerIcon never draws its Bitmap.
Instead, native code uses the Bitmap's internal SkBitmap without
accounting for density.

Test: Ran CTS:
- CtsUiRenderingTestCases
- CtsGraphicsTestCases
- CtsViewTestCases

Change-Id: I030b4bb89c66b0102ccea2d85f5271197558d14e
2018-02-02 10:26:57 -05:00
Leon Scroggins III
046a99ebbb Use ImageDecoder in ImageView.getDrawableFromUri
Bug: 63909536
Test: Existing CTS tests

ImageDecoder will bypass the InputStream if possible, allowing it to be
more efficient. In addition, it handles density scaling differently;
instead of using more RAM to scale the image up, it results in scaling
at draw time.

Change-Id: Ied7c0865a736f9ef0de367299264e18ccc3e0b92
2018-01-31 15:43:49 -05:00
Leon Scroggins
fdb54e3a0d Merge "Do not close InputStream from InputStreamSource" 2018-01-29 11:24:40 +00:00
Leon Scroggins III
0b49f5725c Do not close InputStream from InputStreamSource
Test: I28f82285c0341aff7192eb0157e0de4b97cda577

This is called by public methods that pass along an InputStream. As
such, it is possible that the client was planning to continue reading
from the InputStream, so do not close it.

Change-Id: Iaa53c44d578c1311315616c8fd931bed40290a92
2018-01-29 01:49:13 +00:00
Leon Scroggins
c4533b7085 Merge "Expose AnimatedImageDrawable" 2018-01-26 11:16:04 +00:00
Leon Scroggins III
127d31a684 Expose AnimatedImageDrawable
Bug: 63908092
Test: I85979ae3d8c6a6dae6e4299dc3be291e12024290

Implement Animatable2, adding listeners for starting and ending the
animation.

Add setLoopCount for changing the loop count.

Add the ability to inflate from XML, by using the name of the class or
"animated-image", which mimics "nine-patch", "bitmap" etc.

Move internal variables to a State class so that they can be transferred
to a default constructed AnimatedImageDrawable.

Change-Id: Ice8149e7de55f7ffb4b4ba9dd9c856582fc42bc9
2018-01-24 19:20:18 -05:00
Derek Sollenberger
9f074eeee6 Fix bug in ImageDecoder which provided the incorrect density to NinePatchDrawables
Bug: 72381918
Test: ThemeHostTests
Change-Id: I7679d4a8d9c3495fd7b24536f54d23bfef007f26
2018-01-24 17:35:14 +00:00
Leon Scroggins III
e5de9aa4a9 Unhide ImageDecoder
ImageDecoder is a new way to decode Bitmaps (or Drawables) that
streamlines common use cases of BitmapFactory.

Bug: 63909536
Bug: 63908092
Test: I0f36ce34c968fd7fae4d8edebabea3a421859615
      Ib2877276da8464b5f3eef0bbb848de202c90e97e
      I8d1672180d8325ae1caf44f0bbf41036b94e6253
      I405ebc86f7b6b019e0f040f1d4afe2e9d4354e5d
      Iaeb7c27bafb351932f0fabe59461ef50b1e2424a

Change-Id: Iee236ac73e0bc37ef6903a8150c0d2c84e5cf906
2018-01-22 21:41:28 -05:00
Derek Sollenberger
66c6d78908 Use ImageDecoder for BitmapDrawable
If required due to density mismatches the ImageDecoder will not
upscale the image at decode time, but instead will decode at the
bitmaps default size and upscale at draw time.  This can be a
significant memory savings for low-dpi assets being used on a high-dpi
device.

However, if the provided asset has a higher dpi level than the bitmaps
default density then we will match BitmapFactories behavior and downscale
the bitmap to match the default density.

Bug: 63909536
Test: CtsGraphicsTestCases
Change-Id: I189306b7a3b706bed32bb267970ade27a57c0e7f
2018-01-19 15:10:05 -05:00
Leon Scroggins III
671cce2605 Make ImageDecoder return animated Drawables
Bug: 63909536
Bug: 63908092
Test: TODO

If ImageDecoder.decodeDrawable is called with an animated image Source
(currently GIF or WebP), return an object of a new (hidden) Drawable
subclass. The new Drawable animates, and it implements Animatable (TODO:
implement Animatable2) so users have some control over the animation.

In addition to the normal features of Drawable, this new one supports
many of the features of ImageDecoder, including scaling, cropping and
PostProcess, which enables circle masks/rounded corners and other
arbitrary after-effects. It does *not* support decoding directly to a
Hardware Bitmap, since it cycles through frames and reuses the same
bitmap memory. But it could be made to use shared memory (TODO?).

TODO: Use a better number for the native allocation registry
TODO: Use the RenderThread to drive the animation, and remove decoding
on the UI thread.
TODO: Add support for modifying the loop count

Android.bp:
- build new AnimatedImageDrawable.cpp

AndroidRuntime.cpp:
- register new native methods

AnimatedImageDrawable.java
AnimatedImageDrawable.cpp:
- new Drawable that handles animated images

Canvas.h, SkiaCanvas.h/.cpp
- New virtual method and implementation for drawing SkAnimatedImages

RecordingCanvas.h/.cpp
- Stub implementation of drawing SkAnimatedImages

ImageDecoder.h/cpp
- Allow code sharing with AnimatedImageDrawable.cpp
  - postProcess
  - access the ImageDecoder struct

Depends on https://skia-review.googlesource.com/c/skia/+/94660 in Skia.

Change-Id: Ie2ec98d9c52deda4d439c6ef8e5dea2861bb93a3
2018-01-17 17:06:57 -05:00
Leon Scroggins III
8c9d8f2aec Ensure that PostProcess Canvas is released
Bug: 63909536
Bug: 63908092
Test: CtsGraphicsTestCases (ImageDecoderTest)

Refactor a method for calling postProcess from ImageDecoder. This will
be shared with the animated drawable (TODO). Call
PostProcess.postProcess in Java inside a try block to ensure that the
Canvas is released. Otherwise, a client could hold on to a pointer to
Canvas and keep using it, even though we have removed its backing.

In addition, share code for calling nDecodeBitmap.

Change-Id: I81ce2befce91ac1e27c78ad059c4b173a2c7e679
2018-01-17 17:04:17 -05:00
Leon Scroggins III
c782ad886f Report more specific error if codec creation fails
Bug: 71578461
Test: CtsGraphicsTestCases

Switch to SkCodec::MakeFromStream, and use its error code to determine
the Exception/error message. Then pass that to
SkAndroidCodec::MakeFromCodec. This is essentially what happened
previously (minus error reporting).

Change-Id: Iabaa61a4321d2f2e257db587013afda605b005b0
2018-01-17 14:33:23 +00:00
Leon Scroggins III
edf26d6e40 Rename onException to onPartialImage
Bug: 63909536
Test: CTS: I8d1672180d8325ae1caf44f0bbf41036b94e6253

In ImageDecoder. The goal is to tell the client that there is a partial
image (which they can use if the callback returns true) and also allow
them to know why the image is partial.

Change how the return value is handled. Instead of returning null, which
is inconsistent with the rest of ImageDecoder, throw the Exception.

Change-Id: I56c38a624c978aa6e6d00fc927b5e355bf9c718a
2018-01-09 16:55:24 -05:00
Leon Scroggins III
1fad09d4d3 Add ImageDecoder.ImageInfo.getMimeType
Bug: 63909536
Test: CTS: Ib2877276da8464b5f3eef0bbb848de202c90e97e

Allows a listener to determine the mimetype.

Change-Id: I0d2aa32f2dbfb37dba97a896037c48814390273d
2018-01-09 16:54:22 -05:00
Leon Scroggins III
b1cc8e6437 Additions and cleanups for ImageDecoder API
Bug: 63909536
Test: CTS: I0f36ce34c968fd7fae4d8edebabea3a421859615

Add overloads for null listener, byte[] without offset + length
Clean up comments

Change-Id: I3dd1dae94cf1fe977d96fcae9b36cbed0adfe749
2018-01-09 15:52:49 -05:00
Leon Scroggins III
ed074fd700 Support using ImageDecoder with ContentResolver + URI
Bug: 63909536
Test: CTS: I0f36ce34c968fd7fae4d8edebabea3a421859615

Add ImageDecoder.createSource(ContentResolver, URI), allowing a client
to decode images from files, content, resources, etc.

Prefer using a file descriptor to using an InputStream so the input can
be cheaply seeked and rewound if necessary.

Make ImageDecoder implement AutoCloseable to handle closing the input.

Make decodeDrawable/decodeBitmap always return an object or throw an
IOException. Avoid checking for a file in the Source constructor.

Fix a bug where inner Exception classes were not static.

Update JavaInputStreamAdaptor to be usable by ImageDecoder:
- previously it always swallowed exceptions. Allow them to propagate
  (optionally) so that they can be reported back to the client.
- Add refs to the InputStream and byte[]. ImageDecoder returns from
  native and then uses the JavaInputStreamAdaptor again, making the
  local refs go out of scope.
- Hold on to the JavaVM and convert to the JNIEnv when necessary. Pass
  local env pointers to avoid looking it up multiple times in one call.
- If an exception is thrown inside the doRead() loop, return the number
  of bytes successfully read.

Change-Id: I869dad55521cf942efd010c06baf3f44c1c08374
2018-01-08 12:41:39 -05:00
Leon Scroggins III
0c01dbf8f2 ImageDecoder (BitmapFactory 2.0)
Bug: 63909536
Bug: 63908092

Test: CTS: I0f36ce34c968fd7fae4d8edebabea3a421859615

One-pager:
https://docs.google.com/document/d/1IWSdXb5O9lu-Zbj7SaNWo5pS7-FHlonFnqazjnecozM/
Design doc:
https://docs.google.com/document/d/15S6DSAV4EwOuJLv29UC_9cdSGdPg3KvOJVn2EHoP3fw/

ImageDecoder is designed to streamline certain patterns of BitmapFactory
use:
- choosing sample size based on actual dimensions
- choosing a specific output size
- post-processing (e.g. for rounded corners)
- copying to HARDWARE
- decode directly to ashmem
- creating a Drawable
- use as an alpha mask
- save RAM (e.g. use RGB_565)

In addition, it will include new features:
- animated drawables (TODO)
- report failures *and* optionally create a partial image
- crop

Add PostProcess to handle post-processing. It is separate from
ImageDecoder so that it may be used in the future by other commands that
might want something similar (e.g. capturing a View).

Consolidate NinePatch code for sharing between BitmapFactory and
ImageDecoder.

Some features left out of this CL:
- Create from ContentResolver + URI
- animation
- report more info in ImageInfo
- more overloads (e.g. null OnHeaderDecodedListener)

Change-Id: Icf011dc1b97b492788e47cf51fcf8abe8e9c7b88
2017-12-19 20:22:17 +00:00