summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/android/java_bitmap.h
blob: dc4db2758e64f4a1d107439b47869cfd84c634b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_ANDROID_JAVA_BITMAP_H_
#define UI_GFX_ANDROID_JAVA_BITMAP_H_

#include <jni.h>
#include <stdint.h>

#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/gfx_export.h"

namespace gfx {

// A Java counterpart will be generated for this enum.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.ui.gfx
// The order and values here match AndroidBitmapFormat, as verified
// by static_asserts in java_bitmap.cc.
enum BitmapFormat {
  BITMAP_FORMAT_NO_CONFIG = 0,
  BITMAP_FORMAT_ARGB_8888 = 1,
  BITMAP_FORMAT_RGB_565 = 4,
  BITMAP_FORMAT_ARGB_4444 = 7,
  BITMAP_FORMAT_ALPHA_8 = 8,
};

// This class wraps a JNI AndroidBitmap object to make it easier to use. It
// handles locking and unlocking of the underlying pixels, along with wrapping
// various JNI methods.
class GFX_EXPORT JavaBitmap {
 public:
  explicit JavaBitmap(const base::android::JavaRef<jobject>& bitmap);
  ~JavaBitmap();

  inline void* pixels() { return pixels_; }
  inline const void* pixels() const { return pixels_; }
  inline const gfx::Size& size() const { return size_; }
  inline BitmapFormat format() const { return format_; }
  inline uint32_t bytes_per_row() const { return bytes_per_row_; }
  inline int byte_count() const { return byte_count_; }

 private:
  base::android::ScopedJavaGlobalRef<jobject> bitmap_;
  void* pixels_;
  gfx::Size size_;
  BitmapFormat format_;
  uint32_t bytes_per_row_;
  int byte_count_;

  DISALLOW_COPY_AND_ASSIGN(JavaBitmap);
};

enum class OomBehavior {
  kCrashOnOom,
  kReturnNullOnOom,
};

// Converts |skbitmap| to a Java-backed bitmap (android.graphics.Bitmap).
// Note: |skbitmap| is assumed to be non-null, non-empty and one of RGBA_8888 or
// RGB_565 formats.
GFX_EXPORT base::android::ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(
    const SkBitmap& skbitmap,
    OomBehavior reaction = OomBehavior::kCrashOnOom);

// Converts |bitmap| to an SkBitmap of the same size and format.
// Note: |jbitmap| is assumed to be non-null, non-empty and of format RGBA_8888.
GFX_EXPORT SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap);

// Returns a Skia color type value for the requested input java Bitmap.Config.
GFX_EXPORT SkColorType
ConvertToSkiaColorType(const base::android::JavaRef<jobject>& jbitmap_config);

}  // namespace gfx

#endif  // UI_GFX_ANDROID_JAVA_BITMAP_H_