Bug 1877752 - Remove now-unused use_counter error metric. r=chutten
[gecko.git] / widget / android / AndroidCompositorWidget.cpp
bloba7eeb665e1480b2f557cd7e402d096133f890dfa
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=2 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "AndroidCompositorWidget.h"
9 #include "mozilla/gfx/Logging.h"
10 #include "mozilla/widget/PlatformWidgetTypes.h"
11 #include "nsWindow.h"
13 namespace mozilla {
14 namespace widget {
16 AndroidCompositorWidget::AndroidCompositorWidget(
17 const AndroidCompositorWidgetInitData& aInitData,
18 const layers::CompositorOptions& aOptions)
19 : CompositorWidget(aOptions),
20 mWidgetId(aInitData.widgetId()),
21 mNativeWindow(nullptr),
22 mFormat(WINDOW_FORMAT_RGBA_8888),
23 mClientSize(aInitData.clientSize()) {}
25 AndroidCompositorWidget::~AndroidCompositorWidget() {
26 if (mNativeWindow) {
27 ANativeWindow_release(mNativeWindow);
31 already_AddRefed<gfx::DrawTarget>
32 AndroidCompositorWidget::StartRemoteDrawingInRegion(
33 const LayoutDeviceIntRegion& aInvalidRegion,
34 layers::BufferMode* aBufferMode) {
35 if (!mNativeWindow) {
36 EGLNativeWindowType window = GetEGLNativeWindow();
37 JNIEnv* const env = jni::GetEnvForThread();
38 mNativeWindow =
39 ANativeWindow_fromSurface(env, reinterpret_cast<jobject>(window));
40 if (mNativeWindow) {
41 mFormat = ANativeWindow_getFormat(mNativeWindow);
42 ANativeWindow_acquire(mNativeWindow);
43 } else {
44 return nullptr;
48 if (mFormat != WINDOW_FORMAT_RGBA_8888 &&
49 mFormat != WINDOW_FORMAT_RGBX_8888) {
50 gfxCriticalNoteOnce << "Non supported format: " << mFormat;
51 return nullptr;
54 // XXX Handle inOutDirtyBounds
55 if (ANativeWindow_lock(mNativeWindow, &mBuffer, nullptr) != 0) {
56 return nullptr;
59 const int bpp = 4;
60 gfx::SurfaceFormat format = gfx::SurfaceFormat::R8G8B8A8;
61 if (mFormat == WINDOW_FORMAT_RGBX_8888) {
62 format = gfx::SurfaceFormat::R8G8B8X8;
65 RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateDrawTargetForData(
66 gfx::BackendType::SKIA, static_cast<unsigned char*>(mBuffer.bits),
67 gfx::IntSize(mBuffer.width, mBuffer.height), mBuffer.stride * bpp, format,
68 true);
70 return dt.forget();
73 void AndroidCompositorWidget::EndRemoteDrawingInRegion(
74 gfx::DrawTarget* aDrawTarget, const LayoutDeviceIntRegion& aInvalidRegion) {
75 ANativeWindow_unlockAndPost(mNativeWindow);
78 bool AndroidCompositorWidget::OnResumeComposition() {
79 OnCompositorSurfaceChanged();
81 if (!mSurface) {
82 gfxCriticalError() << "OnResumeComposition called with null Surface";
83 return false;
86 return true;
89 EGLNativeWindowType AndroidCompositorWidget::GetEGLNativeWindow() {
90 return (EGLNativeWindowType)mSurface.Get();
93 LayoutDeviceIntSize AndroidCompositorWidget::GetClientSize() {
94 return mClientSize;
97 void AndroidCompositorWidget::NotifyClientSizeChanged(
98 const LayoutDeviceIntSize& aClientSize) {
99 mClientSize =
100 LayoutDeviceIntSize(std::min(aClientSize.width, MOZ_WIDGET_MAX_SIZE),
101 std::min(aClientSize.height, MOZ_WIDGET_MAX_SIZE));
104 } // namespace widget
105 } // namespace mozilla