OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "core/frame/ImageBitmap.h" | 5 #include "core/frame/ImageBitmap.h" |
6 | 6 |
7 #include "core/html/HTMLCanvasElement.h" | 7 #include "core/html/HTMLCanvasElement.h" |
8 #include "core/html/HTMLVideoElement.h" | 8 #include "core/html/HTMLVideoElement.h" |
9 #include "core/html/ImageData.h" | 9 #include "core/html/ImageData.h" |
10 #include "platform/graphics/skia/SkiaUtils.h" | 10 #include "platform/graphics/skia/SkiaUtils.h" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 // canvas is always premultiplied, so set the last parameter to true and con vert to un-premul later | 251 // canvas is always premultiplied, so set the last parameter to true and con vert to un-premul later |
252 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect, flipY, true); | 252 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect, flipY, true); |
253 if (!m_image) | 253 if (!m_image) |
254 return; | 254 return; |
255 if (!premultiplyAlpha) | 255 if (!premultiplyAlpha) |
256 m_image = StaticBitmapImage::create(premulSkImageToUnPremul(m_image->ima geForCurrentFrame().get())); | 256 m_image = StaticBitmapImage::create(premulSkImageToUnPremul(m_image->ima geForCurrentFrame().get())); |
257 m_image->setOriginClean(canvas->originClean()); | 257 m_image->setOriginClean(canvas->originClean()); |
258 m_image->setPremultiplied(premultiplyAlpha); | 258 m_image->setPremultiplied(premultiplyAlpha); |
259 } | 259 } |
260 | 260 |
261 // The last two parameters are used for structure-cloning. | 261 // This constructor is called by structured-cloning an ImageBitmap. |
262 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options, const bool& isImageDataPremultiplied, const bool& isImageD ataOriginClean) | 262 // isImageBitmapPremultiplied indicates whether the original ImageBitmap is prem ultiplied or not. |
jbroman
2016/06/28 18:50:22
Comments like this normally go in the header, sinc
xidachen
2016/06/29 12:58:35
Done.
| |
263 // isImageBitmapOriginClean indicates whether the original ImageBitmap is origin clean or not. | |
264 ImageBitmap::ImageBitmap(std::unique_ptr<uint8_t[]> data, uint32_t width, uint32 _t height, bool isImageBitmapPremultiplied, bool isImageBitmapOriginClean) | |
265 { | |
266 SkImageInfo info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, isImageBitmapPremultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); | |
267 swizzleImageData(data.get(), height, info.bytesPerPixel() * width, false); | |
jbroman
2016/06/28 18:50:22
What's the purpose of this swizzling? Why aren't w
xidachen
2016/06/29 12:58:35
Done.
| |
268 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy(SkPixma p(info, data.get(), info.bytesPerPixel() * width)))); | |
269 swizzleImageData(data.get(), height, info.bytesPerPixel() * width, false); | |
jbroman
2016/06/28 18:50:22
Why unswizzle the data when we're about to delete
xidachen
2016/06/29 12:58:35
Done.
| |
270 m_image->setPremultiplied(isImageBitmapPremultiplied); | |
271 m_image->setOriginClean(isImageBitmapOriginClean); | |
272 } | |
273 | |
274 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) | |
263 { | 275 { |
264 bool flipY; | 276 bool flipY; |
265 bool premultiplyAlpha; | 277 bool premultiplyAlpha; |
266 parseOptions(options, flipY, premultiplyAlpha); | 278 parseOptions(options, flipY, premultiplyAlpha); |
267 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 279 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
268 | 280 |
269 // treat non-premultiplyAlpha as a special case | 281 // treat non-premultiplyAlpha as a special case |
270 if (!premultiplyAlpha) { | 282 if (!premultiplyAlpha) { |
271 unsigned char* srcAddr = data->data()->data(); | 283 unsigned char* srcAddr = data->data()->data(); |
272 int srcHeight = data->size().height(); | 284 int srcHeight = data->size().height(); |
273 int dstHeight = cropRect.height(); | 285 int dstHeight = cropRect.height(); |
274 // TODO (xidachen): skia doesn't support SkImage::NewRasterCopy from a k RGBA color type. | 286 // TODO (xidachen): skia doesn't support SkImage::NewRasterCopy from a k RGBA color type. |
275 // For now, we swap R and B channel and uses kBGRA color type. | 287 // For now, we swap R and B channel and uses kBGRA color type. |
276 SkImageInfo info; | 288 SkImageInfo info = SkImageInfo::Make(cropRect.width(), dstHeight, kBGRA_ 8888_SkColorType, kUnpremul_SkAlphaType); |
277 if (!isImageDataPremultiplied) | |
278 info = SkImageInfo::Make(cropRect.width(), dstHeight, kBGRA_8888_SkC olorType, kUnpremul_SkAlphaType); | |
279 else | |
280 info = SkImageInfo::Make(cropRect.width(), dstHeight, kBGRA_8888_SkC olorType, kPremul_SkAlphaType); | |
281 int srcPixelBytesPerRow = info.bytesPerPixel() * data->size().width(); | 289 int srcPixelBytesPerRow = info.bytesPerPixel() * data->size().width(); |
282 int dstPixelBytesPerRow = info.bytesPerPixel() * cropRect.width(); | 290 int dstPixelBytesPerRow = info.bytesPerPixel() * cropRect.width(); |
283 if (cropRect == IntRect(IntPoint(), data->size())) { | 291 if (cropRect == IntRect(IntPoint(), data->size())) { |
284 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); | 292 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); |
285 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy (SkPixmap(info, srcAddr, dstPixelBytesPerRow)))); | 293 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy (SkPixmap(info, srcAddr, dstPixelBytesPerRow)))); |
286 // restore the original ImageData | 294 // restore the original ImageData |
287 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); | 295 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); |
288 } else { | 296 } else { |
289 std::unique_ptr<uint8_t[]> copiedDataBuffer = wrapArrayUnique(new ui nt8_t[dstHeight * dstPixelBytesPerRow]()); | 297 std::unique_ptr<uint8_t[]> copiedDataBuffer = wrapArrayUnique(new ui nt8_t[dstHeight * dstPixelBytesPerRow]()); |
290 if (!srcRect.isEmpty()) { | 298 if (!srcRect.isEmpty()) { |
(...skipping 19 matching lines...) Expand all Loading... | |
310 else if (j % 4 == 2) | 318 else if (j % 4 == 2) |
311 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j - 2]; | 319 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j - 2]; |
312 else | 320 else |
313 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j]; | 321 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j]; |
314 } | 322 } |
315 } | 323 } |
316 } | 324 } |
317 m_image = StaticBitmapImage::create(newSkImageFromRaster(info, std:: move(copiedDataBuffer), dstPixelBytesPerRow)); | 325 m_image = StaticBitmapImage::create(newSkImageFromRaster(info, std:: move(copiedDataBuffer), dstPixelBytesPerRow)); |
318 } | 326 } |
319 m_image->setPremultiplied(premultiplyAlpha); | 327 m_image->setPremultiplied(premultiplyAlpha); |
320 m_image->setOriginClean(isImageDataOriginClean); | |
321 return; | 328 return; |
322 } | 329 } |
323 | 330 |
324 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), N onOpaque, DoNotInitializeImagePixels); | 331 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), N onOpaque, DoNotInitializeImagePixels); |
325 if (!buffer) | 332 if (!buffer) |
326 return; | 333 return; |
327 | 334 |
328 if (srcRect.isEmpty()) { | 335 if (srcRect.isEmpty()) { |
329 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration, SnapshotReasonUnknown)); | 336 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration, SnapshotReasonUnknown)); |
330 return; | 337 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 IntRect normalizedCropRect = normalizeRect(cropRect); | 405 IntRect normalizedCropRect = normalizeRect(cropRect); |
399 return new ImageBitmap(video, normalizedCropRect, document, options); | 406 return new ImageBitmap(video, normalizedCropRect, document, options); |
400 } | 407 } |
401 | 408 |
402 ImageBitmap* ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropR ect, const ImageBitmapOptions& options) | 409 ImageBitmap* ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropR ect, const ImageBitmapOptions& options) |
403 { | 410 { |
404 IntRect normalizedCropRect = normalizeRect(cropRect); | 411 IntRect normalizedCropRect = normalizeRect(cropRect); |
405 return new ImageBitmap(canvas, normalizedCropRect, options); | 412 return new ImageBitmap(canvas, normalizedCropRect, options); |
406 } | 413 } |
407 | 414 |
408 ImageBitmap* ImageBitmap::create(ImageData* data, const IntRect& cropRect, const ImageBitmapOptions& options, const bool& isImageDataPremultiplied, const bool& isImageDataOriginClean) | 415 ImageBitmap* ImageBitmap::create(ImageData* data, const IntRect& cropRect, const ImageBitmapOptions& options) |
409 { | 416 { |
410 IntRect normalizedCropRect = normalizeRect(cropRect); | 417 IntRect normalizedCropRect = normalizeRect(cropRect); |
411 return new ImageBitmap(data, normalizedCropRect, options, isImageDataPremult iplied, isImageDataOriginClean); | 418 return new ImageBitmap(data, normalizedCropRect, options); |
412 } | 419 } |
413 | 420 |
414 ImageBitmap* ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect, c onst ImageBitmapOptions& options) | 421 ImageBitmap* ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect, c onst ImageBitmapOptions& options) |
415 { | 422 { |
416 IntRect normalizedCropRect = normalizeRect(cropRect); | 423 IntRect normalizedCropRect = normalizeRect(cropRect); |
417 return new ImageBitmap(bitmap, normalizedCropRect, options); | 424 return new ImageBitmap(bitmap, normalizedCropRect, options); |
418 } | 425 } |
419 | 426 |
420 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image, const IntR ect& cropRect, const ImageBitmapOptions& options) | 427 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image, const IntR ect& cropRect, const ImageBitmapOptions& options) |
421 { | 428 { |
422 IntRect normalizedCropRect = normalizeRect(cropRect); | 429 IntRect normalizedCropRect = normalizeRect(cropRect); |
423 return new ImageBitmap(image, normalizedCropRect, options); | 430 return new ImageBitmap(image, normalizedCropRect, options); |
424 } | 431 } |
425 | 432 |
426 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image) | 433 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image) |
427 { | 434 { |
428 return new ImageBitmap(image); | 435 return new ImageBitmap(image); |
429 } | 436 } |
430 | 437 |
431 ImageBitmap* ImageBitmap::create(WebExternalTextureMailbox& mailbox) | 438 ImageBitmap* ImageBitmap::create(WebExternalTextureMailbox& mailbox) |
432 { | 439 { |
433 return new ImageBitmap(mailbox); | 440 return new ImageBitmap(mailbox); |
434 } | 441 } |
435 | 442 |
443 ImageBitmap* ImageBitmap::create(std::unique_ptr<uint8_t[]> data, uint32_t width , uint32_t height, bool isImageBitmapPremultiplied, bool isImageBitmapOriginClea n) | |
444 { | |
445 return new ImageBitmap(std::move(data), width, height, isImageBitmapPremulti plied, isImageBitmapOriginClean); | |
446 } | |
447 | |
436 void ImageBitmap::close() | 448 void ImageBitmap::close() |
437 { | 449 { |
438 if (!m_image || m_isNeutered) | 450 if (!m_image || m_isNeutered) |
439 return; | 451 return; |
440 m_image.clear(); | 452 m_image.clear(); |
441 m_isNeutered = true; | 453 m_isNeutered = true; |
442 } | 454 } |
443 | 455 |
444 // static | 456 // static |
445 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) | 457 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 FloatSize ImageBitmap::elementSize(const FloatSize&) const | 533 FloatSize ImageBitmap::elementSize(const FloatSize&) const |
522 { | 534 { |
523 return FloatSize(width(), height()); | 535 return FloatSize(width(), height()); |
524 } | 536 } |
525 | 537 |
526 DEFINE_TRACE(ImageBitmap) | 538 DEFINE_TRACE(ImageBitmap) |
527 { | 539 { |
528 } | 540 } |
529 | 541 |
530 } // namespace blink | 542 } // namespace blink |
OLD | NEW |