| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "android_webview/browser/hardware_renderer_legacy.h" | |
| 6 | |
| 7 #include "android_webview/browser/aw_gl_surface.h" | |
| 8 #include "android_webview/browser/shared_renderer_state.h" | |
| 9 #include "android_webview/public/browser/draw_gl.h" | |
| 10 #include "base/debug/trace_event.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "cc/output/compositor_frame.h" | |
| 13 #include "content/public/browser/android/synchronous_compositor.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "ui/gfx/geometry/rect_conversions.h" | |
| 16 #include "ui/gfx/geometry/rect_f.h" | |
| 17 #include "ui/gfx/transform.h" | |
| 18 #include "ui/gl/gl_bindings.h" | |
| 19 | |
| 20 namespace android_webview { | |
| 21 | |
| 22 HardwareRendererLegacy::HardwareRendererLegacy(SharedRendererState* state) | |
| 23 : shared_renderer_state_(state), last_egl_context_(eglGetCurrentContext()) { | |
| 24 DCHECK(last_egl_context_); | |
| 25 | |
| 26 gl_surface_ = new AwGLSurface; | |
| 27 bool success = | |
| 28 shared_renderer_state_->GetCompositor()->InitializeHwDraw(gl_surface_); | |
| 29 DCHECK(success); | |
| 30 } | |
| 31 | |
| 32 HardwareRendererLegacy::~HardwareRendererLegacy() { | |
| 33 draw_gl_input_ = shared_renderer_state_->PassDrawGLInput(); | |
| 34 shared_renderer_state_->GetCompositor()->ReleaseHwDraw(); | |
| 35 gl_surface_ = NULL; | |
| 36 } | |
| 37 | |
| 38 bool HardwareRendererLegacy::DrawGL(bool stencil_enabled, | |
| 39 int framebuffer_binding_ext, | |
| 40 AwDrawGLInfo* draw_info, | |
| 41 DrawGLResult* result) { | |
| 42 TRACE_EVENT0("android_webview", "HardwareRendererLegacy::DrawGL"); | |
| 43 | |
| 44 // We need to watch if the current Android context has changed and enforce | |
| 45 // a clean-up in the compositor. | |
| 46 EGLContext current_context = eglGetCurrentContext(); | |
| 47 if (!current_context) { | |
| 48 DLOG(ERROR) << "DrawGL called without EGLContext"; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 // TODO(boliu): Handle context loss. | |
| 53 if (last_egl_context_ != current_context) | |
| 54 DLOG(WARNING) << "EGLContextChanged"; | |
| 55 | |
| 56 // Should only need to access SharedRendererState in kModeDraw and kModeSync. | |
| 57 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput(); | |
| 58 if (input.get()) | |
| 59 draw_gl_input_ = input.Pass(); | |
| 60 SetCompositorMemoryPolicy(); | |
| 61 | |
| 62 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); | |
| 63 | |
| 64 gfx::Transform transform; | |
| 65 transform.matrix().setColMajorf(draw_info->transform); | |
| 66 transform.Translate(draw_gl_input_->scroll_offset.x(), | |
| 67 draw_gl_input_->scroll_offset.y()); | |
| 68 gfx::Rect clip_rect(draw_info->clip_left, | |
| 69 draw_info->clip_top, | |
| 70 draw_info->clip_right - draw_info->clip_left, | |
| 71 draw_info->clip_bottom - draw_info->clip_top); | |
| 72 | |
| 73 gfx::Rect viewport(draw_info->width, draw_info->height); | |
| 74 if (!draw_info->is_layer) { | |
| 75 gfx::RectF view_rect(draw_gl_input_->width, draw_gl_input_->height); | |
| 76 transform.TransformRect(&view_rect); | |
| 77 viewport.Intersect(gfx::ToEnclosingRect(view_rect)); | |
| 78 } | |
| 79 | |
| 80 scoped_ptr<cc::CompositorFrame> frame = | |
| 81 shared_renderer_state_->GetCompositor()->DemandDrawHw( | |
| 82 gfx::Size(draw_info->width, draw_info->height), | |
| 83 transform, | |
| 84 viewport, | |
| 85 clip_rect, | |
| 86 framebuffer_binding_ext); | |
| 87 gl_surface_->ResetBackingFrameBufferObject(); | |
| 88 | |
| 89 if (frame.get()) { | |
| 90 result->clip_contains_visible_rect = | |
| 91 clip_rect.Contains(draw_gl_input_->global_visible_rect); | |
| 92 } | |
| 93 return !!frame.get(); | |
| 94 } | |
| 95 | |
| 96 void HardwareRendererLegacy::SetCompositorMemoryPolicy() { | |
| 97 if (shared_renderer_state_->IsMemoryPolicyDirty()) { | |
| 98 content::SynchronousCompositorMemoryPolicy policy = | |
| 99 shared_renderer_state_->GetMemoryPolicy(); | |
| 100 // Memory policy is set by BrowserViewRenderer on UI thread. | |
| 101 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(policy); | |
| 102 shared_renderer_state_->SetMemoryPolicyDirty(false); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 } // namespace android_webview | |
| OLD | NEW |