1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef WebrtcImageBuffer_h__
8 #define WebrtcImageBuffer_h__
9 
10 #include "webrtc/common_video/include/video_frame_buffer.h"
11 #include "webrtc/rtc_base/keep_ref_until_done.h"
12 
13 namespace mozilla {
14 namespace layers {
15 class Image;
16 }
17 
18 class ImageBuffer : public webrtc::VideoFrameBuffer {
19  public:
ImageBuffer(RefPtr<layers::Image> && aImage)20   explicit ImageBuffer(RefPtr<layers::Image>&& aImage)
21       : mImage(std::move(aImage)) {}
22 
ToI420()23   rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override {
24     RefPtr<layers::PlanarYCbCrImage> image = mImage->AsPlanarYCbCrImage();
25     MOZ_ASSERT(image);
26     if (!image) {
27       // TODO. YUV420 ReadBack, Image only provides a RGB readback.
28       return nullptr;
29     }
30     const layers::PlanarYCbCrData* data = image->GetData();
31     rtc::scoped_refptr<webrtc::I420BufferInterface> buf(
32         new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
33             data->mPicSize.width, data->mPicSize.height, data->mYChannel,
34             data->mYStride, data->mCbChannel, data->mCbCrStride,
35             data->mCrChannel, data->mCbCrStride,
36             rtc::KeepRefUntilDone(image.get())));
37     return buf;
38   }
39 
type()40   Type type() const override { return Type::kNative; }
41 
width()42   int width() const override { return mImage->GetSize().width; }
43 
height()44   int height() const override { return mImage->GetSize().height; }
45 
GetNativeImage()46   RefPtr<layers::Image> GetNativeImage() const { return mImage; }
47 
48  private:
49   const RefPtr<layers::Image> mImage;
50 };
51 
52 }  // namespace mozilla
53 
54 #endif  // WebrtcImageBuffer_h__
55