[Histograms]: Add histograms for requestIdleCallback.
[chromium-blink-merge.git] / ui / gl / android / scoped_java_surface.cc
blobae4f81a44bd82a23e9e83f0b5a9f4c7b0bea2e27
1 // Copyright (c) 2013 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 #include "ui/gl/android/scoped_java_surface.h"
7 #include "base/logging.h"
8 #include "jni/Surface_jni.h"
9 #include "ui/gl/android/surface_texture.h"
11 namespace {
13 bool g_jni_initialized = false;
15 void RegisterNativesIfNeeded(JNIEnv* env) {
16 if (!g_jni_initialized) {
17 JNI_Surface::RegisterNativesImpl(env);
18 g_jni_initialized = true;
22 } // anonymous namespace
24 namespace gfx {
26 ScopedJavaSurface::ScopedJavaSurface() {
29 ScopedJavaSurface::ScopedJavaSurface(
30 const base::android::JavaRef<jobject>& surface)
31 : auto_release_(true),
32 is_protected_(false) {
33 JNIEnv* env = base::android::AttachCurrentThread();
34 RegisterNativesIfNeeded(env);
35 DCHECK(env->IsInstanceOf(surface.obj(), Surface_clazz(env)));
36 j_surface_.Reset(surface);
39 ScopedJavaSurface::ScopedJavaSurface(
40 const SurfaceTexture* surface_texture)
41 : auto_release_(true),
42 is_protected_(false) {
43 JNIEnv* env = base::android::AttachCurrentThread();
44 RegisterNativesIfNeeded(env);
45 ScopedJavaLocalRef<jobject> tmp(JNI_Surface::Java_Surface_Constructor(
46 env, surface_texture->j_surface_texture().obj()));
47 DCHECK(!tmp.is_null());
48 j_surface_.Reset(tmp);
51 ScopedJavaSurface::ScopedJavaSurface(RValue rvalue) {
52 MoveFrom(*rvalue.object);
55 ScopedJavaSurface& ScopedJavaSurface::operator=(RValue rhs) {
56 MoveFrom(*rhs.object);
57 return *this;
60 ScopedJavaSurface::~ScopedJavaSurface() {
61 if (auto_release_ && !j_surface_.is_null()) {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 JNI_Surface::Java_Surface_release(env, j_surface_.obj());
67 void ScopedJavaSurface::MoveFrom(ScopedJavaSurface& other) {
68 JNIEnv* env = base::android::AttachCurrentThread();
69 j_surface_.Reset(env, other.j_surface_.Release());
70 auto_release_ = other.auto_release_;
71 is_protected_ = other.is_protected_;
74 bool ScopedJavaSurface::IsEmpty() const {
75 return j_surface_.is_null();
78 // static
79 ScopedJavaSurface ScopedJavaSurface::AcquireExternalSurface(jobject surface) {
80 JNIEnv* env = base::android::AttachCurrentThread();
81 ScopedJavaLocalRef<jobject> surface_ref;
82 surface_ref.Reset(env, surface);
83 gfx::ScopedJavaSurface scoped_surface(surface_ref);
84 scoped_surface.auto_release_ = false;
85 scoped_surface.is_protected_ = true;
86 return scoped_surface;
89 } // namespace gfx