Commit 6ac05756 authored by rohitrao's avatar rohitrao Committed by Commit bot

Enables 300P support for iOS.

The code will continue to load the 200P pak file and fall back to those
resources, but the supported scale factor on @3x devices will now be 300P.

This CL adds a unittest to ensure that image conversion works properly when the
scale factor of the source image does not match the current supported scale
factor of the system.

BUG=413300
TEST=None

Review URL: https://codereview.chromium.org/574663003

Cr-Commit-Position: refs/heads/master@{#295160}
parent b9ec078c
......@@ -610,9 +610,7 @@ void ResourceBundle::InitSharedInstance(Delegate* delegate) {
gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
if (display.device_scale_factor() > 2.0) {
DCHECK_EQ(3.0, display.device_scale_factor());
// TODO(lliabraa): Add 3x images instead of using 2x on 3x screens.
// crbug.com/413300
supported_scale_factors.push_back(SCALE_FACTOR_200P);
supported_scale_factors.push_back(SCALE_FACTOR_300P);
} else if (display.device_scale_factor() > 1.0) {
DCHECK_EQ(2.0, display.device_scale_factor());
supported_scale_factors.push_back(SCALE_FACTOR_200P);
......
......@@ -56,6 +56,13 @@ void ResourceBundle::LoadCommonResources() {
AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil),
SCALE_FACTOR_200P);
}
// TODO(rohitrao): Add a chrome_300_percent file and load it here. For now,
// we are simply falling back to the 200P resources. http://crbug.com/413300.
if (IsScaleFactorSupported(SCALE_FACTOR_300P)) {
AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil),
SCALE_FACTOR_200P);
}
}
base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale,
......@@ -127,6 +134,14 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
// Create the image from the data.
CGFloat target_scale = ui::GetScaleForScaleFactor(scale_factor);
CGFloat source_scale = is_fallback ? 1.0 : target_scale;
// Hack: The 200P pak file is the only pak file loaded on iOS devices with
// an @3x scale factor. Force |source_scale| to be 2.0 to handle this case,
// since it cannot be anything else.
// TODO(rohitrao): Support proper fallback by using the actual scale factor
// of the source image, rather than assuming it is 1.0 or 2.0.
if (scale_factor == SCALE_FACTOR_300P) {
source_scale = 2.0;
}
base::scoped_nsobject<UIImage> ui_image(
[[UIImage alloc] initWithData:ns_data scale:source_scale]);
......
......@@ -15,6 +15,7 @@
'common_sources' : [
'font_unittest.cc',
'image/image_family_unittest.cc',
'image/image_ios_unittest.mm',
'image/image_skia_unittest.cc',
'image/image_unittest.cc',
'image/image_unittest_util.cc',
......
// Copyright (c) 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.
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
namespace {
// Helper function to return a UIImage with the given size and scale.
UIImage* UIImageWithSizeAndScale(CGFloat width, CGFloat height, CGFloat scale) {
CGSize target_size = CGSizeMake(width * scale, height * scale);
// Create a UIImage directly from a CGImage in order to control the exact
// pixel size of the underlying image.
base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
CGColorSpaceCreateDeviceRGB());
base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
NULL,
target_size.width,
target_size.height,
8,
target_size.width * 4,
color_space,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
CGRect target_rect = CGRectMake(0, 0,
target_size.width, target_size.height);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, target_rect);
base::ScopedCFTypeRef<CGImageRef> cg_image(
CGBitmapContextCreateImage(context));
return [UIImage imageWithCGImage:cg_image
scale:scale
orientation:UIImageOrientationUp];
}
class ImageIOSTest : public testing::Test {
public:
ImageIOSTest() {}
virtual ~ImageIOSTest() {}
virtual void SetUp() OVERRIDE {
original_scale_factors_ = gfx::ImageSkia::GetSupportedScales();
}
virtual void TearDown() OVERRIDE {
gfx::ImageSkia::SetSupportedScales(original_scale_factors_);
}
private:
// Used to save and restore the scale factors in effect before this test.
std::vector<float> original_scale_factors_;
DISALLOW_COPY_AND_ASSIGN(ImageIOSTest);
};
// Tests image conversion when the scale factor of the source image is not in
// the list of supported scale factors.
TEST_F(ImageIOSTest, ImageConversionWithUnsupportedScaleFactor) {
const CGFloat kWidth = 200;
const CGFloat kHeight = 100;
const CGFloat kTestScales[3] = { 1.0f, 2.0f, 3.0f };
for (size_t i = 0; i < arraysize(kTestScales); ++i) {
for (size_t j = 0; j < arraysize(kTestScales); ++j) {
const CGFloat source_scale = kTestScales[i];
const CGFloat supported_scale = kTestScales[j];
// Set the supported scale for testing.
std::vector<float> supported_scales;
supported_scales.push_back(supported_scale);
gfx::ImageSkia::SetSupportedScales(supported_scales);
// Create an UIImage with the appropriate source_scale.
UIImage* ui_image =
UIImageWithSizeAndScale(kWidth, kHeight, source_scale);
ASSERT_EQ(kWidth, ui_image.size.width);
ASSERT_EQ(kHeight, ui_image.size.height);
ASSERT_EQ(source_scale, ui_image.scale);
// Convert to SkBitmap and test its size.
gfx::Image to_skbitmap([ui_image retain]);
const SkBitmap* bitmap = to_skbitmap.ToSkBitmap();
ASSERT_TRUE(bitmap != NULL);
EXPECT_EQ(kWidth * supported_scale, bitmap->width());
EXPECT_EQ(kHeight * supported_scale, bitmap->height());
// Convert to ImageSkia and test its size.
gfx::Image to_imageskia([ui_image retain]);
const gfx::ImageSkia* imageskia = to_imageskia.ToImageSkia();
EXPECT_EQ(kWidth, imageskia->width());
EXPECT_EQ(kHeight, imageskia->height());
// TODO(rohitrao): Convert from ImageSkia back to UIImage. This should
// scale the image based on the current set of supported scales.
}
}
}
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment