Move WebView plat_support from frameworks/webview.
Move this code to be in the same repository as the other parts of
WebView's current implementation.
Bug: 62445369
Test: m
Change-Id: I567eac7f3484fa78a948fb84545e578fe18c236d
diff --git a/native/webview/plat_support/Android.mk b/native/webview/plat_support/Android.mk
new file mode 100644
index 0000000..6a33fe2
--- /dev/null
+++ b/native/webview/plat_support/Android.mk
@@ -0,0 +1,52 @@
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# This package provides the system interfaces allowing WebView to render.
+
+LOCAL_PATH := $(call my-dir)
+
+# Native support library (libwebviewchromium_plat_support.so) - does NOT link
+# any native chromium code.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE:= libwebviewchromium_plat_support
+
+LOCAL_SRC_FILES:= \
+ draw_gl_functor.cpp \
+ jni_entry_point.cpp \
+ graphics_utils.cpp \
+ graphic_buffer_impl.cpp \
+
+LOCAL_C_INCLUDES:= \
+ external/skia/include/core \
+ frameworks/base/core/jni/android/graphics \
+ frameworks/native/include/ui \
+
+LOCAL_SHARED_LIBRARIES += \
+ libandroid_runtime \
+ liblog \
+ libcutils \
+ libui \
+ libutils \
+ libhwui \
+ libandroidfw
+
+LOCAL_MODULE_TAGS := optional
+
+# To remove warnings from skia header files
+LOCAL_CFLAGS := -Wno-unused-parameter
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/native/webview/plat_support/LICENSE b/native/webview/plat_support/LICENSE
new file mode 100644
index 0000000..972bb2e
--- /dev/null
+++ b/native/webview/plat_support/LICENSE
@@ -0,0 +1,27 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/native/webview/plat_support/draw_gl.h b/native/webview/plat_support/draw_gl.h
new file mode 100644
index 0000000..c8434b6
--- /dev/null
+++ b/native/webview/plat_support/draw_gl.h
@@ -0,0 +1,131 @@
+// Copyright 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.
+//
+//******************************************************************************
+// This is a copy of the coresponding android_webview/public/browser header.
+// Any changes to the interface should be made there.
+//
+// The purpose of having the copy is twofold:
+// - it removes the need to have Chromium sources present in the tree in order
+// to build the plat_support library,
+// - it captures API that the corresponding Android release supports.
+//******************************************************************************
+
+#ifndef ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_GL_H_
+#define ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_GL_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// 1 is L/L MR1
+//
+// 2 starts at M, and added an imperfect workaround for complex clipping by
+// elevating the WebView into an FBO layer. If any transform, clip, or outline
+// clip occurs that would either likely use the stencil buffer for clipping, or
+// require shader based clipping in HWUI, the WebView is drawn into an FBO (if
+// it fits).
+// This is a temporary workaround for a lack of WebView support for stencil/
+// shader based round rect clipping, and should be removed when webview is
+// capable of supporting these clips internally when drawing.
+//
+// 3 starts during development of P, when android defaults from HWUI to skia as
+// the GL renderer. Skia already maintains and restores its GL state, so there
+// is no need for WebView to restore this state. Skia also no longer promises
+// GL state on entering draw, such as no vertex array buffer binding.
+static const int kAwDrawGLInfoVersion = 3;
+
+// Holds the information required to trigger an OpenGL drawing operation.
+struct AwDrawGLInfo {
+ int version; // The AwDrawGLInfo this struct was built with.
+
+ // Input: tells the draw function what action to perform.
+ enum Mode {
+ kModeDraw = 0,
+ kModeProcess,
+ kModeProcessNoContext,
+ kModeSync,
+ } mode;
+
+ // Input: current clip rect in surface coordinates. Reflects the current state
+ // of the OpenGL scissor rect. Both the OpenGL scissor rect and viewport are
+ // set by the caller of the draw function and updated during View animations.
+ int clip_left;
+ int clip_top;
+ int clip_right;
+ int clip_bottom;
+
+ // Input: current width/height of destination surface.
+ int width;
+ int height;
+
+ // Input: is the View rendered into an independent layer.
+ // If false, the surface is likely to hold to the full screen contents, with
+ // the scissor box set by the caller to the actual View location and size.
+ // Also the transformation matrix will contain at least a translation to the
+ // position of the View to render, plus any other transformations required as
+ // part of any ongoing View animation. View translucency (alpha) is ignored,
+ // although the framework will set is_layer to true for non-opaque cases.
+ // Can be requested via the View.setLayerType(View.LAYER_TYPE_NONE, ...)
+ // Android API method.
+ //
+ // If true, the surface is dedicated to the View and should have its size.
+ // The viewport and scissor box are set by the caller to the whole surface.
+ // Animation transformations are handled by the caller and not reflected in
+ // the provided transformation matrix. Translucency works normally.
+ // Can be requested via the View.setLayerType(View.LAYER_TYPE_HARDWARE, ...)
+ // Android API method.
+ bool is_layer;
+
+ // Input: current transformation matrix in surface pixels.
+ // Uses the column-based OpenGL matrix format.
+ float transform[16];
+};
+
+// Function to invoke a direct GL draw into the client's pre-configured
+// GL context. Obtained via AwContents.getDrawGLFunction() (static).
+// |view_context| is an opaque identifier that was returned by the corresponding
+// call to AwContents.getAwDrawGLViewContext().
+// |draw_info| carries the in and out parameters for this draw.
+// |spare| ignored; pass NULL.
+typedef void (AwDrawGLFunction)(long view_context,
+ AwDrawGLInfo* draw_info,
+ void* spare);
+enum AwMapMode {
+ MAP_READ_ONLY,
+ MAP_WRITE_ONLY,
+ MAP_READ_WRITE,
+};
+
+// Called to create a GraphicBuffer
+typedef long AwCreateGraphicBufferFunction(int w, int h);
+// Called to release a GraphicBuffer
+typedef void AwReleaseGraphicBufferFunction(long buffer_id);
+// Called to map a GraphicBuffer in |mode|.
+typedef int AwMapFunction(long buffer_id, AwMapMode mode, void** vaddr);
+// Called to unmap a GraphicBuffer
+typedef int AwUnmapFunction(long buffer_id);
+// Called to get a native buffer pointer
+typedef void* AwGetNativeBufferFunction(long buffer_id);
+// Called to get the stride of the buffer
+typedef unsigned int AwGetStrideFunction(long buffer_id);
+
+static const int kAwDrawGLFunctionTableVersion = 1;
+
+// Set of functions used in rendering in hardware mode
+struct AwDrawGLFunctionTable {
+ int version;
+ AwCreateGraphicBufferFunction* create_graphic_buffer;
+ AwReleaseGraphicBufferFunction* release_graphic_buffer;
+ AwMapFunction* map;
+ AwUnmapFunction* unmap;
+ AwGetNativeBufferFunction* get_native_buffer;
+ AwGetStrideFunction* get_stride;
+};
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif // ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_GL_H_
diff --git a/native/webview/plat_support/draw_gl_functor.cpp b/native/webview/plat_support/draw_gl_functor.cpp
new file mode 100644
index 0000000..d54f558
--- /dev/null
+++ b/native/webview/plat_support/draw_gl_functor.cpp
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Provides a webviewchromium glue layer adapter from the internal Android
+// GL Functor data types into the types the chromium stack expects, and back.
+
+#define LOG_TAG "webviewchromium_plat_support"
+
+#include "draw_gl.h"
+
+#include <Properties.h>
+#include <errno.h>
+#include <jni.h>
+#include <private/hwui/DrawGlInfo.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <utils/Functor.h>
+#include <utils/Log.h>
+
+#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
+#define COMPILE_ASSERT(expr, err) \
+__unused static const char (err)[(expr) ? 1 : -1] = "";
+
+namespace android {
+namespace {
+
+AwDrawGLFunction* g_aw_drawgl_function = NULL;
+
+class DrawGLFunctor : public Functor {
+ public:
+ explicit DrawGLFunctor(jlong view_context) : view_context_(view_context) {}
+ virtual ~DrawGLFunctor() {}
+
+ // Functor
+ virtual status_t operator ()(int what, void* data) {
+ using uirenderer::DrawGlInfo;
+ if (!g_aw_drawgl_function) {
+ ALOGE("Cannot draw: no DrawGL Function installed");
+ return DrawGlInfo::kStatusDone;
+ }
+
+ AwDrawGLInfo aw_info;
+ // TODO(boliu): Remove property check once OpenGL fallback is removed.
+ auto render_pipeline_type =
+ android::uirenderer::Properties::getRenderPipelineType();
+ aw_info.version = (render_pipeline_type ==
+ android::uirenderer::RenderPipelineType::OpenGL)
+ ? 2
+ : kAwDrawGLInfoVersion;
+ switch (what) {
+ case DrawGlInfo::kModeDraw: {
+ aw_info.mode = AwDrawGLInfo::kModeDraw;
+ DrawGlInfo* gl_info = reinterpret_cast<DrawGlInfo*>(data);
+
+ // Map across the input values.
+ aw_info.clip_left = gl_info->clipLeft;
+ aw_info.clip_top = gl_info->clipTop;
+ aw_info.clip_right = gl_info->clipRight;
+ aw_info.clip_bottom = gl_info->clipBottom;
+ aw_info.width = gl_info->width;
+ aw_info.height = gl_info->height;
+ aw_info.is_layer = gl_info->isLayer;
+ COMPILE_ASSERT(NELEM(aw_info.transform) == NELEM(gl_info->transform),
+ mismatched_transform_matrix_sizes);
+ for (int i = 0; i < NELEM(aw_info.transform); ++i) {
+ aw_info.transform[i] = gl_info->transform[i];
+ }
+ break;
+ }
+ case DrawGlInfo::kModeProcess:
+ aw_info.mode = AwDrawGLInfo::kModeProcess;
+ break;
+ case DrawGlInfo::kModeProcessNoContext:
+ aw_info.mode = AwDrawGLInfo::kModeProcessNoContext;
+ break;
+ case DrawGlInfo::kModeSync:
+ aw_info.mode = AwDrawGLInfo::kModeSync;
+ break;
+ default:
+ ALOGE("Unexpected DrawGLInfo type %d", what);
+ return DrawGlInfo::kStatusDone;
+ }
+
+ // Invoke the DrawGL method.
+ g_aw_drawgl_function(view_context_, &aw_info, NULL);
+
+ return DrawGlInfo::kStatusDone;
+ }
+
+ private:
+ intptr_t view_context_;
+};
+
+// Raise the file handle soft limit to the hard limit since gralloc buffers
+// uses file handles.
+void RaiseFileNumberLimit() {
+ static bool have_raised_limit = false;
+ if (have_raised_limit)
+ return;
+
+ have_raised_limit = true;
+ struct rlimit limit_struct;
+ limit_struct.rlim_cur = 0;
+ limit_struct.rlim_max = 0;
+ if (getrlimit(RLIMIT_NOFILE, &limit_struct) == 0) {
+ limit_struct.rlim_cur = limit_struct.rlim_max;
+ if (setrlimit(RLIMIT_NOFILE, &limit_struct) != 0) {
+ ALOGE("setrlimit failed: %s", strerror(errno));
+ }
+ } else {
+ ALOGE("getrlimit failed: %s", strerror(errno));
+ }
+}
+
+jlong CreateGLFunctor(JNIEnv*, jclass, jlong view_context) {
+ RaiseFileNumberLimit();
+ return reinterpret_cast<jlong>(new DrawGLFunctor(view_context));
+}
+
+void DestroyGLFunctor(JNIEnv*, jclass, jlong functor) {
+ delete reinterpret_cast<DrawGLFunctor*>(functor);
+}
+
+void SetChromiumAwDrawGLFunction(JNIEnv*, jclass, jlong draw_function) {
+ g_aw_drawgl_function = reinterpret_cast<AwDrawGLFunction*>(draw_function);
+}
+
+const char kClassName[] = "com/android/webview/chromium/DrawGLFunctor";
+const JNINativeMethod kJniMethods[] = {
+ { "nativeCreateGLFunctor", "(J)J",
+ reinterpret_cast<void*>(CreateGLFunctor) },
+ { "nativeDestroyGLFunctor", "(J)V",
+ reinterpret_cast<void*>(DestroyGLFunctor) },
+ { "nativeSetChromiumAwDrawGLFunction", "(J)V",
+ reinterpret_cast<void*>(SetChromiumAwDrawGLFunction) },
+};
+
+} // namespace
+
+void RegisterDrawGLFunctor(JNIEnv* env) {
+ jclass clazz = env->FindClass(kClassName);
+ LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);
+
+ int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
+ LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
+}
+
+} // namespace android
diff --git a/native/webview/plat_support/draw_sw.h b/native/webview/plat_support/draw_sw.h
new file mode 100644
index 0000000..7423e13
--- /dev/null
+++ b/native/webview/plat_support/draw_sw.h
@@ -0,0 +1,66 @@
+// 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.
+//
+//******************************************************************************
+// This is a copy of the coresponding android_webview/public/browser header.
+// Any changes to the interface should be made there.
+//
+// The purpose of having the copy is twofold:
+// - it removes the need to have Chromium sources present in the tree in order
+// to build the plat_support library,
+// - it captures API that the corresponding Android release supports.
+//******************************************************************************
+
+#ifndef ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_SW_H_
+#define ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_SW_H_
+
+#include <jni.h>
+#include <stddef.h>
+
+#ifndef __cplusplus
+#error "Can't mix C and C++ when using jni.h"
+#endif
+
+class SkCanvasState;
+class SkPicture;
+
+static const int kAwPixelInfoVersion = 3;
+
+// Holds the information required to implement the SW draw to system canvas.
+struct AwPixelInfo {
+ int version; // The kAwPixelInfoVersion this struct was built with.
+ SkCanvasState* state; // The externalize state in skia format.
+ // NOTE: If you add more members, bump kAwPixelInfoVersion.
+};
+
+// Function that can be called to fish out the underlying native pixel data
+// from a Java canvas object, for optimized rendering path.
+// Returns the pixel info on success, which must be freed via a call to
+// AwReleasePixelsFunction, or NULL.
+typedef AwPixelInfo* (AwAccessPixelsFunction)(JNIEnv* env, jobject canvas);
+
+// Must be called to balance every *successful* call to AwAccessPixelsFunction
+// (i.e. that returned true).
+typedef void (AwReleasePixelsFunction)(AwPixelInfo* pixels);
+
+// Called to create an Android Picture object encapsulating a native SkPicture.
+typedef jobject (AwCreatePictureFunction)(JNIEnv* env, SkPicture* picture);
+
+// Method that returns the current Skia function.
+typedef void (SkiaVersionFunction)(int* major, int* minor, int* patch);
+
+// Called to verify if the Skia versions are compatible.
+typedef bool (AwIsSkiaVersionCompatibleFunction)(SkiaVersionFunction function);
+
+static const int kAwDrawSWFunctionTableVersion = 1;
+
+// "vtable" for the functions declared in this file. An instance must be set via
+// AwContents.setAwDrawSWFunctionTable
+struct AwDrawSWFunctionTable {
+ int version;
+ AwAccessPixelsFunction* access_pixels;
+ AwReleasePixelsFunction* release_pixels;
+};
+
+#endif // ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_SW_H_
diff --git a/native/webview/plat_support/graphic_buffer_impl.cpp b/native/webview/plat_support/graphic_buffer_impl.cpp
new file mode 100644
index 0000000..4426778
--- /dev/null
+++ b/native/webview/plat_support/graphic_buffer_impl.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Provides the implementation of the GraphicBuffer interface in
+// renderer compostior
+
+#include "graphic_buffer_impl.h"
+
+#include <utils/Errors.h>
+
+namespace android {
+
+GraphicBufferImpl::GraphicBufferImpl(uint32_t w, uint32_t h)
+ : mBuffer(new android::GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888,
+ android::GraphicBuffer::USAGE_HW_TEXTURE |
+ android::GraphicBuffer::USAGE_SW_READ_OFTEN |
+ android::GraphicBuffer::USAGE_SW_WRITE_OFTEN)) {
+}
+
+GraphicBufferImpl::~GraphicBufferImpl() {
+}
+
+// static
+long GraphicBufferImpl::Create(int w, int h) {
+ GraphicBufferImpl* buffer = new GraphicBufferImpl(
+ static_cast<uint32_t>(w), static_cast<uint32_t>(h));
+ if (buffer->InitCheck() != NO_ERROR) {
+ delete buffer;
+ return 0;
+ }
+ return reinterpret_cast<intptr_t>(buffer);
+}
+
+// static
+void GraphicBufferImpl::Release(long buffer_id) {
+ GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id);
+ delete buffer;
+}
+
+// static
+int GraphicBufferImpl::MapStatic(long buffer_id, AwMapMode mode, void** vaddr) {
+ GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id);
+ return buffer->Map(mode, vaddr);
+}
+
+// static
+int GraphicBufferImpl::UnmapStatic(long buffer_id) {
+ GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id);
+ return buffer->Unmap();
+}
+
+// static
+void* GraphicBufferImpl::GetNativeBufferStatic(long buffer_id) {
+ GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id);
+ return buffer->GetNativeBuffer();
+}
+
+// static
+uint32_t GraphicBufferImpl::GetStrideStatic(long buffer_id) {
+ GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id);
+ return buffer->GetStride();
+}
+
+status_t GraphicBufferImpl::Map(AwMapMode mode, void** vaddr) {
+ int usage = 0;
+ switch (mode) {
+ case MAP_READ_ONLY:
+ usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN;
+ break;
+ case MAP_WRITE_ONLY:
+ usage = android::GraphicBuffer::USAGE_SW_WRITE_OFTEN;
+ break;
+ case MAP_READ_WRITE:
+ usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN |
+ android::GraphicBuffer::USAGE_SW_WRITE_OFTEN;
+ break;
+ default:
+ return INVALID_OPERATION;
+ }
+ return mBuffer->lock(usage, vaddr);
+}
+
+status_t GraphicBufferImpl::Unmap() {
+ return mBuffer->unlock();
+}
+
+status_t GraphicBufferImpl::InitCheck() const {
+ return mBuffer->initCheck();
+}
+
+void* GraphicBufferImpl::GetNativeBuffer() const {
+ return mBuffer->getNativeBuffer();
+}
+
+uint32_t GraphicBufferImpl::GetStride() const {
+ static const int kBytesPerPixel = 4;
+ return mBuffer->getStride() * kBytesPerPixel;
+}
+
+} // namespace android
diff --git a/native/webview/plat_support/graphic_buffer_impl.h b/native/webview/plat_support/graphic_buffer_impl.h
new file mode 100644
index 0000000..442710a
--- /dev/null
+++ b/native/webview/plat_support/graphic_buffer_impl.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Provides the implementation of the GraphicBuffer interface in
+// renderer compostior
+
+#ifndef ANDROID_GRAPHIC_BUFFER_IMPL_H
+#define ANDROID_GRAPHIC_BUFFER_IMPL_H
+
+#include <ui/GraphicBuffer.h>
+
+#include "draw_gl.h"
+
+namespace android {
+
+class GraphicBufferImpl {
+ public:
+ ~GraphicBufferImpl();
+
+ static long Create(int w, int h);
+ static void Release(long buffer_id);
+ static int MapStatic(long buffer_id, AwMapMode mode, void** vaddr);
+ static int UnmapStatic(long buffer_id);
+ static void* GetNativeBufferStatic(long buffer_id);
+ static uint32_t GetStrideStatic(long buffer_id);
+
+ private:
+ status_t Map(AwMapMode mode, void** vaddr);
+ status_t Unmap();
+ status_t InitCheck() const;
+ void* GetNativeBuffer() const;
+ uint32_t GetStride() const;
+ GraphicBufferImpl(uint32_t w, uint32_t h);
+
+ sp<android::GraphicBuffer> mBuffer;
+};
+
+} // namespace android
+
+#endif // ANDROID_GRAPHIC_BUFFER_IMPL_H
diff --git a/native/webview/plat_support/graphics_utils.cpp b/native/webview/plat_support/graphics_utils.cpp
new file mode 100644
index 0000000..89beb75
--- /dev/null
+++ b/native/webview/plat_support/graphics_utils.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Provides a webviewchromium glue layer adapter from the internal Android
+// graphics types into the types the chromium stack expects, and back.
+
+#define LOG_TAG "webviewchromium_plat_support"
+
+#include "draw_gl.h"
+#include "draw_sw.h"
+
+#include <cstdlib>
+#include <jni.h>
+#include <utils/Log.h>
+#include "graphic_buffer_impl.h"
+#include "GraphicsJNI.h"
+#include "SkCanvasStateUtils.h"
+#include "SkGraphics.h"
+#include "SkPicture.h"
+
+#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
+
+namespace android {
+namespace {
+
+class PixelInfo : public AwPixelInfo {
+ public:
+ explicit PixelInfo(android::Canvas* canvas);
+ ~PixelInfo();
+};
+
+
+PixelInfo::PixelInfo(android::Canvas* canvas) {
+ memset(this, 0, sizeof(AwPixelInfo));
+ version = kAwPixelInfoVersion;
+ state = canvas->captureCanvasState();
+}
+
+PixelInfo::~PixelInfo() {
+ if (state)
+ SkCanvasStateUtils::ReleaseCanvasState(state);
+}
+
+AwPixelInfo* GetPixels(JNIEnv* env, jobject java_canvas) {
+ android::Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, java_canvas);
+ if (!nativeCanvas)
+ return NULL;
+
+ PixelInfo* pixels = new PixelInfo(nativeCanvas);
+ if (!pixels->state) {
+ delete pixels;
+ pixels = NULL;
+ }
+ return pixels;
+}
+
+void ReleasePixels(AwPixelInfo* pixels) {
+ delete static_cast<PixelInfo*>(pixels);
+}
+
+jlong GetDrawSWFunctionTable(JNIEnv* env, jclass) {
+ static AwDrawSWFunctionTable function_table;
+ function_table.version = kAwDrawSWFunctionTableVersion;
+ function_table.access_pixels = &GetPixels;
+ function_table.release_pixels = &ReleasePixels;
+ return reinterpret_cast<intptr_t>(&function_table);
+}
+
+jlong GetDrawGLFunctionTable(JNIEnv* env, jclass) {
+ static AwDrawGLFunctionTable function_table;
+ function_table.version = kAwDrawGLFunctionTableVersion;
+ function_table.create_graphic_buffer = &GraphicBufferImpl::Create;
+ function_table.release_graphic_buffer = &GraphicBufferImpl::Release;
+ function_table.map = &GraphicBufferImpl::MapStatic;
+ function_table.unmap = &GraphicBufferImpl::UnmapStatic;
+ function_table.get_native_buffer = &GraphicBufferImpl::GetNativeBufferStatic;
+ function_table.get_stride = &GraphicBufferImpl::GetStrideStatic;
+ return reinterpret_cast<intptr_t>(&function_table);
+}
+
+const char kClassName[] = "com/android/webview/chromium/GraphicsUtils";
+const JNINativeMethod kJniMethods[] = {
+ { "nativeGetDrawSWFunctionTable", "()J",
+ reinterpret_cast<void*>(GetDrawSWFunctionTable) },
+ { "nativeGetDrawGLFunctionTable", "()J",
+ reinterpret_cast<void*>(GetDrawGLFunctionTable) },
+};
+
+} // namespace
+
+void RegisterGraphicsUtils(JNIEnv* env) {
+ jclass clazz = env->FindClass(kClassName);
+ LOG_ALWAYS_FATAL_IF(!clazz, "Unable to find class '%s'", kClassName);
+
+ int res = env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
+ LOG_ALWAYS_FATAL_IF(res < 0, "register native methods failed: res=%d", res);
+}
+
+} // namespace android
diff --git a/native/webview/plat_support/jni_entry_point.cpp b/native/webview/plat_support/jni_entry_point.cpp
new file mode 100644
index 0000000..4771be1
--- /dev/null
+++ b/native/webview/plat_support/jni_entry_point.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "webviewchromium_plat_support"
+
+#include <jni.h>
+#include <utils/Log.h>
+
+namespace android {
+
+void RegisterDrawGLFunctor(JNIEnv* env);
+void RegisterGraphicsUtils(JNIEnv* env);
+
+} // namespace android
+
+JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
+ JNIEnv* env = NULL;
+ jint ret = vm->AttachCurrentThread(&env, NULL);
+ LOG_ALWAYS_FATAL_IF(ret != JNI_OK, "AttachCurrentThread failed");
+ android::RegisterDrawGLFunctor(env);
+ android::RegisterGraphicsUtils(env);
+
+ return JNI_VERSION_1_4;
+}