Commit 221c45d9 authored by jhorwich@chromium.org's avatar jhorwich@chromium.org

Add PPB_PDF API to load scaled image resources

For HiDPI PDF viewer, add new API to fetch properly-scaled resource for the
target display.

BUG=114674
TEST=Run PDF Viewer on HiDPI screen with HiDPI PDF Viewer flag, view buttons

Review URL: https://chromiumcodereview.appspot.com/10836055

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149548 0039d316-1c4b-4281-b951-d872f2087c98
parent ddabf57e
......@@ -29,7 +29,10 @@
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "unicode/usearch.h"
#include "webkit/plugins/ppapi/host_globals.h"
#include "webkit/plugins/ppapi/plugin_delegate.h"
......@@ -153,46 +156,6 @@ PP_Var GetLocalizedString(PP_Instance instance_id,
return ppapi::StringVar::StringToPPVar(rv);
}
PP_Resource GetResourceImage(PP_Instance instance_id,
PP_ResourceImage image_id) {
int res_id = 0;
for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) {
if (kResourceImageMap[i].pp_id == image_id) {
res_id = kResourceImageMap[i].res_id;
break;
}
}
if (res_id == 0)
return 0;
SkBitmap* res_bitmap =
ResourceBundle::GetSharedInstance().GetBitmapNamed(res_id);
// Validate the instance.
if (!content::GetHostGlobals()->GetInstance(instance_id))
return 0;
scoped_refptr<webkit::ppapi::PPB_ImageData_Impl> image_data(
new webkit::ppapi::PPB_ImageData_Impl(
instance_id,
webkit::ppapi::PPB_ImageData_Impl::PLATFORM));
if (!image_data->Init(
webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(),
res_bitmap->width(), res_bitmap->height(), false)) {
return 0;
}
webkit::ppapi::ImageDataAutoMapper mapper(image_data);
if (!mapper.is_valid())
return 0;
skia::PlatformCanvas* canvas = image_data->GetPlatformCanvas();
// Note: Do not skBitmap::copyTo the canvas bitmap directly because it will
// ignore the allocated pixels in shared memory and re-allocate a new buffer.
canvas->writePixels(*res_bitmap, 0, 0);
return image_data->GetReference();
}
PP_Resource GetFontFileWithFallback(
PP_Instance instance_id,
const PP_FontDescription_Dev* description,
......@@ -363,6 +326,66 @@ PP_Bool IsFeatureEnabled(PP_PDFFeature feature) {
return result;
}
PP_Resource GetResourceImageForScale(PP_Instance instance_id,
PP_ResourceImage image_id,
float scale) {
int res_id = 0;
for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) {
if (kResourceImageMap[i].pp_id == image_id) {
res_id = kResourceImageMap[i].res_id;
break;
}
}
if (res_id == 0)
return 0;
ui::ScaleFactor scale_factor = ui::GetScaleFactorFromScale(scale);
gfx::ImageSkia* res_image_skia =
ResourceBundle::GetSharedInstance().GetImageSkiaNamed(res_id);
if (!res_image_skia)
return 0;
// Validate the instance.
if (!content::GetHostGlobals()->GetInstance(instance_id))
return 0;
if (!res_image_skia->HasRepresentation(scale_factor))
return 0;
gfx::ImageSkiaRep image_skia_rep = res_image_skia->GetRepresentation(
scale_factor);
scoped_refptr<webkit::ppapi::PPB_ImageData_Impl> image_data(
new webkit::ppapi::PPB_ImageData_Impl(
instance_id,
webkit::ppapi::PPB_ImageData_Impl::PLATFORM));
if (!image_data->Init(
webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(),
image_skia_rep.pixel_width(),
image_skia_rep.pixel_height(),
false)) {
return 0;
}
webkit::ppapi::ImageDataAutoMapper mapper(image_data);
if (!mapper.is_valid())
return 0;
skia::PlatformCanvas* canvas = image_data->GetPlatformCanvas();
// Note: Do not skBitmap::copyTo the canvas bitmap directly because it will
// ignore the allocated pixels in shared memory and re-allocate a new buffer.
canvas->writePixels(image_skia_rep.sk_bitmap(), 0, 0);
return image_data->GetReference();
}
PP_Resource GetResourceImage(PP_Instance instance_id,
PP_ResourceImage image_id) {
return GetResourceImageForScale(instance_id, image_id, 1.0f);
}
const PPB_PDF ppb_pdf = {
&GetLocalizedString,
&GetResourceImage,
......@@ -377,7 +400,8 @@ const PPB_PDF ppb_pdf = {
&HasUnsupportedFeature,
&SaveAs,
&PPB_PDF_Impl::InvokePrintingForInstance,
&IsFeatureEnabled
&IsFeatureEnabled,
&GetResourceImageForScale
};
} // namespace
......
......@@ -169,6 +169,12 @@ struct PPB_PDF {
void (*Print)(PP_Instance instance);
PP_Bool(*IsFeatureEnabled)(PP_PDFFeature feature);
// Returns a resource image appropriate for a device with |scale| density.
// Returns 0 (NULL resource) if there is no resource at that scale
PP_Resource (*GetResourceImageForScale)(PP_Instance instance,
PP_ResourceImage image_id,
float scale);
};
#endif // PPAPI_C_PRIVATE_PPB_PDF_H_
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