blob: 28064f48fbc6da476a69914c405b81744cbd8465 [file] [log] [blame]
// Copyright 2018 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 "build/build_config.h"
#include "cc/paint/paint_image.h"
#include "cc/paint/paint_op_buffer.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/gfx_export.h"
namespace gfx {
// An ImageSkiaRep represents an image and the scale factor it is intended for.
// 0.0f scale is used to indicate that this ImageSkiaRep is used for unscaled
// (ImageSkia does not automatically scale the image).
// TODO(malaykeshav): Support transport of PaintRecord across mojo. This would
// require adding inline serialization support for PaintRecords.
class GFX_EXPORT ImageSkiaRep {
// Create null bitmap.
// Note: This is for testing purpose only.
// Creates a bitmap with kARGB_8888_Config config with given |size| in DIP.
// This allocates pixels in the bitmap. It is guaranteed that the data in the
// bitmap are initialized but the actual values are undefined.
// Specifying 0 scale means the image is for unscaled image. (unscaled()
// returns truen, and scale() returns 1.0f;)
ImageSkiaRep(const gfx::Size& size, float scale);
// Creates a bitmap with given scale.
// Adds ref to |src|.
ImageSkiaRep(const SkBitmap& src, float scale);
// Creates an image rep backed by a paint record of given size and scale. This
// is used when the image representation is sourced from a drawable sunch as
// CanvasImageSource.
ImageSkiaRep(sk_sp<cc::PaintRecord> paint_record,
const gfx::Size& size,
float scale);
ImageSkiaRep(const ImageSkiaRep& other);
// Get width and height of the image in pixels.
int pixel_width() const { return pixel_size_.width(); }
int pixel_height() const { return pixel_size_.height(); }
const Size& pixel_size() const { return pixel_size_; }
// Get width and height of the image in DIP.
int GetWidth() const;
int GetHeight() const;
// Retrieves the scale for which this image is a representation of.
float scale() const { return unscaled() ? 1.0f : scale_; }
bool unscaled() const { return scale_ == 0.0f; }
bool is_null() const {
return type_ == ImageRepType::kImageTypeBitmap ? bitmap_.isNull()
: !paint_record_;
// Returns the backing bitmap when the image representation is sourced from a
// bitmap. If this is a |kImageTypeDrawable| then it will generate(and cache)
// a bitmap.
const SkBitmap& GetBitmap() const;
// Returns the backing drawable as a PaintRecord. Use this when the type of
// ImageRep is |kImageTypeDrawable|.
sk_sp<cc::PaintRecord> GetPaintRecord() const;
const cc::PaintImage& paint_image() const { return paint_image_; }
bool has_paint_image() const { return !!paint_image_; }
enum class ImageRepType {
kImageTypeBitmap, // When the source image is rasterized. (Bitmaps, PNGs)
kImageTypeDrawable // When the source image is a drawable generated by a
// CanvasImageSource.
// TODO(malaykeshav): Remove when migration is complete and it is safe.
cc::PaintImage paint_image_;
mutable sk_sp<cc::PaintRecord> paint_record_;
ImageRepType type_;
Size pixel_size_;
mutable SkBitmap bitmap_;
float scale_;
} // namespace gfx