OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" | 5 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" |
6 | 6 |
7 namespace mojo { | 7 namespace mojo { |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 case kSRGB_SkColorProfileType: | 105 case kSRGB_SkColorProfileType: |
106 return skia::mojom::ColorProfileType::SRGB; | 106 return skia::mojom::ColorProfileType::SRGB; |
107 } | 107 } |
108 NOTREACHED(); | 108 NOTREACHED(); |
109 return skia::mojom::ColorProfileType::LINEAR; | 109 return skia::mojom::ColorProfileType::LINEAR; |
110 } | 110 } |
111 | 111 |
112 } // namespace | 112 } // namespace |
113 | 113 |
114 // static | 114 // static |
115 size_t ArrayTraits<BitmapBuffer>::GetSize(const BitmapBuffer& b) { | |
116 return b.size; | |
117 } | |
118 | |
119 // static | |
120 uint8_t* ArrayTraits<BitmapBuffer>::GetData(BitmapBuffer& b) { | |
121 return b.data; | |
122 } | |
123 | |
124 // static | |
125 const uint8_t* ArrayTraits<BitmapBuffer>::GetData(const BitmapBuffer& b) { | |
126 return b.data; | |
127 } | |
128 | |
129 // static | |
130 uint8_t& ArrayTraits<BitmapBuffer>::GetAt(BitmapBuffer& b, size_t i) { | |
131 return *(b.data + i); | |
132 } | |
133 | |
134 // static | |
135 const uint8_t& ArrayTraits<BitmapBuffer>::GetAt(const BitmapBuffer& b, | |
136 size_t i) { | |
137 return *(b.data + i); | |
138 } | |
139 | |
140 // static | |
141 bool ArrayTraits<BitmapBuffer>::Resize(BitmapBuffer& b, size_t size) { | |
142 return b.size == size; | |
143 } | |
144 | |
145 // static | |
146 bool StructTraits<skia::mojom::Bitmap, SkBitmap>::IsNull(const SkBitmap& b) { | 115 bool StructTraits<skia::mojom::Bitmap, SkBitmap>::IsNull(const SkBitmap& b) { |
147 return b.isNull(); | 116 return b.isNull(); |
148 } | 117 } |
149 | 118 |
150 // static | 119 // static |
151 void StructTraits<skia::mojom::Bitmap, SkBitmap>::SetToNull(SkBitmap* b) { | 120 void StructTraits<skia::mojom::Bitmap, SkBitmap>::SetToNull(SkBitmap* b) { |
152 b->reset(); | 121 b->reset(); |
153 } | 122 } |
154 | 123 |
155 // static | 124 // static |
(...skipping 21 matching lines...) Expand all Loading... | |
177 | 146 |
178 // static | 147 // static |
179 uint32_t StructTraits<skia::mojom::Bitmap, SkBitmap>::height( | 148 uint32_t StructTraits<skia::mojom::Bitmap, SkBitmap>::height( |
180 const SkBitmap& b) { | 149 const SkBitmap& b) { |
181 return b.height() < 0 ? 0 : static_cast<uint32_t>(b.height()); | 150 return b.height() < 0 ? 0 : static_cast<uint32_t>(b.height()); |
182 } | 151 } |
183 | 152 |
184 // static | 153 // static |
185 BitmapBuffer StructTraits<skia::mojom::Bitmap, SkBitmap>::pixel_data( | 154 BitmapBuffer StructTraits<skia::mojom::Bitmap, SkBitmap>::pixel_data( |
186 const SkBitmap& b) { | 155 const SkBitmap& b) { |
187 BitmapBuffer bitmap_buffer; | 156 BitmapBuffer bitmap_buffer = {b.getSize(), b.getSize(), |
Tom Sepez
2016/06/08 17:38:47
nit: same here.
Fady Samuel
2016/06/08 17:48:03
Done.
| |
188 bitmap_buffer.data = static_cast<uint8_t*>(b.getPixels()); | 157 static_cast<uint8_t*>(b.getPixels())}; |
189 bitmap_buffer.size = b.getSize(); | |
190 return bitmap_buffer; | 158 return bitmap_buffer; |
191 } | 159 } |
192 | 160 |
193 // static | 161 // static |
194 bool StructTraits<skia::mojom::Bitmap, SkBitmap>::Read( | 162 bool StructTraits<skia::mojom::Bitmap, SkBitmap>::Read( |
195 skia::mojom::BitmapDataView data, | 163 skia::mojom::BitmapDataView data, |
196 SkBitmap* b) { | 164 SkBitmap* b) { |
197 // TODO: Ensure width and height are reasonable, eg. <= kMaxBitmapSize? | 165 // TODO: Ensure width and height are reasonable, eg. <= kMaxBitmapSize? |
198 *b = SkBitmap(); | 166 *b = SkBitmap(); |
199 if (!b->tryAllocPixels(SkImageInfo::Make( | 167 if (!b->tryAllocPixels(SkImageInfo::Make( |
200 data.width(), data.height(), MojoColorTypeToSk(data.color_type()), | 168 data.width(), data.height(), MojoColorTypeToSk(data.color_type()), |
201 MojoAlphaTypeToSk(data.alpha_type()), | 169 MojoAlphaTypeToSk(data.alpha_type()), |
202 MojoProfileTypeToSk(data.profile_type())))) { | 170 MojoProfileTypeToSk(data.profile_type())))) { |
203 return false; | 171 return false; |
204 } | 172 } |
205 | 173 |
206 // If the image is empty, return success after setting the image info. | 174 // If the image is empty, return success after setting the image info. |
207 if (data.width() == 0 || data.height() == 0) | 175 if (data.width() == 0 || data.height() == 0) |
208 return true; | 176 return true; |
209 | 177 |
210 SkAutoPixmapUnlock pixmap; | 178 SkAutoPixmapUnlock pixmap; |
211 if (static_cast<uint32_t>(b->width()) != data.width() || | 179 if (static_cast<uint32_t>(b->width()) != data.width() || |
212 static_cast<uint32_t>(b->height()) != data.height() || | 180 static_cast<uint32_t>(b->height()) != data.height() || |
213 !b->requestLock(&pixmap) || !b->lockPixelsAreWritable() || | 181 !b->requestLock(&pixmap) || !b->lockPixelsAreWritable() || |
214 !b->readyToDraw()) { | 182 !b->readyToDraw()) { |
215 return false; | 183 return false; |
216 } | 184 } |
217 | 185 |
218 BitmapBuffer bitmap_buffer; | 186 BitmapBuffer bitmap_buffer = {0, b->getSize(), |
219 bitmap_buffer.data = static_cast<uint8_t*>(b->getPixels()); | 187 static_cast<uint8_t*>(b->getPixels())}; |
220 bitmap_buffer.size = b->getSize(); | 188 if (!data.ReadPixelData(&bitmap_buffer) || bitmap_buffer.size != b->getSize()) |
221 if (!data.ReadPixelData(&bitmap_buffer)) | |
222 return false; | 189 return false; |
223 | 190 |
224 b->notifyPixelsChanged(); | 191 b->notifyPixelsChanged(); |
225 return true; | 192 return true; |
226 } | 193 } |
227 | 194 |
228 // static | 195 // static |
229 void* StructTraits<skia::mojom::Bitmap, SkBitmap>::SetUpContext( | 196 void* StructTraits<skia::mojom::Bitmap, SkBitmap>::SetUpContext( |
230 const SkBitmap& b) { | 197 const SkBitmap& b) { |
231 b.lockPixels(); | 198 b.lockPixels(); |
232 return nullptr; | 199 return nullptr; |
233 } | 200 } |
234 | 201 |
235 // static | 202 // static |
236 void StructTraits<skia::mojom::Bitmap, SkBitmap>::TearDownContext( | 203 void StructTraits<skia::mojom::Bitmap, SkBitmap>::TearDownContext( |
237 const SkBitmap& b, | 204 const SkBitmap& b, |
238 void* context) { | 205 void* context) { |
239 b.unlockPixels(); | 206 b.unlockPixels(); |
240 } | 207 } |
241 | 208 |
242 } // namespace mojo | 209 } // namespace mojo |
OLD | NEW |