Commit b238298b authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Introduce chrome_pdf::Thumbnail class

The class is move-only and carries raw thumbnail image data, as well as
the intended device to pixel ratio.

The default constructor is deleted, and the constructor tries
allocating pixels in the bitmap to enforce the use of image info
appropriate for thumbnails to be drawn on an HTML <canvas> element.

Bug: 652400
Change-Id: I16bf97d9b0c06fa64bc0cf891f2fdccf0dbd8058
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2376877
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803753}
parent af426d4f
......@@ -129,6 +129,8 @@ if (enable_pdf) {
"preview_mode_client.h",
"range_set.cc",
"range_set.h",
"thumbnail.cc",
"thumbnail.h",
"url_loader_wrapper.h",
"url_loader_wrapper_impl.cc",
"url_loader_wrapper_impl.h",
......
// Copyright 2020 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 "pdf/thumbnail.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/numerics/ranges.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "ui/gfx/geometry/size.h"
namespace chrome_pdf {
namespace {
constexpr float kMinDevicePixelRatio = 0.25;
constexpr float kMaxDevicePixelRatio = 2;
} // namespace
Thumbnail::Thumbnail() = default;
Thumbnail::Thumbnail(const gfx::Size& page_size, float device_pixel_ratio) {
DCHECK_GE(device_pixel_ratio, kMinDevicePixelRatio);
DCHECK_LE(device_pixel_ratio, kMaxDevicePixelRatio);
device_pixel_ratio_ = base::ClampToRange(
device_pixel_ratio, kMinDevicePixelRatio, kMaxDevicePixelRatio);
// TODO(dhoss): Add conversion from page size to thumbnail size.
const gfx::Size thumbnail_size_device_pixels = page_size;
// Note that <canvas> can only hold data in RGBA format. It is the
// responsibility of the thumbnail's renderer to fill `bitmap_` with RGBA
// data.
const SkImageInfo info =
SkImageInfo::Make(thumbnail_size_device_pixels.width(),
thumbnail_size_device_pixels.height(),
kRGBA_8888_SkColorType, kPremul_SkAlphaType);
bool success = bitmap_.tryAllocPixels(info, info.minRowBytes());
DCHECK(success);
}
Thumbnail::Thumbnail(Thumbnail&& other) noexcept = default;
Thumbnail& Thumbnail::operator=(Thumbnail&& other) noexcept = default;
Thumbnail::~Thumbnail() = default;
} // namespace chrome_pdf
// Copyright 2020 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.
#ifndef PDF_THUMBNAIL_H_
#define PDF_THUMBNAIL_H_
#include "third_party/skia/include/core/SkBitmap.h"
namespace gfx {
class Size;
} // namespace gfx
namespace chrome_pdf {
class Thumbnail final {
public:
Thumbnail();
Thumbnail(const gfx::Size& page_size, float device_pixel_ratio);
Thumbnail(Thumbnail&& other) noexcept;
Thumbnail& operator=(Thumbnail&& other) noexcept;
~Thumbnail();
SkBitmap& bitmap() { return bitmap_; }
float device_pixel_ratio() const { return device_pixel_ratio_; }
private:
// Raw image data of the thumbnail.
SkBitmap bitmap_;
// Intended resolution of the thumbnail image. The dimensions of `bitmap_`
// are the dimensions of the thumbnail in CSS pixels multiplied by
// `device_pixel_ratio_`.
// Only values between 0.25 and 2 are supported.
float device_pixel_ratio_ = 1;
};
} // namespace chrome_pdf
#endif // PDF_THUMBNAIL_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