Skip to content

Commit

Permalink
Define public inteface for accessing Canvas pixels
Browse files Browse the repository at this point in the history
The embedder can supply implementations of the declared functions, allowing
the software draw path to have direct access to the pixels underlying
the java Canvas object from native code.

BUG=


Review URL: https://chromiumcodereview.appspot.com/11665020

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174592 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
joth@chromium.org committed Dec 25, 2012
1 parent 931989f commit 441c33c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.http.SslCertificate;
import android.os.AsyncTask;
Expand Down Expand Up @@ -330,6 +331,10 @@ public void destroy() {
mNativeAwContents = 0;
}

public static void setAwDrawSWFunctionTable(int functionTablePointer) {
nativeSetAwDrawSWFunctionTable(functionTablePointer);
}

public static int getAwDrawGLFunction() {
return nativeGetAwDrawGLFunction();
}
Expand All @@ -349,8 +354,11 @@ public boolean onPrepareDrawGL(Canvas canvas) {
}

public void onDraw(Canvas canvas) {
// TODO(joth): Implement. For now, just clear the canvas to red.
canvas.drawRGB(200, 1, 4);
if (!nativeDrawSW(mNativeAwContents, canvas)) {
Log.w(TAG, "Native DrawSW failed; clearing to background color.");
int c = mContentViewCore.getBackgroundColor();
canvas.drawRGB(Color.red(c), Color.green(c), Color.blue(c));
}
}

public int findAllSync(String searchString) {
Expand Down Expand Up @@ -929,6 +937,7 @@ private void handleJsPrompt(String url, String message, String defaultValue,
private native int nativeInit(AwWebContentsDelegate webViewWebContentsDelegate,
boolean privateBrowsing);
private static native void nativeDestroy(int nativeAwContents);
private static native void nativeSetAwDrawSWFunctionTable(int functionTablePointer);
private static native int nativeGetAwDrawGLFunction();

private native int nativeGetWebContents(int nativeAwContents);
Expand All @@ -944,6 +953,7 @@ private native void nativeSetIoThreadClient(int nativeAwContents,
private native void nativeSetInterceptNavigationDelegate(int nativeAwContents,
InterceptNavigationDelegate navigationInterceptionDelegate);

private native boolean nativeDrawSW(int nativeAwContents, Canvas canvas);
private native void nativeSetScrollForHWFrame(int nativeAwContents, int scrollX, int scrollY);
private native int nativeFindAllSync(int nativeAwContents, String searchString);
private native void nativeFindAllAsync(int nativeAwContents, String searchString);
Expand Down
3 changes: 3 additions & 0 deletions android_webview/native/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ include_rules = [
"+content/public/browser",
"+ui/gfx",
"+ui/gl",

# TODO(joth): Remove if we can move the compositor driver into ../browser/
"+third_party/skia/include",
]
49 changes: 49 additions & 0 deletions android_webview/native/aw_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "android_webview/native/aw_contents_io_thread_client_impl.h"
#include "android_webview/native/aw_web_contents_delegate.h"
#include "android_webview/native/state_serializer.h"
#include "android_webview/public/browser/draw_sw.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
Expand All @@ -35,6 +36,9 @@
#include "content/public/common/ssl_status.h"
#include "jni/AwContents_jni.h"
#include "net/base/x509_certificate.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkDevice.h"
#include "ui/gfx/transform.h"
#include "ui/gl/gl_bindings.h"

Expand Down Expand Up @@ -78,6 +82,8 @@ namespace android_webview {

namespace {

AwDrawSWFunctionTable* g_draw_sw_functions = NULL;

const void* kAwContentsUserDataKey = &kAwContentsUserDataKey;

class AwContentsUserData : public base::SupportsUserData::Data {
Expand Down Expand Up @@ -410,6 +416,43 @@ void AwContents::DrawGL(AwDrawGLInfo* draw_info) {
// ---------------------------------------------------------------------------
}

bool AwContents::DrawSW(JNIEnv* env, jobject obj, jobject java_canvas) {
AwPixelInfo* pixels;
if (!g_draw_sw_functions ||
(pixels = g_draw_sw_functions->access_pixels(env, java_canvas)) == NULL) {
// TODO(joth): Fall back to slow path rendering via temporary bitmap.
return false;
}

{
SkBitmap bitmap;
bitmap.setConfig(static_cast<SkBitmap::Config>(pixels->config),
pixels->width,
pixels->height,
pixels->row_bytes);
bitmap.setPixels(pixels->pixels);
SkDevice device(bitmap);
SkCanvas canvas(&device);
SkMatrix matrix;
for (int i = 0; i < 9; i++) {
matrix.set(i, pixels->matrix[i]);
}
canvas.setMatrix(matrix);
SkRegion clip;
if (pixels->clip_region_size) {
size_t bytes_read = clip.readFromMemory(pixels->clip_region);
DCHECK_EQ(pixels->clip_region_size, bytes_read);
canvas.setClipRegion(clip);
}

// TODO(joth): Implement real drawing. For now, fill the view in blue.
canvas.drawARGB(128, 1, 30, 250);
}

g_draw_sw_functions->release_pixels(pixels);
return true;
}

jint AwContents::GetWebContents(JNIEnv* env, jobject obj) {
return reinterpret_cast<jint>(web_contents_.get());
}
Expand Down Expand Up @@ -446,6 +489,12 @@ void AwContents::Destroy(JNIEnv* env, jobject obj) {
delete this;
}

// static
void SetAwDrawSWFunctionTable(JNIEnv* env, jclass, jint function_table) {
g_draw_sw_functions =
reinterpret_cast<AwDrawSWFunctionTable*>(function_table);
}

// static
jint GetAwDrawGLFunction(JNIEnv* env, jclass) {
return reinterpret_cast<jint>(&DrawGLFunction);
Expand Down
1 change: 1 addition & 0 deletions android_webview/native/aw_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AwContents : public FindHelper::Listener,
virtual ~AwContents();

void DrawGL(AwDrawGLInfo* draw_info);
bool DrawSW(JNIEnv* env, jobject obj, jobject canvas);

void RunJavaScriptDialog(
content::JavaScriptMessageType message_type,
Expand Down
44 changes: 44 additions & 0 deletions android_webview/public/browser/draw_sw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 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

// Holds the information required to implement the SW draw to system canvas.
struct AwPixelInfo {
int config; // In SkBitmap::Config format.
int width; // In pixels.
int height; // In pixels.
int row_bytes; // Number of bytes from start of one line to next.
void* pixels; // The pixels, all (height * row_bytes) of them.
float matrix[9]; // The matrix currently in effect on the canvas.
void* clip_region; // Flattened clip region.
size_t clip_region_size; // Number of bytes in |clip_region|.
};

// 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);

// "vtable" for the functions declared in this file. An instance must be set via
// AwContents.setAwDrawSWFunctionTable
struct AwDrawSWFunctionTable {
AwAccessPixelsFunction* access_pixels;
AwReleasePixelsFunction* release_pixels;
};

#endif // ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_SW_H_

0 comments on commit 441c33c

Please sign in to comment.