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 // Deletes the image and, if the only owner of the storage, all of its cached 25 // representations. 26 ~CefImageImpl() override = default; 27 28 // CefImage methods: 29 bool IsEmpty() override; 30 bool IsSame(CefRefPtr<CefImage> that) override; 31 bool AddBitmap(float scale_factor, 32 int pixel_width, 33 int pixel_height, 34 cef_color_type_t color_type, 35 cef_alpha_type_t alpha_type, 36 const void* pixel_data, 37 size_t pixel_data_size) override; 38 bool AddPNG(float scale_factor, 39 const void* png_data, 40 size_t png_data_size) override; 41 bool AddJPEG(float scale_factor, 42 const void* jpeg_data, 43 size_t jpeg_data_size) override; 44 size_t GetWidth() override; 45 size_t GetHeight() override; 46 bool HasRepresentation(float scale_factor) override; 47 bool RemoveRepresentation(float scale_factor) override; 48 bool GetRepresentationInfo(float scale_factor, 49 float& actual_scale_factor, 50 int& pixel_width, 51 int& pixel_height) override; 52 CefRefPtr<CefBinaryValue> GetAsBitmap(float scale_factor, 53 cef_color_type_t color_type, 54 cef_alpha_type_t alpha_type, 55 int& pixel_width, 56 int& pixel_height) override; 57 CefRefPtr<CefBinaryValue> GetAsPNG(float scale_factor, 58 bool with_transparency, 59 int& pixel_width, 60 int& pixel_height) override; 61 CefRefPtr<CefBinaryValue> GetAsJPEG(float scale_factor, 62 int quality, 63 int& pixel_width, 64 int& pixel_height) override; 65 66 // Add |bitmaps| which should be the same image at different scale factors. 67 // |scale_1x_size| is the size in pixels of the 1x factor image. If 68 // |scale_1x_size| is 0 the smallest image size in pixels will be used as the 69 // 1x factor size. 70 void AddBitmaps(int32_t scale_1x_size, const std::vector<SkBitmap>& bitmaps); 71 72 // Return a representation of this Image that contains only the bitmap nearest 73 // |scale_factor| as the 1x scale representation. Conceptually this is an 74 // incorrect representation but is necessary to work around bugs in the views 75 // architecture. 76 // TODO(cef): Remove once https://crbug.com/597732 is resolved. 77 gfx::ImageSkia GetForced1xScaleRepresentation(float scale_factor) const; 78 79 // Returns the skia representation of this Image. 80 gfx::ImageSkia AsImageSkia() const; 81 82 private: 83 // Add a bitmap. 84 bool AddBitmap(float scale_factor, const SkBitmap& bitmap); 85 86 // Returns the bitmap that most closely matches |scale_factor| or nullptr if 87 // one doesn't exist. 88 const SkBitmap* GetBitmap(float scale_factor) const; 89 90 // Convert |src_bitmap| to |target_bitmap| with |target_ct| and |target_at|. 91 static bool ConvertBitmap(const SkBitmap& src_bitmap, 92 SkBitmap* target_bitmap, 93 SkColorType target_ct, 94 SkAlphaType target_at); 95 96 // The |bitmap| argument will be RGBA or BGRA and either opaque or transparent 97 // with post-multiplied alpha. Writes the compressed output into |compressed|. 98 typedef base::Callback<bool(const SkBitmap& /*bitmap*/, 99 std::vector<unsigned char>* /*compressed*/)> 100 CompressionMethod; 101 102 // Write |bitmap| into |compressed| using |method|. 103 static bool WriteCompressedFormat(const SkBitmap& bitmap, 104 std::vector<unsigned char>* compressed, 105 const CompressionMethod& method); 106 107 // Write |bitmap| into |compressed| using PNG encoding. 108 static bool WritePNG(const SkBitmap& bitmap, 109 std::vector<unsigned char>* compressed, 110 bool with_transparency); 111 112 // Write |bitmap| into |compressed| using JPEG encoding. The alpha channel 113 // will be ignored. 114 static bool WriteJPEG(const SkBitmap& bitmap, 115 std::vector<unsigned char>* compressed, 116 int quality); 117 118 mutable base::Lock lock_; 119 120 // Access to |image_| must be protected by |lock_|. 121 gfx::Image image_; 122 123 IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(CefImageImpl); 124 DISALLOW_COPY_AND_ASSIGN(CefImageImpl); 125 }; 126 127 #endif // CEF_LIBCEF_BROWSER_IMAGE_IMPL_H_ 128