Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: content/browser/renderer_host/render_widget_host_view_android.cc

Issue 11234008: Enable texture readback support for Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/render_widget_host_view_android.h" 5 #include "content/browser/renderer_host/render_widget_host_view_android.h"
6 6
7 #include <android/bitmap.h>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/message_loop.h" 11 #include "base/message_loop.h"
10 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
11 #include "content/browser/android/content_view_core_impl.h" 13 #include "content/browser/android/content_view_core_impl.h"
12 #include "content/browser/android/draw_delegate_impl.h" 14 #include "content/browser/android/draw_delegate_impl.h"
13 #include "content/browser/gpu/gpu_surface_tracker.h" 15 #include "content/browser/gpu/gpu_surface_tracker.h"
14 #include "content/browser/renderer_host/compositor_impl_android.h" 16 #include "content/browser/renderer_host/compositor_impl_android.h"
15 #include "content/browser/renderer_host/image_transport_factory_android.h" 17 #include "content/browser/renderer_host/image_transport_factory_android.h"
16 #include "content/browser/renderer_host/render_widget_host_impl.h" 18 #include "content/browser/renderer_host/render_widget_host_impl.h"
17 #include "content/common/android/device_info.h" 19 #include "content/common/android/device_info.h"
20 #include "content/common/gpu/client/gl_helper.h"
18 #include "content/common/gpu/gpu_messages.h" 21 #include "content/common/gpu/gpu_messages.h"
19 #include "content/common/view_messages.h" 22 #include "content/common/view_messages.h"
23 #include "third_party/khronos/GLES2/gl2.h"
24 #include "third_party/khronos/GLES2/gl2ext.h"
20 #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h" 25 #include "third_party/WebKit/Source/Platform/chromium/public/Platform.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorSuppor t.h" 26 #include "third_party/WebKit/Source/Platform/chromium/public/WebCompositorSuppor t.h"
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" 27 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
28 #include "ui/gfx/android/java_bitmap.h"
23 29
24 namespace content { 30 namespace content {
25 31
26 namespace { 32 namespace {
27 33
28 // TODO(pliard): http://crbug.com/142585. Remove this helper function and update 34 // TODO(pliard): http://crbug.com/142585. Remove this helper function and update
29 // the clients to deal directly with WebKit::WebTextDirection. 35 // the clients to deal directly with WebKit::WebTextDirection.
30 base::i18n::TextDirection ConvertTextDirection(WebKit::WebTextDirection dir) { 36 base::i18n::TextDirection ConvertTextDirection(WebKit::WebTextDirection dir) {
31 switch (dir) { 37 switch (dir) {
32 case WebKit::WebTextDirectionDefault: return base::i18n::UNKNOWN_DIRECTION; 38 case WebKit::WebTextDirectionDefault: return base::i18n::UNKNOWN_DIRECTION;
33 case WebKit::WebTextDirectionLeftToRight: return base::i18n::LEFT_TO_RIGHT; 39 case WebKit::WebTextDirectionLeftToRight: return base::i18n::LEFT_TO_RIGHT;
34 case WebKit::WebTextDirectionRightToLeft: return base::i18n::RIGHT_TO_LEFT; 40 case WebKit::WebTextDirectionRightToLeft: return base::i18n::RIGHT_TO_LEFT;
35 } 41 }
36 NOTREACHED() << "Unsupported text direction " << dir; 42 NOTREACHED() << "Unsupported text direction " << dir;
37 return base::i18n::UNKNOWN_DIRECTION; 43 return base::i18n::UNKNOWN_DIRECTION;
38 } 44 }
39 45
40 } // namespace 46 } // namespace
41 47
42 RenderWidgetHostViewAndroid::RenderWidgetHostViewAndroid( 48 RenderWidgetHostViewAndroid::RenderWidgetHostViewAndroid(
43 RenderWidgetHostImpl* widget_host, 49 RenderWidgetHostImpl* widget_host,
44 ContentViewCoreImpl* content_view_core) 50 ContentViewCoreImpl* content_view_core)
45 : host_(widget_host), 51 : host_(widget_host),
46 // ContentViewCoreImpl represents the native side of the Java 52 // ContentViewCoreImpl represents the native side of the Java
47 // ContentViewCore. It being NULL means that it is not attached to the 53 // ContentViewCore. It being NULL means that it is not attached to the
48 // View system yet, so we treat it as hidden. 54 // View system yet, so we treat it as hidden.
49 is_hidden_(!content_view_core), 55 is_hidden_(!content_view_core),
50 content_view_core_(content_view_core), 56 content_view_core_(content_view_core),
51 ime_adapter_android_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 57 ime_adapter_android_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
52 texture_layer_(WebKit::WebExternalTextureLayer::create()) { 58 texture_layer_(WebKit::WebExternalTextureLayer::create()),
59 texture_id_in_layer_(0) {
53 host_->SetView(this); 60 host_->SetView(this);
54 // RenderWidgetHost is initialized as visible. If is_hidden_ is true, tell 61 // RenderWidgetHost is initialized as visible. If is_hidden_ is true, tell
55 // RenderWidgetHost to hide. 62 // RenderWidgetHost to hide.
56 if (is_hidden_) 63 if (is_hidden_)
57 host_->WasHidden(); 64 host_->WasHidden();
58 texture_layer_->layer()->setDrawsContent(!is_hidden_); 65 texture_layer_->layer()->setDrawsContent(!is_hidden_);
59 host_->AttachLayer(texture_layer_->layer()); 66 host_->AttachLayer(texture_layer_->layer());
60 } 67 }
61 68
62 RenderWidgetHostViewAndroid::~RenderWidgetHostViewAndroid() { 69 RenderWidgetHostViewAndroid::~RenderWidgetHostViewAndroid() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 texture_layer_->layer()->setBounds(size); 124 texture_layer_->layer()->setBounds(size);
118 } 125 }
119 126
120 void RenderWidgetHostViewAndroid::SetBounds(const gfx::Rect& rect) { 127 void RenderWidgetHostViewAndroid::SetBounds(const gfx::Rect& rect) {
121 if (rect.origin().x() || rect.origin().y()) { 128 if (rect.origin().x() || rect.origin().y()) {
122 VLOG(0) << "SetBounds not implemented for (x,y)!=(0,0)"; 129 VLOG(0) << "SetBounds not implemented for (x,y)!=(0,0)";
123 } 130 }
124 SetSize(rect.size()); 131 SetSize(rect.size());
125 } 132 }
126 133
134 WebKit::WebGLId RenderWidgetHostViewAndroid::GetScaledContentTexture(
135 const gfx::Size& size) {
136 if (!CompositorImpl::IsInitialized() || texture_id_in_layer_ == 0)
137 return 0;
138
139 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
140 return helper->CopyAndScaleTexture(texture_id_in_layer_,
141 requested_size_,
142 size);
143 }
144
145 bool RenderWidgetHostViewAndroid::PopulateBitmapWithContents(jobject jbitmap) {
146 if (!CompositorImpl::IsInitialized() || texture_id_in_layer_ == 0)
147 return false;
148
149 gfx::JavaBitmap bitmap(jbitmap);
150
151 // TODO(dtrainor): Eventually add support for multiple formats here.
152 DCHECK(bitmap.format() == ANDROID_BITMAP_FORMAT_RGBA_8888);
153
154 WebKit::WebGLId texture = texture_id_in_layer_;
155
156 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
157
158 // If we're trying to read to a bitmap of a different size, we need to copy
159 // and scale the texture before we can read it back.
160 if (bitmap.size() != requested_size_) {
161 texture = helper->CopyAndScaleTexture(texture_id_in_layer_,
162 requested_size_,
163 bitmap.size());
164 if (texture == 0)
165 return false;
166 }
167
168 helper->ReadbackTextureSync(texture,
169 bitmap.size(),
170 static_cast<unsigned char*> (bitmap.pixels()));
171
172 if (texture != texture_id_in_layer_) {
173 // We created a temporary texture. We need to clean it up.
174 WebKit::WebGraphicsContext3D* context =
175 ImageTransportFactoryAndroid::GetInstance()->GetContext3D();
176 context->deleteTexture(texture);
177 }
178
179 return true;
180 }
181
127 gfx::NativeView RenderWidgetHostViewAndroid::GetNativeView() const { 182 gfx::NativeView RenderWidgetHostViewAndroid::GetNativeView() const {
128 return content_view_core_; 183 return content_view_core_;
129 } 184 }
130 185
131 gfx::NativeViewId RenderWidgetHostViewAndroid::GetNativeViewId() const { 186 gfx::NativeViewId RenderWidgetHostViewAndroid::GetNativeViewId() const {
132 return reinterpret_cast<gfx::NativeViewId>( 187 return reinterpret_cast<gfx::NativeViewId>(
133 const_cast<RenderWidgetHostViewAndroid*>(this)); 188 const_cast<RenderWidgetHostViewAndroid*>(this));
134 } 189 }
135 190
136 gfx::NativeViewAccessible 191 gfx::NativeViewAccessible
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 callback.Run(false); 358 callback.Run(false);
304 } 359 }
305 360
306 void RenderWidgetHostViewAndroid::OnAcceleratedCompositingStateChange() { 361 void RenderWidgetHostViewAndroid::OnAcceleratedCompositingStateChange() {
307 } 362 }
308 363
309 void RenderWidgetHostViewAndroid::AcceleratedSurfaceBuffersSwapped( 364 void RenderWidgetHostViewAndroid::AcceleratedSurfaceBuffersSwapped(
310 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params, 365 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
311 int gpu_host_id) { 366 int gpu_host_id) {
312 texture_layer_->setTextureId(params.surface_handle); 367 texture_layer_->setTextureId(params.surface_handle);
368 texture_id_in_layer_ = params.surface_handle;
313 texture_layer_->layer()->invalidate(); 369 texture_layer_->layer()->invalidate();
314 // TODO(sievers): The view and layer should get sized proactively. 370 // TODO(sievers): The view and layer should get sized proactively.
315 if (((gfx::Size)texture_layer_->layer()->bounds()).IsEmpty()) 371 if (((gfx::Size)texture_layer_->layer()->bounds()).IsEmpty())
316 texture_layer_->layer()->setBounds( 372 texture_layer_->layer()->setBounds(
317 DrawDelegateImpl::GetInstance()->GetBounds()); 373 DrawDelegateImpl::GetInstance()->GetBounds());
318 374
319 // TODO(sievers): When running the impl thread in the browser we 375 // TODO(sievers): When running the impl thread in the browser we
320 // need to delay the ACK until after commit. 376 // need to delay the ACK until after commit.
321 DCHECK(!WebKit::Platform::current()->compositorSupport()-> 377 DCHECK(!WebKit::Platform::current()->compositorSupport()->
322 isThreadingEnabled()); 378 isThreadingEnabled());
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 // RenderWidgetHostView, public: 566 // RenderWidgetHostView, public:
511 567
512 // static 568 // static
513 RenderWidgetHostView* 569 RenderWidgetHostView*
514 RenderWidgetHostView::CreateViewForWidget(RenderWidgetHost* widget) { 570 RenderWidgetHostView::CreateViewForWidget(RenderWidgetHost* widget) {
515 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(widget); 571 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(widget);
516 return new RenderWidgetHostViewAndroid(rwhi, NULL); 572 return new RenderWidgetHostViewAndroid(rwhi, NULL);
517 } 573 }
518 574
519 } // namespace content 575 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698