1 // Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_IMAGE_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_IMAGE_IMPL_H_ 7 #pragma once 8 9 #include "include/cef_image.h" 10 #include "libcef/browser/thread_util.h" 11 12 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "ui/gfx/image/image.h" 14 15 class CefImageImpl : public CefImage { 16 public: 17 // Creates an empty image with no representations. 18 CefImageImpl() = default; 19 20 // Creates a new image by copying the ImageSkia for use as the default 21 // representation. 22 explicit CefImageImpl(const gfx::ImageSkia& image_skia); 23 24 CefImageImpl(const CefImageImpl&) = delete; 25 CefImageImpl& operator=(const CefImageImpl&) = delete; 26 27 // Deletes the image and, if the only owner of the storage, all of its cached 28 // representations. 29 ~CefImageImpl() override = default; 30 31 // CefImage methods: 32 bool IsEmpty() override; 33 bool IsSame(CefRefPtr<CefImage> that) override; 34 bool AddBitmap(float scale_factor, 35 int pixel_width, 36 int pixel_height, 37 cef_color_type_t color_type, 38 cef_alpha_type_t alpha_type, 39 const void* pixel_data, 40 size_t pixel_data_size) override; 41 bool AddPNG(float scale_factor, 42 const void* png_data, 43 size_t png_data_size) override; 44 bool AddJPEG(float scale_factor, 45 const void* jpeg_data, 46 size_t jpeg_data_size) override; 47 size_t GetWidth() override; 48 size_t GetHeight() override; 49 bool HasRepresentation(float scale_factor) override; 50 bool RemoveRepresentation(float scale_factor) override; 51 bool GetRepresentationInfo(float scale_factor, 52 float& actual_scale_factor, 53 int& pixel_width, 54 int& pixel_height) override; 55 CefRefPtr<CefBinaryValue> GetAsBitmap(float scale_factor, 56 cef_color_type_t color_type, 57 cef_alpha_type_t alpha_type, 58 int& pixel_width, 59 int& pixel_height) override; 60 CefRefPtr<CefBinaryValue> GetAsPNG(float scale_factor, 61 bool with_transparency, 62 int& pixel_width, 63 int& pixel_height) override; 64 CefRefPtr<CefBinaryValue> GetAsJPEG(float scale_factor, 65 int quality, 66 int& pixel_width, 67 int& pixel_height) override; 68 69 // Add |bitmaps| which should be the same image at different scale factors. 70 // |scale_1x_size| is the size in pixels of the 1x factor image. If 71 // |scale_1x_size| is 0 the smallest image size in pixels will be used as the 72 // 1x factor size. 73 void AddBitmaps(int32_t scale_1x_size, const std::vector<SkBitmap>& bitmaps); 74 75 // Return a representation of this Image that contains only the bitmap nearest 76 // |scale_factor| as the 1x scale representation. Conceptually this is an 77 // incorrect representation but is necessary to work around bugs in the views 78 // architecture. 79 // TODO(cef): Remove once https://crbug.com/597732 is resolved. 80 gfx::ImageSkia GetForced1xScaleRepresentation(float scale_factor) const; 81 82 // Returns the skia representation of this Image. 83 gfx::ImageSkia AsImageSkia() const; 84 85 private: 86 // Add a bitmap. 87 bool AddBitmap(float scale_factor, const SkBitmap& bitmap); 88 89 // Returns the bitmap that most closely matches |scale_factor| or nullptr if 90 // one doesn't exist. 91 const SkBitmap* GetBitmap(float scale_factor) const; 92 93 // Convert |src_bitmap| to |target_bitmap| with |target_ct| and |target_at|. 94 static bool ConvertBitmap(const SkBitmap& src_bitmap, 95 SkBitmap* target_bitmap, 96 SkColorType target_ct, 97 SkAlphaType target_at); 98 99 // The |bitmap| argument will be RGBA or BGRA and either opaque or transparent 100 // with post-multiplied alpha. Writes the compressed output into |compressed|. 101 using CompressionMethod = 102 base::OnceCallback<bool(const SkBitmap& /*bitmap*/, 103 std::vector<unsigned char>* /*compressed*/)>; 104 105 // Write |bitmap| into |compressed| using |method|. 106 static bool WriteCompressedFormat(const SkBitmap& bitmap, 107 std::vector<unsigned char>* compressed, 108 CompressionMethod method); 109 110 // Write |bitmap| into |compressed| using PNG encoding. 111 static bool WritePNG(const SkBitmap& bitmap, 112 std::vector<unsigned char>* compressed, 113 bool with_transparency); 114 115 // Write |bitmap| into |compressed| using JPEG encoding. The alpha channel 116 // will be ignored. 117 static bool WriteJPEG(const SkBitmap& bitmap, 118 std::vector<unsigned char>* compressed, 119 int quality); 120 121 mutable base::Lock lock_; 122 123 // Access to |image_| must be protected by |lock_|. 124 gfx::Image image_; 125 126 IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(CefImageImpl); 127 }; 128 129 #endif // CEF_LIBCEF_BROWSER_IMAGE_IMPL_H_ 130