Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics;
     18 
     19 import com.android.layoutlib.bridge.Bridge;
     20 import com.android.layoutlib.bridge.impl.DelegateManager;
     21 import com.android.ninepatch.NinePatchChunk;
     22 import com.android.resources.Density;
     23 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
     24 
     25 import android.annotation.Nullable;
     26 import android.content.res.BridgeResources.NinePatchInputStream;
     27 import android.graphics.BitmapFactory.Options;
     28 import android.graphics.Bitmap_Delegate.BitmapCreateFlags;
     29 
     30 import java.io.FileDescriptor;
     31 import java.io.IOException;
     32 import java.io.InputStream;
     33 import java.util.EnumSet;
     34 import java.util.Set;
     35 
     36 /**
     37  * Delegate implementing the native methods of android.graphics.BitmapFactory
     38  *
     39  * Through the layoutlib_create tool, the original native methods of BitmapFactory have been
     40  * replaced by calls to methods of the same name in this delegate class.
     41  *
     42  * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager}
     43  * around to map int to instance of the delegate.
     44  *
     45  */
     46 /*package*/ class BitmapFactory_Delegate {
     47 
     48     // ------ Native Delegates ------
     49 
     50     @LayoutlibDelegate
     51     /*package*/ static Bitmap nativeDecodeStream(InputStream is, byte[] storage,
     52             @Nullable Rect padding, @Nullable Options opts) {
     53         Bitmap bm = null;
     54 
     55         Density density = Density.MEDIUM;
     56         Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
     57         if (opts != null) {
     58             density = Density.getEnum(opts.inDensity);
     59             if (opts.inPremultiplied) {
     60                 bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
     61             }
     62         }
     63 
     64         try {
     65             if (is instanceof NinePatchInputStream) {
     66                 NinePatchInputStream npis = (NinePatchInputStream) is;
     67                 npis.disableFakeMarkSupport();
     68 
     69                 // load the bitmap as a nine patch
     70                 com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load(
     71                         npis, true /*is9Patch*/, false /*convert*/);
     72 
     73                 // get the bitmap and chunk objects.
     74                 bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags,
     75                         density);
     76                 NinePatchChunk chunk = ninePatch.getChunk();
     77 
     78                 // put the chunk in the bitmap
     79                 bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
     80 
     81                 if (padding != null) {
     82                     // read the padding
     83                     int[] paddingArray = chunk.getPadding();
     84                     padding.left = paddingArray[0];
     85                     padding.top = paddingArray[1];
     86                     padding.right = paddingArray[2];
     87                     padding.bottom = paddingArray[3];
     88                 }
     89             } else {
     90                 // load the bitmap directly.
     91                 bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density);
     92             }
     93         } catch (IOException e) {
     94             Bridge.getLog().error(null, "Failed to load image", e, null);
     95         }
     96 
     97         return bm;
     98     }
     99 
    100     @LayoutlibDelegate
    101     /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,
    102             Rect padding, Options opts) {
    103         opts.inBitmap = null;
    104         return null;
    105     }
    106 
    107     @LayoutlibDelegate
    108     /*package*/ static Bitmap nativeDecodeAsset(long asset, Rect padding, Options opts) {
    109         opts.inBitmap = null;
    110         return null;
    111     }
    112 
    113     @LayoutlibDelegate
    114     /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset,
    115             int length, Options opts) {
    116         opts.inBitmap = null;
    117         return null;
    118     }
    119 
    120     @LayoutlibDelegate
    121     /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) {
    122         return true;
    123     }
    124 }
    125