1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef UI_GFX_ANDROID_JAVA_BITMAP_H_ 6#define UI_GFX_ANDROID_JAVA_BITMAP_H_ 7 8#include <jni.h> 9 10#include "base/android/scoped_java_ref.h" 11#include "third_party/skia/include/core/SkBitmap.h" 12#include "ui/gfx/size.h" 13 14namespace gfx { 15 16// A Java counterpart will be generated for this enum. 17// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.ui.gfx 18enum BitmapFormat { 19 BITMAP_FORMAT_NO_CONFIG, 20 BITMAP_FORMAT_ALPHA_8, 21 BITMAP_FORMAT_ARGB_4444, 22 BITMAP_FORMAT_ARGB_8888, 23 BITMAP_FORMAT_RGB_565, 24}; 25 26// This class wraps a JNI AndroidBitmap object to make it easier to use. It 27// handles locking and unlocking of the underlying pixels, along with wrapping 28// various JNI methods. 29class GFX_EXPORT JavaBitmap { 30 public: 31 explicit JavaBitmap(jobject bitmap); 32 ~JavaBitmap(); 33 34 inline void* pixels() { return pixels_; } 35 inline const void* pixels() const { return pixels_; } 36 inline const gfx::Size& size() const { return size_; } 37 // Formats are in android/bitmap.h; e.g. ANDROID_BITMAP_FORMAT_RGBA_8888 38 inline int format() const { return format_; } 39 inline uint32_t stride() const { return stride_; } 40 41 // Registers methods with JNI and returns true if succeeded. 42 static bool RegisterJavaBitmap(JNIEnv* env); 43 44 private: 45 jobject bitmap_; 46 void* pixels_; 47 gfx::Size size_; 48 int format_; 49 uint32_t stride_; 50 51 DISALLOW_COPY_AND_ASSIGN(JavaBitmap); 52}; 53 54// Allocates a Java-backed bitmap (android.graphics.Bitmap) with the given 55// (non-empty!) size and color type. 56GFX_EXPORT base::android::ScopedJavaLocalRef<jobject> CreateJavaBitmap( 57 int width, 58 int height, 59 SkColorType color_type); 60 61// Loads an SkBitmap from the provided drawable resource identifier (e.g., 62// android:drawable/overscroll_glow). If the resource loads successfully, it 63// will be integrally scaled down, preserving aspect ratio, to a size no smaller 64// than |size|. Otherwise, an empty bitmap is returned. 65GFX_EXPORT SkBitmap 66 CreateSkBitmapFromAndroidResource(const char* name, gfx::Size size); 67 68// Converts |skbitmap| to a Java-backed bitmap (android.graphics.Bitmap). 69// Note: |skbitmap| is assumed to be non-null, non-empty and one of RGBA_8888 or 70// RGB_565 formats. 71GFX_EXPORT base::android::ScopedJavaLocalRef<jobject> ConvertToJavaBitmap( 72 const SkBitmap* skbitmap); 73 74// Converts |bitmap| to an SkBitmap of the same size and format. 75// Note: |jbitmap| is assumed to be non-null, non-empty and of format RGBA_8888. 76GFX_EXPORT SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap); 77 78// Returns a Skia color type value for the requested input java Bitmap.Config. 79GFX_EXPORT SkColorType ConvertToSkiaColorType(jobject jbitmap_config); 80 81} // namespace gfx 82 83#endif // UI_GFX_ANDROID_JAVA_BITMAP_H_ 84