1 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "chrome/common/ipc_message_utils.h"
6 :
7 : #include "base/gfx/rect.h"
8 : #ifndef EXCLUDE_SKIA_DEPENDENCIES
9 : #include "SkBitmap.h"
10 : #endif
11 :
12 : namespace IPC {
13 :
14 : #ifndef EXCLUDE_SKIA_DEPENDENCIES
15 :
16 : namespace {
17 :
18 : struct SkBitmap_Data {
19 : // The configuration for the bitmap (bits per pixel, etc).
20 : SkBitmap::Config fConfig;
21 :
22 : // The width of the bitmap in pixels.
23 : uint32 fWidth;
24 :
25 : // The height of the bitmap in pixels.
26 : uint32 fHeight;
27 :
28 : // The number of bytes between subsequent rows of the bitmap.
29 : uint32 fRowBytes;
30 :
31 : void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) {
32 : fConfig = bitmap.config();
33 : fWidth = bitmap.width();
34 : fHeight = bitmap.height();
35 : fRowBytes = bitmap.rowBytes();
36 : }
37 :
38 : // Returns whether |bitmap| successfully initialized.
39 : bool InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels,
40 : size_t total_pixels) const {
41 : if (total_pixels) {
42 : bitmap->setConfig(fConfig, fWidth, fHeight, fRowBytes);
43 : if (!bitmap->allocPixels())
44 : return false;
45 : if (total_pixels > bitmap->getSize())
46 : return false;
47 : memcpy(bitmap->getPixels(), pixels, total_pixels);
48 : }
49 : return true;
50 : }
51 : };
52 :
53 : } // namespace
54 :
55 :
56 : void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) {
57 : size_t fixed_size = sizeof(SkBitmap_Data);
58 : SkBitmap_Data bmp_data;
59 : bmp_data.InitSkBitmapDataForTransfer(p);
60 : m->WriteData(reinterpret_cast<const char*>(&bmp_data),
61 : static_cast<int>(fixed_size));
62 : size_t pixel_size = p.getSize();
63 : SkAutoLockPixels p_lock(p);
64 : m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
65 : static_cast<int>(pixel_size));
66 : }
67 :
68 : bool ParamTraits<SkBitmap>::Read(const Message* m, void** iter, SkBitmap* r) {
69 : const char* fixed_data;
70 : int fixed_data_size = 0;
71 : if (!m->ReadData(iter, &fixed_data, &fixed_data_size) ||
72 : (fixed_data_size <= 0)) {
73 : NOTREACHED();
74 : return false;
75 : }
76 : if (fixed_data_size != sizeof(SkBitmap_Data))
77 : return false; // Message is malformed.
78 :
79 : const char* variable_data;
80 : int variable_data_size = 0;
81 : if (!m->ReadData(iter, &variable_data, &variable_data_size) ||
82 : (variable_data_size < 0)) {
83 : NOTREACHED();
84 : return false;
85 : }
86 : const SkBitmap_Data* bmp_data =
87 : reinterpret_cast<const SkBitmap_Data*>(fixed_data);
88 : return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size);
89 : }
90 :
91 : void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::wstring* l) {
92 : l->append(StringPrintf(L"<SkBitmap>"));
93 : }
94 :
95 : #endif // EXCLUDE_SKIA_DEPENDENCIES
96 :
97 4392 : } // namespace IPC
|