This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathhardware_buffer_external_texture.cc
More file actions
115 lines (98 loc) · 3.7 KB
/
hardware_buffer_external_texture.cc
File metadata and controls
115 lines (98 loc) · 3.7 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "flutter/shell/platform/android/hardware_buffer_external_texture.h"
#include <android/hardware_buffer_jni.h>
#include <android/sensor.h>
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
#include "flutter/shell/platform/android/ndk_helpers.h"
namespace flutter {
HardwareBufferExternalTexture::HardwareBufferExternalTexture(
int64_t id,
const fml::jni::ScopedJavaGlobalRef<jobject>& image_texture_entry,
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade)
: Texture(id),
image_texture_entry_(image_texture_entry),
jni_facade_(jni_facade) {}
// Implementing flutter::Texture.
void HardwareBufferExternalTexture::Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
const DlImageSampling sampling) {
if (state_ == AttachmentState::kDetached) {
return;
}
Attach(context);
const bool should_process_frame =
(!freeze && new_frame_ready_) || dl_image_ == nullptr;
if (should_process_frame) {
ProcessFrame(context, bounds);
new_frame_ready_ = false;
}
if (dl_image_) {
context.canvas->DrawImageRect(
dl_image_, // image
SkRect::Make(dl_image_->bounds()), // source rect
bounds, // destination rect
sampling, // sampling
context.paint, // paint
flutter::DlCanvas::SrcRectConstraint::kStrict // enforce edges
);
} else {
FML_LOG(ERROR)
<< "No DlImage available for HardwareBufferExternalTexture to paint.";
}
}
// Implementing flutter::Texture.
void HardwareBufferExternalTexture::MarkNewFrameAvailable() {
new_frame_ready_ = true;
}
// Implementing flutter::Texture.
void HardwareBufferExternalTexture::OnTextureUnregistered() {}
// Implementing flutter::ContextListener.
void HardwareBufferExternalTexture::OnGrContextCreated() {
state_ = AttachmentState::kUninitialized;
}
// Implementing flutter::ContextListener.
void HardwareBufferExternalTexture::OnGrContextDestroyed() {
if (state_ == AttachmentState::kAttached) {
dl_image_.reset();
Detach();
}
state_ = AttachmentState::kDetached;
}
JavaLocalRef HardwareBufferExternalTexture::AcquireLatestImage() {
JNIEnv* env = fml::jni::AttachCurrentThread();
FML_CHECK(env != nullptr);
// ImageTextureEntry.acquireLatestImage.
JavaLocalRef image_java = jni_facade_->ImageTextureEntryAcquireLatestImage(
JavaLocalRef(image_texture_entry_));
return image_java;
}
void HardwareBufferExternalTexture::CloseImage(
const fml::jni::JavaRef<jobject>& image) {
if (image.obj() == nullptr) {
return;
}
jni_facade_->ImageClose(JavaLocalRef(image));
}
void HardwareBufferExternalTexture::CloseHardwareBuffer(
const fml::jni::JavaRef<jobject>& hardware_buffer) {
if (hardware_buffer.obj() == nullptr) {
return;
}
jni_facade_->HardwareBufferClose(JavaLocalRef(hardware_buffer));
}
JavaLocalRef HardwareBufferExternalTexture::HardwareBufferFor(
const fml::jni::JavaRef<jobject>& image) {
if (image.obj() == nullptr) {
return JavaLocalRef();
}
// Image.getHardwareBuffer.
return jni_facade_->ImageGetHardwareBuffer(JavaLocalRef(image));
}
AHardwareBuffer* HardwareBufferExternalTexture::AHardwareBufferFor(
const fml::jni::JavaRef<jobject>& hardware_buffer) {
JNIEnv* env = fml::jni::AttachCurrentThread();
FML_CHECK(env != nullptr);
return NDKHelpers::AHardwareBuffer_fromHardwareBuffer(env,
hardware_buffer.obj());
}
} // namespace flutter