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 "modules/shapedetection/ShapeDetector.h" | 5 #include "modules/shapedetection/ShapeDetector.h" |
6 | 6 |
7 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
8 #include "core/dom/DOMRect.h" | 8 #include "core/dom/DOMRect.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/frame/ImageBitmap.h" | 10 #include "core/frame/ImageBitmap.h" |
11 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
12 #include "core/html/HTMLImageElement.h" | 12 #include "core/html/HTMLImageElement.h" |
13 #include "core/html/HTMLVideoElement.h" | 13 #include "core/html/HTMLVideoElement.h" |
14 #include "core/html/ImageData.h" | 14 #include "core/html/ImageData.h" |
15 #include "core/loader/resource/ImageResourceContent.h" | 15 #include "core/loader/resource/ImageResourceContent.h" |
16 #include "platform/graphics/Image.h" | 16 #include "platform/graphics/Image.h" |
17 #include "third_party/skia/include/core/SkImage.h" | 17 #include "third_party/skia/include/core/SkImage.h" |
18 #include "third_party/skia/include/core/SkImageInfo.h" | 18 #include "third_party/skia/include/core/SkImageInfo.h" |
19 #include "wtf/CheckedNumeric.h" | 19 #include "wtf/CheckedNumeric.h" |
20 | 20 |
21 namespace blink { | 21 namespace blink { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 skia::mojom::blink::BitmapPtr createBitmapFromData(int width, | 25 mojo::ScopedSharedBufferHandle getSharedBufferOnData( |
26 int height, | 26 ScriptPromiseResolver* resolver, |
27 Vector<uint8_t> bitmapData) { | 27 uint8_t* data, |
28 skia::mojom::blink::BitmapPtr bitmap = skia::mojom::blink::Bitmap::New(); | 28 int size) { |
| 29 DCHECK(data); |
| 30 DCHECK(size); |
| 31 ScriptPromise promise = resolver->promise(); |
29 | 32 |
30 bitmap->color_type = (kN32_SkColorType == kRGBA_8888_SkColorType) | 33 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
31 ? skia::mojom::ColorType::RGBA_8888 | 34 mojo::SharedBufferHandle::Create(size); |
32 : skia::mojom::ColorType::BGRA_8888; | 35 if (!sharedBufferHandle->is_valid()) { |
33 bitmap->width = width; | 36 resolver->reject( |
34 bitmap->height = height; | 37 DOMException::create(InvalidStateError, "Internal allocation error")); |
35 bitmap->pixel_data = std::move(bitmapData); | 38 return sharedBufferHandle; |
| 39 } |
36 | 40 |
37 return bitmap; | 41 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 42 sharedBufferHandle->Map(size); |
| 43 DCHECK(mappedBuffer.get()); |
| 44 memcpy(mappedBuffer.get(), data, size); |
| 45 |
| 46 return sharedBufferHandle; |
38 } | 47 } |
39 | 48 |
40 } // anonymous namespace | 49 } // anonymous namespace |
41 | 50 |
42 ScriptPromise ShapeDetector::detect(ScriptState* scriptState, | 51 ScriptPromise ShapeDetector::detect(ScriptState* scriptState, |
43 const ImageBitmapSourceUnion& imageSource) { | 52 const ImageBitmapSourceUnion& imageSource) { |
44 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
45 ScriptPromise promise = resolver->promise(); | 54 ScriptPromise promise = resolver->promise(); |
46 | 55 |
47 // ImageDatas cannot be tainted by definition. | 56 // ImageDatas cannot be tainted by definition. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 pixelDataPtr = pixelData->data(); | 127 pixelDataPtr = pixelData->data(); |
119 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */; | 128 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */; |
120 } else { | 129 } else { |
121 // TODO(mcasas): retrieve the pixels from elsewhere. | 130 // TODO(mcasas): retrieve the pixels from elsewhere. |
122 NOTREACHED(); | 131 NOTREACHED(); |
123 resolver->reject(DOMException::create( | 132 resolver->reject(DOMException::create( |
124 InvalidStateError, "Failed to get pixels for current frame.")); | 133 InvalidStateError, "Failed to get pixels for current frame.")); |
125 return promise; | 134 return promise; |
126 } | 135 } |
127 | 136 |
128 WTF::Vector<uint8_t> bitmapData; | 137 mojo::ScopedSharedBufferHandle sharedBufferHandle = getSharedBufferOnData( |
129 bitmapData.append(pixelDataPtr, | 138 resolver, pixelDataPtr, allocationSize.ValueOrDefault(0)); |
130 static_cast<int>(allocationSize.ValueOrDefault(0))); | 139 if (!sharedBufferHandle->is_valid()) |
| 140 return promise; |
131 | 141 |
132 return doDetect(resolver, | 142 return doDetect(resolver, std::move(sharedBufferHandle), image->width(), |
133 createBitmapFromData(image->width(), image->height(), | 143 image->height()); |
134 std::move(bitmapData))); | |
135 } | 144 } |
136 | 145 |
137 ScriptPromise ShapeDetector::detectShapesOnImageData( | 146 ScriptPromise ShapeDetector::detectShapesOnImageData( |
138 ScriptPromiseResolver* resolver, | 147 ScriptPromiseResolver* resolver, |
139 ImageData* imageData) { | 148 ImageData* imageData) { |
140 ScriptPromise promise = resolver->promise(); | 149 ScriptPromise promise = resolver->promise(); |
141 | 150 |
142 if (imageData->size().isZero()) { | 151 if (imageData->size().isZero()) { |
143 resolver->resolve(HeapVector<Member<DOMRect>>()); | 152 resolver->resolve(HeapVector<Member<DOMRect>>()); |
144 return promise; | 153 return promise; |
145 } | 154 } |
146 | 155 |
147 uint8_t* const data = imageData->data()->data(); | 156 uint8_t* const data = imageData->data()->data(); |
148 WTF::CheckedNumeric<int> allocationSize = imageData->size().area() * 4; | 157 WTF::CheckedNumeric<int> allocationSize = imageData->size().area() * 4; |
149 WTF::Vector<uint8_t> bitmapData; | |
150 bitmapData.append(data, static_cast<int>(allocationSize.ValueOrDefault(0))); | |
151 | 158 |
152 return doDetect(resolver, | 159 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
153 createBitmapFromData(imageData->width(), imageData->height(), | 160 getSharedBufferOnData(resolver, data, allocationSize.ValueOrDefault(0)); |
154 std::move(bitmapData))); | 161 if (!sharedBufferHandle->is_valid()) |
| 162 return promise; |
| 163 |
| 164 return doDetect(resolver, std::move(sharedBufferHandle), imageData->width(), |
| 165 imageData->height()); |
155 } | 166 } |
156 | 167 |
157 ScriptPromise ShapeDetector::detectShapesOnImageElement( | 168 ScriptPromise ShapeDetector::detectShapesOnImageElement( |
158 ScriptPromiseResolver* resolver, | 169 ScriptPromiseResolver* resolver, |
159 const HTMLImageElement* img) { | 170 const HTMLImageElement* img) { |
160 ScriptPromise promise = resolver->promise(); | 171 ScriptPromise promise = resolver->promise(); |
161 | 172 |
162 if (img->bitmapSourceSize().isZero()) { | 173 if (img->bitmapSourceSize().isZero()) { |
163 resolver->resolve(HeapVector<Member<DOMRect>>()); | 174 resolver->resolve(HeapVector<Member<DOMRect>>()); |
164 return promise; | 175 return promise; |
(...skipping 18 matching lines...) Expand all Loading... |
183 DCHECK_EQ(img->naturalHeight(), static_cast<unsigned>(image->height())); | 194 DCHECK_EQ(img->naturalHeight(), static_cast<unsigned>(image->height())); |
184 | 195 |
185 if (!image) { | 196 if (!image) { |
186 resolver->reject(DOMException::create( | 197 resolver->reject(DOMException::create( |
187 InvalidStateError, "Failed to get image from current frame.")); | 198 InvalidStateError, "Failed to get image from current frame.")); |
188 return promise; | 199 return promise; |
189 } | 200 } |
190 | 201 |
191 const SkImageInfo skiaInfo = | 202 const SkImageInfo skiaInfo = |
192 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); | 203 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); |
193 size_t rowBytes = skiaInfo.minRowBytes(); | |
194 | 204 |
195 Vector<uint8_t> bitmapData(skiaInfo.getSafeSize(rowBytes)); | 205 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); |
196 const SkPixmap pixmap(skiaInfo, bitmapData.data(), rowBytes); | 206 |
| 207 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
| 208 mojo::SharedBufferHandle::Create(allocationSize); |
| 209 if (!sharedBufferHandle.is_valid()) { |
| 210 DLOG(ERROR) << "Requested allocation : " << allocationSize |
| 211 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB "; |
| 212 // TODO(xianglu): For now we reject the promise if the image is too large. |
| 213 // But consider resizing the image to remove restriction on the user side. |
| 214 // Also, add LayoutTests for this case later. |
| 215 resolver->reject( |
| 216 DOMException::create(InvalidStateError, "Image exceeds size limit.")); |
| 217 return promise; |
| 218 } |
| 219 |
| 220 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 221 sharedBufferHandle->Map(allocationSize); |
| 222 |
| 223 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); |
197 if (!image->readPixels(pixmap, 0, 0)) { | 224 if (!image->readPixels(pixmap, 0, 0)) { |
198 resolver->reject(DOMException::create( | 225 resolver->reject(DOMException::create( |
199 InvalidStateError, | 226 InvalidStateError, |
200 "Failed to read pixels: Unable to decompress or unsupported format.")); | 227 "Failed to read pixels: Unable to decompress or unsupported format.")); |
201 return promise; | 228 return promise; |
202 } | 229 } |
203 | 230 |
204 return doDetect( | 231 return doDetect(resolver, std::move(sharedBufferHandle), img->naturalWidth(), |
205 resolver, createBitmapFromData(img->naturalWidth(), img->naturalHeight(), | 232 img->naturalHeight()); |
206 std::move(bitmapData))); | |
207 } | 233 } |
208 | 234 |
209 } // namespace blink | 235 } // namespace blink |
OLD | NEW |