forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser_view_renderer.cc
918 lines (792 loc) · 32.2 KB
/
browser_view_renderer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
// Copyright 2014 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.
#include "android_webview/browser/browser_view_renderer.h"
#include "android_webview/browser/browser_view_renderer_client.h"
#include "android_webview/browser/shared_renderer_state.h"
#include "android_webview/common/aw_switches.h"
#include "android_webview/public/browser/draw_gl.h"
#include "base/android/jni_android.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "cc/output/compositor_frame.h"
#include "content/public/browser/android/synchronous_compositor.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "ui/gfx/vector2d_conversions.h"
using base::android::AttachCurrentThread;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
using content::BrowserThread;
using content::SynchronousCompositorMemoryPolicy;
namespace android_webview {
namespace {
const int64 kFallbackTickTimeoutInMilliseconds = 20;
// Used to calculate memory allocation. Determined experimentally.
const size_t kMemoryMultiplier = 10;
const size_t kBytesPerPixel = 4;
const size_t kMemoryAllocationStep = 5 * 1024 * 1024;
// Used to calculate tile allocation. Determined experimentally.
const size_t kTileMultiplier = 12;
const size_t kTileAllocationStep = 20;
// This will be set by static function CalculateTileMemoryPolicy() during init.
// See AwMainDelegate::BasicStartupComplete.
size_t g_tile_area;
class AutoResetWithLock {
public:
AutoResetWithLock(gfx::Vector2dF* scoped_variable,
gfx::Vector2dF new_value,
base::Lock& lock)
: scoped_variable_(scoped_variable),
original_value_(*scoped_variable),
lock_(lock) {
base::AutoLock auto_lock(lock_);
*scoped_variable_ = new_value;
}
~AutoResetWithLock() {
base::AutoLock auto_lock(lock_);
*scoped_variable_ = original_value_;
}
private:
gfx::Vector2dF* scoped_variable_;
gfx::Vector2dF original_value_;
base::Lock& lock_;
DISALLOW_COPY_AND_ASSIGN(AutoResetWithLock);
};
class TracedValue : public base::debug::ConvertableToTraceFormat {
public:
explicit TracedValue(base::Value* value) : value_(value) {}
static scoped_refptr<base::debug::ConvertableToTraceFormat> FromValue(
base::Value* value) {
return scoped_refptr<base::debug::ConvertableToTraceFormat>(
new TracedValue(value));
}
virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
std::string tmp;
base::JSONWriter::Write(value_.get(), &tmp);
*out += tmp;
}
private:
virtual ~TracedValue() {}
scoped_ptr<base::Value> value_;
DISALLOW_COPY_AND_ASSIGN(TracedValue);
};
} // namespace
// static
void BrowserViewRenderer::CalculateTileMemoryPolicy() {
CommandLine* cl = CommandLine::ForCurrentProcess();
const char kDefaultTileSize[] = "384";
if (!cl->HasSwitch(switches::kDefaultTileWidth))
cl->AppendSwitchASCII(switches::kDefaultTileWidth, kDefaultTileSize);
if (!cl->HasSwitch(switches::kDefaultTileHeight))
cl->AppendSwitchASCII(switches::kDefaultTileHeight, kDefaultTileSize);
size_t tile_size;
base::StringToSizeT(kDefaultTileSize, &tile_size);
g_tile_area = tile_size * tile_size;
}
BrowserViewRenderer::BrowserViewRenderer(
BrowserViewRendererClient* client,
SharedRendererState* shared_renderer_state,
content::WebContents* web_contents,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
: client_(client),
shared_renderer_state_(shared_renderer_state),
web_contents_(web_contents),
weak_factory_on_ui_thread_(this),
ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()),
ui_task_runner_(ui_task_runner),
has_compositor_(false),
is_paused_(false),
view_visible_(false),
window_visible_(false),
attached_to_window_(false),
hardware_enabled_(false),
dip_scale_(0.0),
page_scale_factor_(1.0),
on_new_picture_enable_(false),
clear_view_(false),
compositor_needs_continuous_invalidate_(false),
block_invalidates_(false),
width_(0),
height_(0),
num_tiles_(0u),
num_bytes_(0u) {
CHECK(web_contents_);
content::SynchronousCompositor::SetClientForWebContents(web_contents_, this);
// Currently the logic in this class relies on |has_compositor_| remaining
// false until the DidInitializeCompositor() call, hence it is not set here.
}
BrowserViewRenderer::~BrowserViewRenderer() {
content::SynchronousCompositor::SetClientForWebContents(web_contents_, NULL);
// OnDetachedFromWindow should be called before the destructor, so the memory
// policy should have already been updated.
}
// This function updates the cached memory policy in shared renderer state, as
// well as the tile resource allocation in GlobalTileManager.
void BrowserViewRenderer::TrimMemory(const int level, const bool visible) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Constants from Android ComponentCallbacks2.
enum {
TRIM_MEMORY_RUNNING_LOW = 10,
TRIM_MEMORY_UI_HIDDEN = 20,
TRIM_MEMORY_BACKGROUND = 40,
};
// Not urgent enough. TRIM_MEMORY_UI_HIDDEN is treated specially because
// it does not indicate memory pressure, but merely that the app is
// backgrounded.
if (level < TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_UI_HIDDEN)
return;
// Do not release resources on view we expect to get DrawGL soon.
if (level < TRIM_MEMORY_BACKGROUND && visible)
return;
// Just set the memory limit to 0 and drop all tiles. This will be reset to
// normal levels in the next DrawGL call.
SynchronousCompositorMemoryPolicy zero_policy;
if (shared_renderer_state_->GetMemoryPolicy() == zero_policy)
return;
TRACE_EVENT0("android_webview", "BrowserViewRenderer::TrimMemory");
RequestMemoryPolicy(zero_policy);
EnforceMemoryPolicyImmediately(zero_policy);
}
SynchronousCompositorMemoryPolicy
BrowserViewRenderer::CalculateDesiredMemoryPolicy() {
SynchronousCompositorMemoryPolicy policy;
size_t width = last_on_draw_global_visible_rect_.width();
size_t height = last_on_draw_global_visible_rect_.height();
policy.bytes_limit = kMemoryMultiplier * kBytesPerPixel * width * height;
// Round up to a multiple of kMemoryAllocationStep.
policy.bytes_limit =
(policy.bytes_limit / kMemoryAllocationStep + 1) * kMemoryAllocationStep;
size_t tiles = width * height * kTileMultiplier / g_tile_area;
// Round up to a multiple of kTileAllocationStep. The minimum number of tiles
// is also kTileAllocationStep.
tiles = (tiles / kTileAllocationStep + 1) * kTileAllocationStep;
policy.num_resources_limit = tiles;
return policy;
}
// This function updates the cached memory policy in shared renderer state, as
// well as the tile resource allocation in GlobalTileManager.
void BrowserViewRenderer::RequestMemoryPolicy(
SynchronousCompositorMemoryPolicy& new_policy) {
// This will be used in SetNumTiles.
num_bytes_ = new_policy.bytes_limit;
GlobalTileManager* manager = GlobalTileManager::GetInstance();
// The following line will call BrowserViewRenderer::SetTilesNum().
manager->RequestTiles(new_policy.num_resources_limit, tile_manager_key_);
}
void BrowserViewRenderer::SetNumTiles(size_t num_tiles,
bool effective_immediately) {
if (num_tiles == num_tiles_)
return;
num_tiles_ = num_tiles;
SynchronousCompositorMemoryPolicy new_policy;
new_policy.num_resources_limit = num_tiles_;
new_policy.bytes_limit = num_bytes_;
shared_renderer_state_->SetMemoryPolicy(new_policy);
if (effective_immediately)
EnforceMemoryPolicyImmediately(new_policy);
}
void BrowserViewRenderer::EnforceMemoryPolicyImmediately(
SynchronousCompositorMemoryPolicy new_policy) {
shared_renderer_state_->GetCompositor()->SetMemoryPolicy(new_policy);
ForceFakeCompositeSW();
shared_renderer_state_->SetMemoryPolicyDirty(false);
}
size_t BrowserViewRenderer::GetNumTiles() const {
return shared_renderer_state_->GetMemoryPolicy().num_resources_limit;
}
bool BrowserViewRenderer::OnDraw(jobject java_canvas,
bool is_hardware_canvas,
const gfx::Vector2d& scroll,
const gfx::Rect& global_visible_rect,
const gfx::Rect& clip) {
last_on_draw_scroll_offset_ = scroll;
last_on_draw_global_visible_rect_ = global_visible_rect;
if (clear_view_)
return false;
if (is_hardware_canvas && attached_to_window_) {
if (switches::UbercompEnabled()) {
return OnDrawHardware(java_canvas);
} else {
return OnDrawHardwareLegacy(java_canvas);
}
}
// Perform a software draw
return DrawSWInternal(java_canvas, clip);
}
bool BrowserViewRenderer::OnDrawHardwareLegacy(jobject java_canvas) {
scoped_ptr<DrawGLInput> draw_gl_input(new DrawGLInput);
draw_gl_input->scroll_offset = last_on_draw_scroll_offset_;
draw_gl_input->global_visible_rect = last_on_draw_global_visible_rect_;
draw_gl_input->width = width_;
draw_gl_input->height = height_;
SynchronousCompositorMemoryPolicy old_policy =
shared_renderer_state_->GetMemoryPolicy();
SynchronousCompositorMemoryPolicy new_policy = CalculateDesiredMemoryPolicy();
RequestMemoryPolicy(new_policy);
// We should be performing a hardware draw here. If we don't have the
// compositor yet or if RequestDrawGL fails, it means we failed this draw
// and thus return false here to clear to background color for this draw.
bool did_draw_gl =
has_compositor_ && client_->RequestDrawGL(java_canvas, false);
if (did_draw_gl) {
GlobalTileManager::GetInstance()->DidUse(tile_manager_key_);
shared_renderer_state_->SetDrawGLInput(draw_gl_input.Pass());
} else {
RequestMemoryPolicy(old_policy);
}
return did_draw_gl;
}
void BrowserViewRenderer::DidDrawGL(scoped_ptr<DrawGLResult> result) {
DidComposite(!result->clip_contains_visible_rect);
}
bool BrowserViewRenderer::OnDrawHardware(jobject java_canvas) {
if (!has_compositor_)
return false;
if (!hardware_enabled_) {
hardware_enabled_ =
shared_renderer_state_->GetCompositor()->InitializeHwDraw(NULL);
if (hardware_enabled_) {
gpu::GLInProcessContext* share_context =
shared_renderer_state_->GetCompositor()->GetShareContext();
DCHECK(share_context);
shared_renderer_state_->SetSharedContext(share_context);
}
}
if (!hardware_enabled_)
return false;
ReturnResources();
SynchronousCompositorMemoryPolicy new_policy = CalculateDesiredMemoryPolicy();
RequestMemoryPolicy(new_policy);
shared_renderer_state_->GetCompositor()->SetMemoryPolicy(
shared_renderer_state_->GetMemoryPolicy());
scoped_ptr<DrawGLInput> draw_gl_input(new DrawGLInput);
draw_gl_input->scroll_offset = last_on_draw_scroll_offset_;
draw_gl_input->global_visible_rect = last_on_draw_global_visible_rect_;
draw_gl_input->width = width_;
draw_gl_input->height = height_;
gfx::Transform transform;
gfx::Size surface_size(width_, height_);
gfx::Rect viewport(surface_size);
// TODO(boliu): Should really be |last_on_draw_global_visible_rect_|.
// See crbug.com/372073.
gfx::Rect clip = viewport;
bool stencil_enabled = false;
scoped_ptr<cc::CompositorFrame> frame =
shared_renderer_state_->GetCompositor()->DemandDrawHw(
surface_size, transform, viewport, clip, stencil_enabled);
if (!frame.get())
return false;
GlobalTileManager::GetInstance()->DidUse(tile_manager_key_);
frame->AssignTo(&draw_gl_input->frame);
scoped_ptr<DrawGLInput> old_input = shared_renderer_state_->PassDrawGLInput();
if (old_input.get()) {
shared_renderer_state_->ReturnResources(
old_input->frame.delegated_frame_data->resource_list);
}
shared_renderer_state_->SetDrawGLInput(draw_gl_input.Pass());
DidComposite(false);
bool did_request = client_->RequestDrawGL(java_canvas, false);
if (did_request)
return true;
ReturnResources();
return false;
}
void BrowserViewRenderer::DidDrawDelegated(scoped_ptr<DrawGLResult> result) {
if (!ui_task_runner_->BelongsToCurrentThread()) {
// TODO(boliu): This should be a cancelable callback.
ui_task_runner_->PostTask(FROM_HERE,
base::Bind(&BrowserViewRenderer::DidDrawDelegated,
ui_thread_weak_ptr_,
base::Passed(&result)));
return;
}
ReturnResources();
}
void BrowserViewRenderer::ReturnResources() {
cc::CompositorFrameAck frame_ack;
shared_renderer_state_->SwapReturnedResources(&frame_ack.resources);
if (!frame_ack.resources.empty()) {
shared_renderer_state_->GetCompositor()->ReturnResources(frame_ack);
}
}
bool BrowserViewRenderer::DrawSWInternal(jobject java_canvas,
const gfx::Rect& clip) {
if (clip.IsEmpty()) {
TRACE_EVENT_INSTANT0(
"android_webview", "EarlyOut_EmptyClip", TRACE_EVENT_SCOPE_THREAD);
return true;
}
if (!has_compositor_) {
TRACE_EVENT_INSTANT0(
"android_webview", "EarlyOut_NoCompositor", TRACE_EVENT_SCOPE_THREAD);
return false;
}
return BrowserViewRendererJavaHelper::GetInstance()
->RenderViaAuxilaryBitmapIfNeeded(
java_canvas,
last_on_draw_scroll_offset_,
clip,
base::Bind(&BrowserViewRenderer::CompositeSW,
base::Unretained(this)));
}
skia::RefPtr<SkPicture> BrowserViewRenderer::CapturePicture(int width,
int height) {
TRACE_EVENT0("android_webview", "BrowserViewRenderer::CapturePicture");
// Return empty Picture objects for empty SkPictures.
if (width <= 0 || height <= 0) {
return skia::AdoptRef(new SkPicture);
}
// Reset scroll back to the origin, will go back to the old
// value when scroll_reset is out of scope.
AutoResetWithLock scroll_reset(
&scroll_offset_dip_, gfx::Vector2dF(), render_thread_lock_);
SkPictureRecorder recorder;
SkCanvas* rec_canvas = recorder.beginRecording(width, height, NULL, 0);
if (has_compositor_)
CompositeSW(rec_canvas);
return skia::AdoptRef(recorder.endRecording());
}
void BrowserViewRenderer::EnableOnNewPicture(bool enabled) {
on_new_picture_enable_ = enabled;
}
void BrowserViewRenderer::ClearView() {
TRACE_EVENT_INSTANT0("android_webview",
"BrowserViewRenderer::ClearView",
TRACE_EVENT_SCOPE_THREAD);
if (clear_view_)
return;
clear_view_ = true;
// Always invalidate ignoring the compositor to actually clear the webview.
EnsureContinuousInvalidation(true);
}
void BrowserViewRenderer::SetIsPaused(bool paused) {
TRACE_EVENT_INSTANT1("android_webview",
"BrowserViewRenderer::SetIsPaused",
TRACE_EVENT_SCOPE_THREAD,
"paused",
paused);
is_paused_ = paused;
EnsureContinuousInvalidation(false);
}
void BrowserViewRenderer::SetViewVisibility(bool view_visible) {
TRACE_EVENT_INSTANT1("android_webview",
"BrowserViewRenderer::SetViewVisibility",
TRACE_EVENT_SCOPE_THREAD,
"view_visible",
view_visible);
view_visible_ = view_visible;
}
void BrowserViewRenderer::SetWindowVisibility(bool window_visible) {
TRACE_EVENT_INSTANT1("android_webview",
"BrowserViewRenderer::SetWindowVisibility",
TRACE_EVENT_SCOPE_THREAD,
"window_visible",
window_visible);
window_visible_ = window_visible;
EnsureContinuousInvalidation(false);
}
void BrowserViewRenderer::OnSizeChanged(int width, int height) {
TRACE_EVENT_INSTANT2("android_webview",
"BrowserViewRenderer::OnSizeChanged",
TRACE_EVENT_SCOPE_THREAD,
"width",
width,
"height",
height);
width_ = width;
height_ = height;
}
void BrowserViewRenderer::OnAttachedToWindow(int width, int height) {
TRACE_EVENT2("android_webview",
"BrowserViewRenderer::OnAttachedToWindow",
"width",
width,
"height",
height);
attached_to_window_ = true;
width_ = width;
height_ = height;
tile_manager_key_ = GlobalTileManager::GetInstance()->PushBack(this);
}
void BrowserViewRenderer::OnDetachedFromWindow() {
TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
attached_to_window_ = false;
if (hardware_enabled_) {
scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput();
if (input.get()) {
shared_renderer_state_->ReturnResources(
input->frame.delegated_frame_data->resource_list);
}
ReturnResources();
DCHECK(shared_renderer_state_->ReturnedResourcesEmpty());
if (switches::UbercompEnabled())
shared_renderer_state_->GetCompositor()->ReleaseHwDraw();
shared_renderer_state_->SetSharedContext(NULL);
hardware_enabled_ = false;
}
SynchronousCompositorMemoryPolicy zero_policy;
RequestMemoryPolicy(zero_policy);
GlobalTileManager::GetInstance()->Remove(tile_manager_key_);
// The hardware resources are released in the destructor of hardware renderer,
// so we don't need to do it here.
// See AwContents::ReleaseHardwareDrawOnRenderThread(JNIEnv*, jobject).
}
bool BrowserViewRenderer::IsAttachedToWindow() const {
return attached_to_window_;
}
bool BrowserViewRenderer::IsVisible() const {
// Ignore |window_visible_| if |attached_to_window_| is false.
return view_visible_ && (!attached_to_window_ || window_visible_);
}
gfx::Rect BrowserViewRenderer::GetScreenRect() const {
return gfx::Rect(client_->GetLocationOnScreen(), gfx::Size(width_, height_));
}
void BrowserViewRenderer::DidInitializeCompositor(
content::SynchronousCompositor* compositor) {
TRACE_EVENT0("android_webview",
"BrowserViewRenderer::DidInitializeCompositor");
DCHECK(compositor);
DCHECK(!has_compositor_);
DCHECK(ui_task_runner_->BelongsToCurrentThread());
has_compositor_ = true;
shared_renderer_state_->SetCompositorOnUiThread(compositor);
}
void BrowserViewRenderer::DidDestroyCompositor(
content::SynchronousCompositor* compositor) {
TRACE_EVENT0("android_webview", "BrowserViewRenderer::DidDestroyCompositor");
DCHECK(has_compositor_);
DCHECK(ui_task_runner_->BelongsToCurrentThread());
has_compositor_ = false;
shared_renderer_state_->SetCompositorOnUiThread(NULL);
SynchronousCompositorMemoryPolicy zero_policy;
DCHECK(shared_renderer_state_->GetMemoryPolicy() == zero_policy);
}
void BrowserViewRenderer::SetContinuousInvalidate(bool invalidate) {
{
base::AutoLock lock(render_thread_lock_);
if (compositor_needs_continuous_invalidate_ == invalidate)
return;
TRACE_EVENT_INSTANT1("android_webview",
"BrowserViewRenderer::SetContinuousInvalidate",
TRACE_EVENT_SCOPE_THREAD,
"invalidate",
invalidate);
compositor_needs_continuous_invalidate_ = invalidate;
}
if (ui_task_runner_->BelongsToCurrentThread()) {
EnsureContinuousInvalidation(false);
return;
}
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&BrowserViewRenderer::EnsureContinuousInvalidation,
ui_thread_weak_ptr_,
false));
}
void BrowserViewRenderer::SetDipScale(float dip_scale) {
dip_scale_ = dip_scale;
CHECK(dip_scale_ > 0);
}
gfx::Vector2d BrowserViewRenderer::max_scroll_offset() const {
DCHECK_GT(dip_scale_, 0);
return gfx::ToCeiledVector2d(gfx::ScaleVector2d(
max_scroll_offset_dip_, dip_scale_ * page_scale_factor_));
}
void BrowserViewRenderer::ScrollTo(gfx::Vector2d scroll_offset) {
gfx::Vector2d max_offset = max_scroll_offset();
gfx::Vector2dF scroll_offset_dip;
// To preserve the invariant that scrolling to the maximum physical pixel
// value also scrolls to the maximum dip pixel value we transform the physical
// offset into the dip offset by using a proportion (instead of dividing by
// dip_scale * page_scale_factor).
if (max_offset.x()) {
scroll_offset_dip.set_x((scroll_offset.x() * max_scroll_offset_dip_.x()) /
max_offset.x());
}
if (max_offset.y()) {
scroll_offset_dip.set_y((scroll_offset.y() * max_scroll_offset_dip_.y()) /
max_offset.y());
}
DCHECK_LE(0, scroll_offset_dip.x());
DCHECK_LE(0, scroll_offset_dip.y());
DCHECK_LE(scroll_offset_dip.x(), max_scroll_offset_dip_.x());
DCHECK_LE(scroll_offset_dip.y(), max_scroll_offset_dip_.y());
{
base::AutoLock lock(render_thread_lock_);
if (scroll_offset_dip_ == scroll_offset_dip)
return;
scroll_offset_dip_ = scroll_offset_dip;
}
TRACE_EVENT_INSTANT2("android_webview",
"BrowserViewRenderer::ScrollTo",
TRACE_EVENT_SCOPE_THREAD,
"x",
scroll_offset_dip.x(),
"y",
scroll_offset_dip.y());
if (has_compositor_)
shared_renderer_state_->GetCompositor()->
DidChangeRootLayerScrollOffset();
}
void BrowserViewRenderer::DidUpdateContent() {
if (!ui_task_runner_->BelongsToCurrentThread()) {
ui_task_runner_->PostTask(FROM_HERE,
base::Bind(&BrowserViewRenderer::DidUpdateContent,
ui_thread_weak_ptr_));
return;
}
TRACE_EVENT_INSTANT0("android_webview",
"BrowserViewRenderer::DidUpdateContent",
TRACE_EVENT_SCOPE_THREAD);
clear_view_ = false;
if (on_new_picture_enable_)
client_->OnNewPicture();
}
void BrowserViewRenderer::SetTotalRootLayerScrollOffset(
gfx::Vector2dF scroll_offset_dip) {
{
base::AutoLock lock(render_thread_lock_);
// TOOD(mkosiba): Add a DCHECK to say that this does _not_ get called during
// DrawGl when http://crbug.com/249972 is fixed.
if (scroll_offset_dip_ == scroll_offset_dip)
return;
scroll_offset_dip_ = scroll_offset_dip;
}
gfx::Vector2d max_offset = max_scroll_offset();
gfx::Vector2d scroll_offset;
// For an explanation as to why this is done this way see the comment in
// BrowserViewRenderer::ScrollTo.
if (max_scroll_offset_dip_.x()) {
scroll_offset.set_x((scroll_offset_dip.x() * max_offset.x()) /
max_scroll_offset_dip_.x());
}
if (max_scroll_offset_dip_.y()) {
scroll_offset.set_y((scroll_offset_dip.y() * max_offset.y()) /
max_scroll_offset_dip_.y());
}
DCHECK(0 <= scroll_offset.x());
DCHECK(0 <= scroll_offset.y());
DCHECK(scroll_offset.x() <= max_offset.x());
DCHECK(scroll_offset.y() <= max_offset.y());
client_->ScrollContainerViewTo(scroll_offset);
}
gfx::Vector2dF BrowserViewRenderer::GetTotalRootLayerScrollOffset() {
base::AutoLock lock(render_thread_lock_);
return scroll_offset_dip_;
}
bool BrowserViewRenderer::IsExternalFlingActive() const {
if (!ui_task_runner_->BelongsToCurrentThread()) {
// TODO(boliu): This is short term hack since we cannot call into
// view system on non-UI thread.
return false;
}
return client_->IsFlingActive();
}
void BrowserViewRenderer::UpdateRootLayerState(
const gfx::Vector2dF& total_scroll_offset_dip,
const gfx::Vector2dF& max_scroll_offset_dip,
const gfx::SizeF& scrollable_size_dip,
float page_scale_factor,
float min_page_scale_factor,
float max_page_scale_factor) {
if (!ui_task_runner_->BelongsToCurrentThread()) {
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&BrowserViewRenderer::UpdateRootLayerState,
ui_thread_weak_ptr_,
total_scroll_offset_dip,
max_scroll_offset_dip,
scrollable_size_dip,
page_scale_factor,
min_page_scale_factor,
max_page_scale_factor));
return;
}
TRACE_EVENT_INSTANT1(
"android_webview",
"BrowserViewRenderer::UpdateRootLayerState",
TRACE_EVENT_SCOPE_THREAD,
"state",
TracedValue::FromValue(
RootLayerStateAsValue(total_scroll_offset_dip, scrollable_size_dip)
.release()));
DCHECK_GT(dip_scale_, 0);
max_scroll_offset_dip_ = max_scroll_offset_dip;
DCHECK_LE(0, max_scroll_offset_dip_.x());
DCHECK_LE(0, max_scroll_offset_dip_.y());
page_scale_factor_ = page_scale_factor;
DCHECK_GT(page_scale_factor_, 0);
client_->UpdateScrollState(max_scroll_offset(),
scrollable_size_dip,
page_scale_factor,
min_page_scale_factor,
max_page_scale_factor);
SetTotalRootLayerScrollOffset(total_scroll_offset_dip);
}
scoped_ptr<base::Value> BrowserViewRenderer::RootLayerStateAsValue(
const gfx::Vector2dF& total_scroll_offset_dip,
const gfx::SizeF& scrollable_size_dip) {
scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue);
state->SetDouble("total_scroll_offset_dip.x", total_scroll_offset_dip.x());
state->SetDouble("total_scroll_offset_dip.y", total_scroll_offset_dip.y());
state->SetDouble("max_scroll_offset_dip.x", max_scroll_offset_dip_.x());
state->SetDouble("max_scroll_offset_dip.y", max_scroll_offset_dip_.y());
state->SetDouble("scrollable_size_dip.width", scrollable_size_dip.width());
state->SetDouble("scrollable_size_dip.height", scrollable_size_dip.height());
state->SetDouble("page_scale_factor", page_scale_factor_);
return state.PassAs<base::Value>();
}
void BrowserViewRenderer::DidOverscroll(gfx::Vector2dF accumulated_overscroll,
gfx::Vector2dF latest_overscroll_delta,
gfx::Vector2dF current_fling_velocity) {
if (!ui_task_runner_->BelongsToCurrentThread()) {
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&BrowserViewRenderer::DidOverscroll,
ui_thread_weak_ptr_,
accumulated_overscroll,
latest_overscroll_delta,
current_fling_velocity));
return;
}
const float physical_pixel_scale = dip_scale_ * page_scale_factor_;
if (accumulated_overscroll == latest_overscroll_delta)
overscroll_rounding_error_ = gfx::Vector2dF();
gfx::Vector2dF scaled_overscroll_delta =
gfx::ScaleVector2d(latest_overscroll_delta, physical_pixel_scale);
gfx::Vector2d rounded_overscroll_delta = gfx::ToRoundedVector2d(
scaled_overscroll_delta + overscroll_rounding_error_);
overscroll_rounding_error_ =
scaled_overscroll_delta - rounded_overscroll_delta;
client_->DidOverscroll(rounded_overscroll_delta);
}
void BrowserViewRenderer::EnsureContinuousInvalidation(bool force_invalidate) {
// This method should be called again when any of these conditions change.
bool need_invalidate =
compositor_needs_continuous_invalidate_ || force_invalidate;
if (!need_invalidate || block_invalidates_)
return;
// Always call view invalidate. We rely the Android framework to ignore the
// invalidate when it's not needed such as when view is not visible.
client_->PostInvalidate();
// Stop fallback ticks when one of these is true.
// 1) Webview is paused. Also need to check we are not in clear view since
// paused, offscreen still expect clear view to recover.
// 2) If we are attached to window and the window is not visible (eg when
// app is in the background). We are sure in this case the webview is used
// "on-screen" but that updates are not needed when in the background.
bool throttle_fallback_tick =
(is_paused_ && !clear_view_) || (attached_to_window_ && !window_visible_);
if (throttle_fallback_tick)
return;
{
base::AutoLock lock(render_thread_lock_);
block_invalidates_ = compositor_needs_continuous_invalidate_;
}
// Unretained here is safe because the callback is cancelled when
// |fallback_tick_| is destroyed.
fallback_tick_.Reset(base::Bind(&BrowserViewRenderer::FallbackTickFired,
base::Unretained(this)));
// No need to reschedule fallback tick if compositor does not need to be
// ticked. This can happen if this is reached because force_invalidate is
// true.
if (compositor_needs_continuous_invalidate_) {
ui_task_runner_->PostDelayedTask(
FROM_HERE,
fallback_tick_.callback(),
base::TimeDelta::FromMilliseconds(kFallbackTickTimeoutInMilliseconds));
}
}
void BrowserViewRenderer::FallbackTickFired() {
TRACE_EVENT1("android_webview",
"BrowserViewRenderer::FallbackTickFired",
"compositor_needs_continuous_invalidate_",
compositor_needs_continuous_invalidate_);
// This should only be called if OnDraw or DrawGL did not come in time, which
// means block_invalidates_ must still be true.
DCHECK(block_invalidates_);
if (compositor_needs_continuous_invalidate_ && has_compositor_)
ForceFakeCompositeSW();
}
void BrowserViewRenderer::ForceFakeCompositeSW() {
DCHECK(has_compositor_);
SkBitmap bitmap;
bitmap.allocN32Pixels(1, 1);
bitmap.eraseColor(0);
SkCanvas canvas(bitmap);
CompositeSW(&canvas);
}
bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) {
DCHECK(has_compositor_);
bool result = shared_renderer_state_->GetCompositor()->
DemandDrawSw(canvas);
DidComposite(false);
return result;
}
void BrowserViewRenderer::DidComposite(bool force_invalidate) {
{
base::AutoLock lock(render_thread_lock_);
block_invalidates_ = false;
}
if (!ui_task_runner_->BelongsToCurrentThread()) {
ui_task_runner_->PostTask(
FROM_HERE,
base::Bind(&BrowserViewRenderer::EnsureContinuousInvalidation,
ui_thread_weak_ptr_,
force_invalidate));
return;
}
fallback_tick_.Cancel();
EnsureContinuousInvalidation(force_invalidate);
}
std::string BrowserViewRenderer::ToString(AwDrawGLInfo* draw_info) const {
std::string str;
base::StringAppendF(&str, "is_paused: %d ", is_paused_);
base::StringAppendF(&str, "view_visible: %d ", view_visible_);
base::StringAppendF(&str, "window_visible: %d ", window_visible_);
base::StringAppendF(&str, "dip_scale: %f ", dip_scale_);
base::StringAppendF(&str, "page_scale_factor: %f ", page_scale_factor_);
base::StringAppendF(&str,
"compositor_needs_continuous_invalidate: %d ",
compositor_needs_continuous_invalidate_);
base::StringAppendF(&str, "block_invalidates: %d ", block_invalidates_);
base::StringAppendF(&str, "view width height: [%d %d] ", width_, height_);
base::StringAppendF(&str, "attached_to_window: %d ", attached_to_window_);
base::StringAppendF(&str,
"global visible rect: %s ",
last_on_draw_global_visible_rect_.ToString().c_str());
base::StringAppendF(
&str, "scroll_offset_dip: %s ", scroll_offset_dip_.ToString().c_str());
base::StringAppendF(&str,
"overscroll_rounding_error_: %s ",
overscroll_rounding_error_.ToString().c_str());
base::StringAppendF(
&str, "on_new_picture_enable: %d ", on_new_picture_enable_);
base::StringAppendF(&str, "clear_view: %d ", clear_view_);
if (draw_info) {
base::StringAppendF(&str,
"clip left top right bottom: [%d %d %d %d] ",
draw_info->clip_left,
draw_info->clip_top,
draw_info->clip_right,
draw_info->clip_bottom);
base::StringAppendF(&str,
"surface width height: [%d %d] ",
draw_info->width,
draw_info->height);
base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer);
}
return str;
}
} // namespace android_webview