Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / blimp / client / compositor / blimp_compositor_android.cc
blob269fe0abdf5de3ed09704bbd352a964d5a68349b
1 // Copyright 2015 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.
5 #include "blimp/client/compositor/blimp_compositor_android.h"
7 #include <android/native_window_jni.h>
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/gfx/geometry/size.h"
13 namespace {
14 // The minimum valid content width in a tile. This is used to make sure the
15 // width of the content on the rightmost tile isn't a tiny sliver.
16 const int kMinimumTileContentWidthPixels = 64;
19 namespace blimp {
21 // static
22 scoped_ptr<BlimpCompositorAndroid> BlimpCompositorAndroid::Create(
23 const gfx::Size& real_size,
24 const gfx::Size& size,
25 float dp_to_px) {
26 gfx::Size device_size(real_size);
27 bool real_size_supported = true;
28 if (device_size.IsEmpty()) {
29 real_size_supported = false;
30 device_size = size;
32 return make_scoped_ptr(
33 new BlimpCompositorAndroid(device_size, real_size_supported, dp_to_px));
36 BlimpCompositorAndroid::BlimpCompositorAndroid(const gfx::Size& size,
37 bool real_size_supported,
38 float dp_to_px)
39 : BlimpCompositor(dp_to_px),
40 portrait_width_(std::min(size.width(), size.height())),
41 landscape_width_(std::max(size.width(), size.height())),
42 real_size_supported_(real_size_supported),
43 window_(nullptr) {}
45 BlimpCompositorAndroid::~BlimpCompositorAndroid() {
46 SetSurface(nullptr, 0 /* null surface */);
49 void BlimpCompositorAndroid::SetSurface(JNIEnv* env, jobject jsurface) {
50 if (window_) {
51 SetVisible(false);
52 ANativeWindow_release(window_);
53 window_ = nullptr;
56 if (!jsurface)
57 return;
59 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
60 window_ = ANativeWindow_fromSurface(env, jsurface);
61 SetVisible(true);
64 gfx::AcceleratedWidget BlimpCompositorAndroid::GetWindow() {
65 return window_;
68 void BlimpCompositorAndroid::GenerateLayerTreeSettings(
69 cc::LayerTreeSettings* settings) {
70 BlimpCompositor::GenerateLayerTreeSettings(settings);
72 // Calculate the correct raster tile size to use. Assuming a square tile.
73 DCHECK_EQ(settings->default_tile_size.width(),
74 settings->default_tile_size.height());
76 int default_tile_size = settings->default_tile_size.width();
77 if (real_size_supported_) {
78 // Maximum HD dimensions should be 768x1280
79 // Maximum FHD dimensions should be 1200x1920
80 if (portrait_width_ > 768 || landscape_width_ > 1280)
81 default_tile_size = 384;
82 if (portrait_width_ > 1200 || landscape_width_ > 1920)
83 default_tile_size = 512;
85 // Adjust for some resolutions that barely straddle an extra
86 // tile when in portrait mode. This helps worst case scroll/raster
87 // by not needing a full extra tile for each row.
88 int right_tile_width = ((portrait_width_ - 1) % default_tile_size) + 1;
89 if (right_tile_width < kMinimumTileContentWidthPixels) {
90 // Figure out the new tile count without the small edge tile.
91 int full_tile_count = portrait_width_ / default_tile_size;
92 DCHECK_GT(full_tile_count, 0);
94 // Calculate the ideal new tile width with the new tile count.
95 default_tile_size = std::ceil(static_cast<float>(portrait_width_) /
96 static_cast<float>(full_tile_count));
98 // Round up to nearest 32 for GPU efficiency.
99 if (default_tile_size & 0x1F)
100 default_tile_size = (default_tile_size & ~0x1F) + 32;
102 } else {
103 // We don't know the exact resolution due to screen controls etc., so this
104 // just estimates the values above using tile counts.
105 int numTiles = (portrait_width_ * landscape_width_) / (256 * 256);
106 if (numTiles > 16)
107 default_tile_size = 384;
108 if (numTiles >= 40)
109 default_tile_size = 512;
111 settings->default_tile_size.SetSize(default_tile_size, default_tile_size);
114 } // namespace blimp