Performance histograms for extension content verification
[chromium-blink-merge.git] / ui / gfx / android / java_bitmap.h
blob44a17cade7442f4f8dc9550717c6268a291d2611
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.
5 #ifndef UI_GFX_ANDROID_JAVA_BITMAP_H_
6 #define UI_GFX_ANDROID_JAVA_BITMAP_H_
8 #include <jni.h>
10 #include "base/android/scoped_java_ref.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/gfx/size.h"
14 namespace gfx {
16 // Define Bitmap Config values like BITMAP_CONFIG_ARGB_8888 in a
17 // way that ensures they're always the same than their Java counterpart.
19 enum BitmapConfig {
20 #define DEFINE_BITMAP_CONFIG(x, y) BITMAP_##x = y,
21 #include "bitmap_config_list.h"
22 #undef DEFINE_BITMAP_CONFIG
25 // This class wraps a JNI AndroidBitmap object to make it easier to use. It
26 // handles locking and unlocking of the underlying pixels, along with wrapping
27 // various JNI methods.
28 class GFX_EXPORT JavaBitmap {
29 public:
30 explicit JavaBitmap(jobject bitmap);
31 ~JavaBitmap();
33 inline void* pixels() { return pixels_; }
34 inline const void* pixels() const { return pixels_; }
35 inline const gfx::Size& size() const { return size_; }
36 // Formats are in android/bitmap.h; e.g. ANDROID_BITMAP_FORMAT_RGBA_8888
37 inline int format() const { return format_; }
38 inline uint32_t stride() const { return stride_; }
40 // Registers methods with JNI and returns true if succeeded.
41 static bool RegisterJavaBitmap(JNIEnv* env);
43 private:
44 jobject bitmap_;
45 void* pixels_;
46 gfx::Size size_;
47 int format_;
48 uint32_t stride_;
50 DISALLOW_COPY_AND_ASSIGN(JavaBitmap);
53 // Allocates a Java-backed bitmap (android.graphics.Bitmap) with the given
54 // (non-empty!) size and configuration.
55 GFX_EXPORT base::android::ScopedJavaLocalRef<jobject> CreateJavaBitmap(
56 int width,
57 int height,
58 SkBitmap::Config bitmap_config);
60 // Loads a Java-backed bitmap (android.graphics.Bitmap) from the provided
61 // drawable resource identifier (e.g., android:drawable/overscroll_glow). If the
62 // resource loads successfully, it will be integrally scaled down, preserving
63 // aspect ratio, to a size no smaller than |size|. Otherwise, null is returned.
64 GFX_EXPORT base::android::ScopedJavaLocalRef<jobject>
65 CreateJavaBitmapFromAndroidResource(const char* name, gfx::Size size);
67 // Converts |skbitmap| to a Java-backed bitmap (android.graphics.Bitmap).
68 // Note: |skbitmap| is assumed to be non-null, non-empty and one of RGBA_8888 or
69 // RGB_565 formats.
70 GFX_EXPORT base::android::ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(
71 const SkBitmap* skbitmap);
73 // Converts |bitmap| to an SkBitmap of the same size and format.
74 // Note: |jbitmap| is assumed to be non-null, non-empty and of format RGBA_8888.
75 GFX_EXPORT SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap);
77 // Returns a Skia config value for the requested input java Bitmap.Config.
78 GFX_EXPORT SkBitmap::Config ConvertToSkiaConfig(jobject bitmap_config);
80 } // namespace gfx
82 #endif // UI_GFX_ANDROID_JAVA_BITMAP_H_