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 ImageBitmap::ImageBitmap(std::unique_ptr<uint8_t[]> data, uint32_t width, uint32 _t height, bool isImageBitmapPremultiplied, bool isImageBitmapOriginClean) |
262 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options, const bool& isImageDataPremultiplied, const bool& isImageD ataOriginClean) | 262 { |
263 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, isImag eBitmapPremultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); | |
jbroman
2016/06/29 18:03:53
super-nit: this can be written slightly shorter:
| |
264 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy(SkPixma p(info, data.get(), info.bytesPerPixel() * width)))); | |
265 m_image->setPremultiplied(isImageBitmapPremultiplied); | |
266 m_image->setOriginClean(isImageBitmapOriginClean); | |
267 } | |
268 | |
269 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) | |
263 { | 270 { |
264 bool flipY; | 271 bool flipY; |
265 bool premultiplyAlpha; | 272 bool premultiplyAlpha; |
266 parseOptions(options, flipY, premultiplyAlpha); | 273 parseOptions(options, flipY, premultiplyAlpha); |
267 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 274 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
268 | 275 |
269 // treat non-premultiplyAlpha as a special case | 276 // treat non-premultiplyAlpha as a special case |
270 if (!premultiplyAlpha) { | 277 if (!premultiplyAlpha) { |
271 unsigned char* srcAddr = data->data()->data(); | 278 unsigned char* srcAddr = data->data()->data(); |
272 int srcHeight = data->size().height(); | 279 int srcHeight = data->size().height(); |
273 int dstHeight = cropRect.height(); | 280 int dstHeight = cropRect.height(); |
274 // TODO (xidachen): skia doesn't support SkImage::NewRasterCopy from a k RGBA color type. | 281 // 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. | 282 // For now, we swap R and B channel and uses kBGRA color type. |
276 SkImageInfo info; | 283 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(); | 284 int srcPixelBytesPerRow = info.bytesPerPixel() * data->size().width(); |
282 int dstPixelBytesPerRow = info.bytesPerPixel() * cropRect.width(); | 285 int dstPixelBytesPerRow = info.bytesPerPixel() * cropRect.width(); |
283 if (cropRect == IntRect(IntPoint(), data->size())) { | 286 if (cropRect == IntRect(IntPoint(), data->size())) { |
284 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); | 287 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); |
285 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy (SkPixmap(info, srcAddr, dstPixelBytesPerRow)))); | 288 m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy (SkPixmap(info, srcAddr, dstPixelBytesPerRow)))); |
286 // restore the original ImageData | 289 // restore the original ImageData |
287 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); | 290 swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, flipY); |
288 } else { | 291 } else { |
289 std::unique_ptr<uint8_t[]> copiedDataBuffer = wrapArrayUnique(new ui nt8_t[dstHeight * dstPixelBytesPerRow]()); | 292 std::unique_ptr<uint8_t[]> copiedDataBuffer = wrapArrayUnique(new ui nt8_t[dstHeight * dstPixelBytesPerRow]()); |
290 if (!srcRect.isEmpty()) { | 293 if (!srcRect.isEmpty()) { |
(...skipping 19 matching lines...) Expand all Loading... | |
310 else if (j % 4 == 2) | 313 else if (j % 4 == 2) |
311 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j - 2]; | 314 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j - 2]; |
312 else | 315 else |
313 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j]; | 316 copiedDataBuffer[dstStartCopyPosition + j] = srcAddr [srcStartCopyPosition + j]; |
314 } | 317 } |
315 } | 318 } |
316 } | 319 } |
317 m_image = StaticBitmapImage::create(newSkImageFromRaster(info, std:: move(copiedDataBuffer), dstPixelBytesPerRow)); | 320 m_image = StaticBitmapImage::create(newSkImageFromRaster(info, std:: move(copiedDataBuffer), dstPixelBytesPerRow)); |
318 } | 321 } |
319 m_image->setPremultiplied(premultiplyAlpha); | 322 m_image->setPremultiplied(premultiplyAlpha); |
320 m_image->setOriginClean(isImageDataOriginClean); | |
321 return; | 323 return; |
322 } | 324 } |
323 | 325 |
324 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), N onOpaque, DoNotInitializeImagePixels); | 326 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), N onOpaque, DoNotInitializeImagePixels); |
325 if (!buffer) | 327 if (!buffer) |
326 return; | 328 return; |
327 | 329 |
328 if (srcRect.isEmpty()) { | 330 if (srcRect.isEmpty()) { |
329 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration, SnapshotReasonUnknown)); | 331 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration, SnapshotReasonUnknown)); |
330 return; | 332 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 IntRect normalizedCropRect = normalizeRect(cropRect); | 400 IntRect normalizedCropRect = normalizeRect(cropRect); |
399 return new ImageBitmap(video, normalizedCropRect, document, options); | 401 return new ImageBitmap(video, normalizedCropRect, document, options); |
400 } | 402 } |
401 | 403 |
402 ImageBitmap* ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropR ect, const ImageBitmapOptions& options) | 404 ImageBitmap* ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropR ect, const ImageBitmapOptions& options) |
403 { | 405 { |
404 IntRect normalizedCropRect = normalizeRect(cropRect); | 406 IntRect normalizedCropRect = normalizeRect(cropRect); |
405 return new ImageBitmap(canvas, normalizedCropRect, options); | 407 return new ImageBitmap(canvas, normalizedCropRect, options); |
406 } | 408 } |
407 | 409 |
408 ImageBitmap* ImageBitmap::create(ImageData* data, const IntRect& cropRect, const ImageBitmapOptions& options, const bool& isImageDataPremultiplied, const bool& isImageDataOriginClean) | 410 ImageBitmap* ImageBitmap::create(ImageData* data, const IntRect& cropRect, const ImageBitmapOptions& options) |
409 { | 411 { |
410 IntRect normalizedCropRect = normalizeRect(cropRect); | 412 IntRect normalizedCropRect = normalizeRect(cropRect); |
411 return new ImageBitmap(data, normalizedCropRect, options, isImageDataPremult iplied, isImageDataOriginClean); | 413 return new ImageBitmap(data, normalizedCropRect, options); |
412 } | 414 } |
413 | 415 |
414 ImageBitmap* ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect, c onst ImageBitmapOptions& options) | 416 ImageBitmap* ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect, c onst ImageBitmapOptions& options) |
415 { | 417 { |
416 IntRect normalizedCropRect = normalizeRect(cropRect); | 418 IntRect normalizedCropRect = normalizeRect(cropRect); |
417 return new ImageBitmap(bitmap, normalizedCropRect, options); | 419 return new ImageBitmap(bitmap, normalizedCropRect, options); |
418 } | 420 } |
419 | 421 |
420 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image, const IntR ect& cropRect, const ImageBitmapOptions& options) | 422 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image, const IntR ect& cropRect, const ImageBitmapOptions& options) |
421 { | 423 { |
422 IntRect normalizedCropRect = normalizeRect(cropRect); | 424 IntRect normalizedCropRect = normalizeRect(cropRect); |
423 return new ImageBitmap(image, normalizedCropRect, options); | 425 return new ImageBitmap(image, normalizedCropRect, options); |
424 } | 426 } |
425 | 427 |
426 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image) | 428 ImageBitmap* ImageBitmap::create(PassRefPtr<StaticBitmapImage> image) |
427 { | 429 { |
428 return new ImageBitmap(image); | 430 return new ImageBitmap(image); |
429 } | 431 } |
430 | 432 |
431 ImageBitmap* ImageBitmap::create(WebExternalTextureMailbox& mailbox) | 433 ImageBitmap* ImageBitmap::create(WebExternalTextureMailbox& mailbox) |
432 { | 434 { |
433 return new ImageBitmap(mailbox); | 435 return new ImageBitmap(mailbox); |
434 } | 436 } |
435 | 437 |
438 ImageBitmap* ImageBitmap::create(std::unique_ptr<uint8_t[]> data, uint32_t width , uint32_t height, bool isImageBitmapPremultiplied, bool isImageBitmapOriginClea n) | |
439 { | |
440 return new ImageBitmap(std::move(data), width, height, isImageBitmapPremulti plied, isImageBitmapOriginClean); | |
441 } | |
442 | |
436 void ImageBitmap::close() | 443 void ImageBitmap::close() |
437 { | 444 { |
438 if (!m_image || m_isNeutered) | 445 if (!m_image || m_isNeutered) |
439 return; | 446 return; |
440 m_image.clear(); | 447 m_image.clear(); |
441 m_isNeutered = true; | 448 m_isNeutered = true; |
442 } | 449 } |
443 | 450 |
444 // static | 451 // static |
445 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) | 452 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) |
446 { | 453 { |
447 return ImageBitmap::create(StaticBitmapImage::create(fromSkSp(image))); | 454 return ImageBitmap::create(StaticBitmapImage::create(fromSkSp(image))); |
448 } | 455 } |
449 | 456 |
450 std::unique_ptr<uint8_t[]> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp) | 457 std::unique_ptr<uint8_t[]> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp, DataColorFormat format) |
451 { | 458 { |
452 SkImageInfo info = SkImageInfo::Make(width(), height(), kRGBA_8888_SkColorTy pe, (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType) ; | 459 SkImageInfo info = SkImageInfo::Make(width(), height(), (format == RGBAColor Type) ? kRGBA_8888_SkColorType : kN32_SkColorType, (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); |
453 std::unique_ptr<uint8_t[]> dstPixels = copySkImageData(m_image->imageForCurr entFrame().get(), info); | 460 std::unique_ptr<uint8_t[]> dstPixels = copySkImageData(m_image->imageForCurr entFrame().get(), info); |
454 return dstPixels; | 461 return dstPixels; |
455 } | 462 } |
456 | 463 |
457 unsigned long ImageBitmap::width() const | 464 unsigned long ImageBitmap::width() const |
458 { | 465 { |
459 if (!m_image) | 466 if (!m_image) |
460 return 0; | 467 return 0; |
461 ASSERT(m_image->width() > 0); | 468 ASSERT(m_image->width() > 0); |
462 return m_image->width(); | 469 return m_image->width(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 FloatSize ImageBitmap::elementSize(const FloatSize&) const | 528 FloatSize ImageBitmap::elementSize(const FloatSize&) const |
522 { | 529 { |
523 return FloatSize(width(), height()); | 530 return FloatSize(width(), height()); |
524 } | 531 } |
525 | 532 |
526 DEFINE_TRACE(ImageBitmap) | 533 DEFINE_TRACE(ImageBitmap) |
527 { | 534 { |
528 } | 535 } |
529 | 536 |
530 } // namespace blink | 537 } // namespace blink |
OLD | NEW |