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" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 canvasImageSource->sourceHeight()); | 100 canvasImageSource->sourceHeight()); |
101 | 101 |
102 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; | 102 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus; |
103 RefPtr<Image> image = canvasImageSource->getSourceImageForCanvas( | 103 RefPtr<Image> image = canvasImageSource->getSourceImageForCanvas( |
104 &sourceImageStatus, PreferNoAcceleration, SnapshotReasonDrawImage, size); | 104 &sourceImageStatus, PreferNoAcceleration, SnapshotReasonDrawImage, size); |
105 if (!image || sourceImageStatus != NormalSourceImageStatus) { | 105 if (!image || sourceImageStatus != NormalSourceImageStatus) { |
106 resolver->reject( | 106 resolver->reject( |
107 DOMException::create(InvalidStateError, "Invalid element or state.")); | 107 DOMException::create(InvalidStateError, "Invalid element or state.")); |
108 return promise; | 108 return promise; |
109 } | 109 } |
| 110 if (size.isEmpty()) { |
| 111 resolver->resolve(HeapVector<Member<DOMRect>>()); |
| 112 return promise; |
| 113 } |
110 | 114 |
111 SkPixmap pixmap; | 115 SkPixmap pixmap; |
112 RefPtr<Uint8Array> pixelData; | 116 RefPtr<Uint8Array> pixelData; |
113 uint8_t* pixelDataPtr = nullptr; | 117 uint8_t* pixelDataPtr = nullptr; |
114 WTF::CheckedNumeric<int> allocationSize = 0; | 118 WTF::CheckedNumeric<int> allocationSize = 0; |
115 | 119 |
116 // TODO(ccameron): ShapeDetector can ignore color conversion. | 120 // TODO(ccameron): ShapeDetector can ignore color conversion. |
117 sk_sp<SkImage> skImage = | 121 sk_sp<SkImage> skImage = |
118 image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); | 122 image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); |
119 // Use |skImage|'s pixels if it has direct access to them. | 123 // Use |skImage|'s pixels if it has direct access to them. |
(...skipping 23 matching lines...) Expand all Loading... |
143 | 147 |
144 return doDetect(resolver, std::move(sharedBufferHandle), image->width(), | 148 return doDetect(resolver, std::move(sharedBufferHandle), image->width(), |
145 image->height()); | 149 image->height()); |
146 } | 150 } |
147 | 151 |
148 ScriptPromise ShapeDetector::detectShapesOnImageData( | 152 ScriptPromise ShapeDetector::detectShapesOnImageData( |
149 ScriptPromiseResolver* resolver, | 153 ScriptPromiseResolver* resolver, |
150 ImageData* imageData) { | 154 ImageData* imageData) { |
151 ScriptPromise promise = resolver->promise(); | 155 ScriptPromise promise = resolver->promise(); |
152 | 156 |
| 157 if (imageData->size().isZero()) { |
| 158 resolver->resolve(HeapVector<Member<DOMRect>>()); |
| 159 return promise; |
| 160 } |
| 161 |
153 uint8_t* const data = imageData->data()->data(); | 162 uint8_t* const data = imageData->data()->data(); |
154 WTF::CheckedNumeric<int> allocationSize = imageData->size().area() * 4; | 163 WTF::CheckedNumeric<int> allocationSize = imageData->size().area() * 4; |
155 | 164 |
156 mojo::ScopedSharedBufferHandle sharedBufferHandle = | 165 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
157 getSharedBufferOnData(resolver, data, allocationSize.ValueOrDefault(0)); | 166 getSharedBufferOnData(resolver, data, allocationSize.ValueOrDefault(0)); |
158 if (!sharedBufferHandle->is_valid()) | 167 if (!sharedBufferHandle->is_valid()) |
159 return promise; | 168 return promise; |
160 | 169 |
161 return doDetect(resolver, std::move(sharedBufferHandle), imageData->width(), | 170 return doDetect(resolver, std::move(sharedBufferHandle), imageData->width(), |
162 imageData->height()); | 171 imageData->height()); |
163 } | 172 } |
164 | 173 |
165 ScriptPromise ShapeDetector::detectShapesOnImageElement( | 174 ScriptPromise ShapeDetector::detectShapesOnImageElement( |
166 ScriptPromiseResolver* resolver, | 175 ScriptPromiseResolver* resolver, |
167 const HTMLImageElement* img) { | 176 const HTMLImageElement* img) { |
168 ScriptPromise promise = resolver->promise(); | 177 ScriptPromise promise = resolver->promise(); |
169 | 178 |
170 // TODO(mcasas): reconsider this resolve(), https://crbug.com/674306. | |
171 if (img->bitmapSourceSize().isZero()) { | 179 if (img->bitmapSourceSize().isZero()) { |
172 resolver->resolve(HeapVector<Member<DOMRect>>()); | 180 resolver->resolve(HeapVector<Member<DOMRect>>()); |
173 return promise; | 181 return promise; |
174 } | 182 } |
175 | 183 |
176 ImageResourceContent* const imageResource = img->cachedImage(); | 184 ImageResourceContent* const imageResource = img->cachedImage(); |
177 if (!imageResource || imageResource->errorOccurred()) { | 185 if (!imageResource || imageResource->errorOccurred()) { |
178 resolver->reject(DOMException::create( | 186 resolver->reject(DOMException::create( |
179 InvalidStateError, "Failed to load or decode HTMLImageElement.")); | 187 InvalidStateError, "Failed to load or decode HTMLImageElement.")); |
180 return promise; | 188 return promise; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 InvalidStateError, | 234 InvalidStateError, |
227 "Failed to read pixels: Unable to decompress or unsupported format.")); | 235 "Failed to read pixels: Unable to decompress or unsupported format.")); |
228 return promise; | 236 return promise; |
229 } | 237 } |
230 | 238 |
231 return doDetect(resolver, std::move(sharedBufferHandle), img->naturalWidth(), | 239 return doDetect(resolver, std::move(sharedBufferHandle), img->naturalWidth(), |
232 img->naturalHeight()); | 240 img->naturalHeight()); |
233 } | 241 } |
234 | 242 |
235 } // namespace blink | 243 } // namespace blink |
OLD | NEW |