Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Unified Diff: services/ui/public/cpp/property_type_converters.cc

Issue 2523433003: Use mojo [de]serialization for SkBitmap property type conversion. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/ui/public/cpp/property_type_converters.cc
diff --git a/services/ui/public/cpp/property_type_converters.cc b/services/ui/public/cpp/property_type_converters.cc
index b88d4c1aeb7a59e74a1bff194526de05f96fd966..276d2006e1ee6fef637e3b51d9f8dbaf6fb31b68 100644
--- a/services/ui/public/cpp/property_type_converters.cc
+++ b/services/ui/public/cpp/property_type_converters.cc
@@ -7,19 +7,12 @@
#include <stdint.h>
#include "base/strings/utf_string_conversions.h"
+#include "skia/public/interfaces/bitmap.mojom.h"
+#include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
-namespace {
-
-// Maximum allowed height or width of a bitmap, in pixels. This limit prevents
-// malformed bitmap headers from causing arbitrarily large memory allocations
-// for pixel data.
-const int kMaxBitmapSize = 4096;
-
-} // namespace
-
namespace mojo {
// static
@@ -153,71 +146,14 @@ std::string TypeConverter<std::string, std::vector<uint8_t>>::Convert(
// static
std::vector<uint8_t> TypeConverter<std::vector<uint8_t>, SkBitmap>::Convert(
const SkBitmap& input) {
- // Empty images are valid to serialize and are represented by an empty vector.
- if (input.isNull())
- return std::vector<uint8_t>();
-
- // Only RGBA 8888 bitmaps with premultiplied alpha are supported.
- if (input.colorType() != kBGRA_8888_SkColorType ||
- input.alphaType() != kPremul_SkAlphaType) {
- NOTREACHED();
- return std::vector<uint8_t>();
- }
-
- // Sanity check the bitmap size.
- int width = input.width();
- int height = input.height();
- if (width < 0 || width > kMaxBitmapSize || height < 0 ||
- height > kMaxBitmapSize) {
- NOTREACHED();
- return std::vector<uint8_t>();
- }
-
- // Serialize the bitmap. The size is restricted so only 2 bytes are required
- // per dimension.
- std::vector<uint8_t> vec(4 + input.getSize());
- vec[0] = (width >> 8) & 0xFF;
- vec[1] = width & 0xFF;
- vec[2] = (height >> 8) & 0xFF;
- vec[3] = height & 0xFF;
- if (!input.copyPixelsTo(&vec[4], input.getSize()))
- return std::vector<uint8_t>();
- return vec;
+ return skia::mojom::Bitmap::Serialize(&input);
}
// static
SkBitmap TypeConverter<SkBitmap, std::vector<uint8_t>>::Convert(
const std::vector<uint8_t>& input) {
- // Empty images are represented by empty vectors.
- if (input.empty())
- return SkBitmap();
-
- // Read and sanity check size.
- int width = input[0] << 8 | input[1];
- int height = input[2] << 8 | input[3];
- if (width < 0 || width > kMaxBitmapSize || height < 0 ||
- height > kMaxBitmapSize) {
- NOTREACHED();
- return SkBitmap();
- }
-
- // Try to allocate a bitmap of the appropriate size.
- SkBitmap bitmap;
- if (!bitmap.tryAllocPixels(SkImageInfo::Make(
- width, height, kBGRA_8888_SkColorType, kPremul_SkAlphaType))) {
- return SkBitmap();
- }
-
- // Ensure the vector contains the right amount of data.
- if (input.size() != bitmap.getSize() + 4) {
- NOTREACHED();
- return SkBitmap();
- }
-
- // Read the pixel data.
- SkAutoLockPixels lock(bitmap);
- memcpy(bitmap.getPixels(), &input[4], bitmap.getSize());
- return bitmap;
+ SkBitmap output;
+ return skia::mojom::Bitmap::Deserialize(input, &output) ? output : SkBitmap();
}
// static
« no previous file with comments | « services/ui/public/cpp/property_type_converters.h ('k') | services/ui/public/cpp/tests/property_type_converters_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698