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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp

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

Powered by Google App Engine
This is Rietveld 408576698