Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/blink/skcanvas_video_renderer.h" | 5 #include "media/blink/skcanvas_video_renderer.h" |
| 6 | 6 |
| 7 #include "gpu/GLES2/gl2extchromium.h" | 7 #include "gpu/GLES2/gl2extchromium.h" |
| 8 #include "gpu/command_buffer/client/gles2_interface.h" | 8 #include "gpu/command_buffer/client/gles2_interface.h" |
| 9 #include "gpu/command_buffer/common/mailbox_holder.h" | 9 #include "gpu/command_buffer/common/mailbox_holder.h" |
| 10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| 11 #include "media/base/yuv_convert.h" | 11 #include "media/base/yuv_convert.h" |
| 12 #include "skia/ext/refptr.h" | 12 #include "skia/ext/refptr.h" |
| 13 #include "third_party/libyuv/include/libyuv.h" | 13 #include "third_party/libyuv/include/libyuv.h" |
| 14 #include "third_party/skia/include/core/SkCanvas.h" | 14 #include "third_party/skia/include/core/SkCanvas.h" |
| 15 #include "third_party/skia/include/core/SkImage.h" | 15 #include "third_party/skia/include/core/SkImage.h" |
| 16 #include "third_party/skia/include/core/SkImageGenerator.h" | 16 #include "third_party/skia/include/core/SkImageGenerator.h" |
| 17 #include "third_party/skia/include/gpu/GrContext.h" | 17 #include "third_party/skia/include/gpu/GrContext.h" |
| 18 #include "third_party/skia/include/gpu/GrPaint.h" | 18 #include "third_party/skia/include/gpu/GrPaint.h" |
| 19 #include "third_party/skia/include/gpu/GrTexture.h" | 19 #include "third_party/skia/include/gpu/GrTexture.h" |
| 20 #include "third_party/skia/include/gpu/GrTextureProvider.h" | 20 #include "third_party/skia/include/gpu/GrTextureProvider.h" |
| 21 #include "third_party/skia/include/gpu/SkGr.h" | |
| 22 #include "third_party/skia/include/gpu/SkGrPixelRef.h" | |
| 23 #include "ui/gfx/skbitmap_operations.h" | |
| 24 | 21 |
| 25 // Skia internal format depends on a platform. On Android it is ABGR, on others | 22 // Skia internal format depends on a platform. On Android it is ABGR, on others |
| 26 // it is ARGB. | 23 // it is ARGB. |
| 27 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \ | 24 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \ |
| 28 SK_A32_SHIFT == 24 | 25 SK_A32_SHIFT == 24 |
| 29 #define LIBYUV_I420_TO_ARGB libyuv::I420ToARGB | 26 #define LIBYUV_I420_TO_ARGB libyuv::I420ToARGB |
| 30 #define LIBYUV_I422_TO_ARGB libyuv::I422ToARGB | 27 #define LIBYUV_I422_TO_ARGB libyuv::I422ToARGB |
| 31 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \ | 28 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \ |
| 32 SK_A32_SHIFT == 24 | 29 SK_A32_SHIFT == 24 |
| 33 #define LIBYUV_I420_TO_ARGB libyuv::I420ToABGR | 30 #define LIBYUV_I420_TO_ARGB libyuv::I420ToABGR |
| 34 #define LIBYUV_I422_TO_ARGB libyuv::I422ToABGR | 31 #define LIBYUV_I422_TO_ARGB libyuv::I422ToABGR |
| 35 #else | 32 #else |
| 36 #error Unexpected Skia ARGB_8888 layout! | 33 #error Unexpected Skia ARGB_8888 layout! |
| 37 #endif | 34 #endif |
| 38 | 35 |
| 39 namespace media { | 36 namespace media { |
| 40 | 37 |
| 41 namespace { | 38 namespace { |
| 42 | 39 |
| 43 // This class keeps two temporary resources; software bitmap, hardware bitmap. | 40 // This class keeps the last image drawn. |
| 44 // If both bitmap are created and then only software bitmap is updated every | 41 // We delete the temporary resource if it is not used for 3 seconds. |
| 45 // frame, hardware bitmap outlives until the media player dies. So we delete | |
| 46 // a temporary resource if it is not used for 3 sec. | |
| 47 const int kTemporaryResourceDeletionDelay = 3; // Seconds; | 42 const int kTemporaryResourceDeletionDelay = 3; // Seconds; |
| 48 | 43 |
| 49 bool CheckColorSpace(const scoped_refptr<VideoFrame>& video_frame, | 44 bool CheckColorSpace(const scoped_refptr<VideoFrame>& video_frame, |
| 50 ColorSpace color_space) { | 45 ColorSpace color_space) { |
| 51 int result; | 46 int result; |
| 52 return video_frame->metadata()->GetInteger( | 47 return video_frame->metadata()->GetInteger( |
| 53 VideoFrameMetadata::COLOR_SPACE, &result) && | 48 VideoFrameMetadata::COLOR_SPACE, &result) && |
| 54 result == color_space; | 49 result == color_space; |
| 55 } | 50 } |
| 56 | 51 |
| 57 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap, | |
| 58 const gfx::Size& size) { | |
| 59 return bitmap->getTexture() && bitmap->width() == size.width() && | |
| 60 bitmap->height() == size.height(); | |
| 61 } | |
| 62 | |
| 63 bool AllocateSkBitmapTexture(GrContext* gr, | |
| 64 SkBitmap* bitmap, | |
| 65 const gfx::Size& size) { | |
| 66 DCHECK(gr); | |
| 67 GrTextureDesc desc; | |
| 68 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid | |
| 69 // RGBA to BGRA conversion. | |
| 70 desc.fConfig = kRGBA_8888_GrPixelConfig; | |
| 71 desc.fFlags = kRenderTarget_GrSurfaceFlag; | |
| 72 desc.fSampleCnt = 0; | |
| 73 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | |
| 74 desc.fWidth = size.width(); | |
| 75 desc.fHeight = size.height(); | |
| 76 skia::RefPtr<GrTexture> texture = skia::AdoptRef( | |
| 77 gr->textureProvider()->refScratchTexture( | |
| 78 desc, GrTextureProvider::kExact_ScratchTexMatch)); | |
| 79 if (!texture.get()) | |
| 80 return false; | |
| 81 | |
| 82 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight); | |
| 83 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get())); | |
| 84 if (!pixel_ref) | |
| 85 return false; | |
| 86 bitmap->setInfo(info); | |
| 87 bitmap->setPixelRef(pixel_ref)->unref(); | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 class SyncPointClientImpl : public VideoFrame::SyncPointClient { | 52 class SyncPointClientImpl : public VideoFrame::SyncPointClient { |
| 92 public: | 53 public: |
| 93 explicit SyncPointClientImpl(gpu::gles2::GLES2Interface* gl) : gl_(gl) {} | 54 explicit SyncPointClientImpl(gpu::gles2::GLES2Interface* gl) : gl_(gl) {} |
| 94 ~SyncPointClientImpl() override {} | 55 ~SyncPointClientImpl() override {} |
| 95 uint32 InsertSyncPoint() override { return gl_->InsertSyncPointCHROMIUM(); } | 56 uint32 InsertSyncPoint() override { return gl_->InsertSyncPointCHROMIUM(); } |
| 96 void WaitSyncPoint(uint32 sync_point) override { | 57 void WaitSyncPoint(uint32 sync_point) override { |
| 97 gl_->WaitSyncPointCHROMIUM(sync_point); | 58 gl_->WaitSyncPointCHROMIUM(sync_point); |
| 98 } | 59 } |
| 99 | 60 |
| 100 private: | 61 private: |
| 101 gpu::gles2::GLES2Interface* gl_; | 62 gpu::gles2::GLES2Interface* gl_; |
| 102 | 63 |
| 103 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncPointClientImpl); | 64 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncPointClientImpl); |
| 104 }; | 65 }; |
| 105 | 66 |
| 106 scoped_ptr<SkImage> CreateSkImageFromVideoFrameYUVTextures( | 67 skia::RefPtr<SkImage> NewSkImageFromVideoFrameYUVTextures( |
| 107 VideoFrame* video_frame, | 68 const scoped_refptr<VideoFrame>& video_frame, |
| 108 const Context3D& context_3d) { | 69 const Context3D& context_3d) { |
| 109 // Support only TEXTURE_YUV_420. | 70 // Support only TEXTURE_YUV_420. |
| 110 DCHECK(video_frame->HasTextures()); | 71 DCHECK(video_frame->HasTextures()); |
| 111 DCHECK_EQ(media::PIXEL_FORMAT_I420, video_frame->format()); | 72 DCHECK_EQ(media::PIXEL_FORMAT_I420, video_frame->format()); |
| 112 DCHECK_EQ(3u, media::VideoFrame::NumPlanes(video_frame->format())); | 73 DCHECK_EQ(3u, media::VideoFrame::NumPlanes(video_frame->format())); |
| 113 | 74 |
| 114 gpu::gles2::GLES2Interface* gl = context_3d.gl; | 75 gpu::gles2::GLES2Interface* gl = context_3d.gl; |
| 115 DCHECK(gl); | 76 DCHECK(gl); |
| 116 gfx::Size ya_tex_size = video_frame->coded_size(); | 77 gfx::Size ya_tex_size = video_frame->coded_size(); |
| 117 gfx::Size uv_tex_size((ya_tex_size.width() + 1) / 2, | 78 gfx::Size uv_tex_size((ya_tex_size.width() + 1) / 2, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 if (CheckColorSpace(video_frame, media::COLOR_SPACE_JPEG)) | 118 if (CheckColorSpace(video_frame, media::COLOR_SPACE_JPEG)) |
| 158 color_space = kJPEG_SkYUVColorSpace; | 119 color_space = kJPEG_SkYUVColorSpace; |
| 159 else if (CheckColorSpace(video_frame, media::COLOR_SPACE_HD_REC709)) | 120 else if (CheckColorSpace(video_frame, media::COLOR_SPACE_HD_REC709)) |
| 160 color_space = kRec709_SkYUVColorSpace; | 121 color_space = kRec709_SkYUVColorSpace; |
| 161 | 122 |
| 162 SkImage* img = SkImage::NewFromYUVTexturesCopy(context_3d.gr_context, | 123 SkImage* img = SkImage::NewFromYUVTexturesCopy(context_3d.gr_context, |
| 163 color_space, handles, yuvSizes, | 124 color_space, handles, yuvSizes, |
| 164 kTopLeft_GrSurfaceOrigin); | 125 kTopLeft_GrSurfaceOrigin); |
| 165 DCHECK(img); | 126 DCHECK(img); |
| 166 gl->DeleteTextures(3, source_textures); | 127 gl->DeleteTextures(3, source_textures); |
| 167 SyncPointClientImpl client(gl); | 128 return skia::AdoptRef(img); |
| 168 video_frame->UpdateReleaseSyncPoint(&client); | |
| 169 return make_scoped_ptr(img); | |
| 170 } | 129 } |
| 171 | 130 |
| 172 bool CopyVideoFrameSingleTextureToSkBitmap(VideoFrame* video_frame, | 131 bool ShouldCacheVideoFrameSkImage( |
| 173 SkBitmap* bitmap, | 132 const scoped_refptr<VideoFrame>& video_frame) { |
| 174 const Context3D& context_3d) { | 133 return !video_frame->HasTextures() || |
| 175 // Check if we could reuse existing texture based bitmap. | 134 media::VideoFrame::NumPlanes(video_frame->format()) != 1 || |
| 176 // Otherwise, release existing texture based bitmap and allocate | 135 video_frame->mailbox_holder(0).texture_target != GL_TEXTURE_2D; |
| 177 // a new one based on video size. | 136 } |
| 178 if (!IsSkBitmapProperlySizedTexture(bitmap, | 137 |
| 179 video_frame->visible_rect().size())) { | 138 // Creates a SkImage from a |video_frame| backed by native resources. |
| 180 if (!AllocateSkBitmapTexture(context_3d.gr_context, bitmap, | 139 // The SkImage will take ownership of the underlying resource. |
| 181 video_frame->visible_rect().size())) { | 140 skia::RefPtr<SkImage> NewSkImageFromVideoFrameNative( |
| 182 return false; | 141 const scoped_refptr<VideoFrame>& video_frame, |
| 183 } | 142 const Context3D& context_3d) { |
| 143 DCHECK_EQ(PIXEL_FORMAT_ARGB, video_frame->format()); | |
| 144 | |
| 145 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0); | |
| 146 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D || | |
| 147 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB || | |
| 148 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES) | |
| 149 << mailbox_holder.texture_target; | |
| 150 | |
| 151 gpu::gles2::GLES2Interface* gl = context_3d.gl; | |
| 152 unsigned source_texture = 0; | |
| 153 if (mailbox_holder.texture_target != GL_TEXTURE_2D) { | |
| 154 // TODO(dcastagna): At the moment Skia doesn't support targets different | |
| 155 // than GL_TEXTURE_2D. Avoid this copy once | |
| 156 // https://code.google.com/p/skia/issues/detail?id=3868 is addressed. | |
| 157 gl->GenTextures(1, &source_texture); | |
| 158 DCHECK(source_texture); | |
| 159 gl->BindTexture(GL_TEXTURE_2D, source_texture); | |
| 160 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture( | |
| 161 gl, video_frame.get(), source_texture, GL_RGBA, GL_UNSIGNED_BYTE, true, | |
| 162 false); | |
| 163 } else { | |
| 164 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point); | |
| 165 source_texture = gl->CreateAndConsumeTextureCHROMIUM( | |
| 166 mailbox_holder.texture_target, mailbox_holder.mailbox.name); | |
| 184 } | 167 } |
| 185 | 168 GrBackendTextureDesc desc; |
| 186 unsigned texture_id = | 169 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 187 static_cast<unsigned>((bitmap->getTexture())->getTextureHandle()); | 170 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 188 // If CopyVideoFrameSingleTextureToGLTexture() changes the state of the | 171 desc.fWidth = video_frame->coded_size().width(); |
| 189 // |texture_id|, it's needed to invalidate the state cached in skia, | 172 desc.fHeight = video_frame->coded_size().height(); |
| 190 // but currently the state isn't changed. | 173 desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 191 | 174 desc.fTextureHandle = source_texture; |
| 192 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture( | 175 return skia::AdoptRef( |
| 193 context_3d.gl, video_frame, texture_id, GL_RGBA, GL_UNSIGNED_BYTE, true, | 176 SkImage::NewFromAdoptedTexture(context_3d.gr_context, desc)); |
| 194 false); | |
| 195 bitmap->notifyPixelsChanged(); | |
| 196 return true; | |
| 197 } | 177 } |
| 198 | 178 |
| 199 } // anonymous namespace | 179 } // anonymous namespace |
| 200 | 180 |
| 201 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU. | 181 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU. |
| 202 class VideoImageGenerator : public SkImageGenerator { | 182 class VideoImageGenerator : public SkImageGenerator { |
| 203 public: | 183 public: |
| 204 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame) | 184 VideoImageGenerator(VideoFrame* frame) |
| 205 : SkImageGenerator( | 185 : SkImageGenerator( |
| 206 SkImageInfo::MakeN32Premul(frame->visible_rect().width(), | 186 SkImageInfo::MakeN32Premul(frame->visible_rect().width(), |
| 207 frame->visible_rect().height())) | 187 frame->visible_rect().height())), |
| 208 , frame_(frame) { | 188 frame_(frame) {} |
| 209 DCHECK(frame_.get()); | |
| 210 } | |
| 211 ~VideoImageGenerator() override {} | 189 ~VideoImageGenerator() override {} |
| 212 | 190 |
| 213 void set_frame(const scoped_refptr<VideoFrame>& frame) { frame_ = frame; } | 191 void set_frame(VideoFrame* frame) { frame_ = frame; } |
| 214 | 192 |
| 215 protected: | 193 protected: |
| 216 bool onGetPixels(const SkImageInfo& info, | 194 bool onGetPixels(const SkImageInfo& info, |
| 217 void* pixels, | 195 void* pixels, |
| 218 size_t row_bytes, | 196 size_t row_bytes, |
| 219 SkPMColor ctable[], | 197 SkPMColor ctable[], |
| 220 int* ctable_count) override { | 198 int* ctable_count) override { |
| 221 if (!frame_.get()) | 199 if (!frame_) |
| 222 return false; | 200 return false; |
| 223 // If skia couldn't do the YUV conversion on GPU, we will on CPU. | 201 // If skia couldn't do the YUV conversion on GPU, we will on CPU. |
| 224 SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels( | 202 SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels( |
| 225 frame_, pixels, row_bytes); | 203 frame_, pixels, row_bytes); |
| 226 return true; | 204 return true; |
| 227 } | 205 } |
| 228 | 206 |
| 229 bool onGetYUV8Planes(SkISize sizes[3], | 207 bool onGetYUV8Planes(SkISize sizes[3], |
| 230 void* planes[3], | 208 void* planes[3], |
| 231 size_t row_bytes[3], | 209 size_t row_bytes[3], |
| 232 SkYUVColorSpace* color_space) override { | 210 SkYUVColorSpace* color_space) override { |
| 233 if (!frame_.get() || !media::IsYuvPlanar(frame_->format()) || | 211 if (!frame_ || !media::IsYuvPlanar(frame_->format()) || |
| 234 // TODO(rileya): Skia currently doesn't support YUVA conversion. Remove | 212 // TODO(rileya): Skia currently doesn't support YUVA conversion. Remove |
| 235 // this case once it does. As-is we will fall back on the pure-software | 213 // this case once it does. As-is we will fall back on the pure-software |
| 236 // path in this case. | 214 // path in this case. |
| 237 frame_->format() == PIXEL_FORMAT_YV12A) { | 215 frame_->format() == PIXEL_FORMAT_YV12A) { |
| 238 return false; | 216 return false; |
| 239 } | 217 } |
| 240 | 218 |
| 241 if (color_space) { | 219 if (color_space) { |
| 242 if (CheckColorSpace(frame_, COLOR_SPACE_JPEG)) | 220 if (CheckColorSpace(frame_, COLOR_SPACE_JPEG)) |
| 243 *color_space = kJPEG_SkYUVColorSpace; | 221 *color_space = kJPEG_SkYUVColorSpace; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 in_line += in_line_stride; | 267 in_line += in_line_stride; |
| 290 out_line += out_line_stride; | 268 out_line += out_line_stride; |
| 291 } | 269 } |
| 292 } | 270 } |
| 293 } | 271 } |
| 294 } | 272 } |
| 295 return true; | 273 return true; |
| 296 } | 274 } |
| 297 | 275 |
| 298 private: | 276 private: |
| 299 scoped_refptr<VideoFrame> frame_; | 277 VideoFrame* frame_; |
| 300 | 278 |
| 301 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator); | 279 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator); |
| 302 }; | 280 }; |
| 303 | 281 |
| 304 SkCanvasVideoRenderer::SkCanvasVideoRenderer() | 282 SkCanvasVideoRenderer::SkCanvasVideoRenderer() |
| 305 : last_frame_timestamp_(media::kNoTimestamp()), | 283 : last_image_deleting_timer_( |
| 306 frame_deleting_timer_( | |
| 307 FROM_HERE, | 284 FROM_HERE, |
| 308 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), | 285 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), |
| 309 this, | 286 this, |
| 310 &SkCanvasVideoRenderer::ResetLastFrame), | 287 &SkCanvasVideoRenderer::ResetCache) {} |
| 311 accelerated_generator_(nullptr), | 288 |
| 312 accelerated_last_frame_timestamp_(media::kNoTimestamp()), | 289 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() { |
| 313 accelerated_frame_deleting_timer_( | 290 ResetCache(); |
| 314 FROM_HERE, | |
| 315 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), | |
| 316 this, | |
| 317 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame) { | |
| 318 last_frame_.setIsVolatile(true); | |
| 319 } | 291 } |
| 320 | 292 |
| 321 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {} | |
| 322 | |
| 323 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame, | 293 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame, |
| 324 SkCanvas* canvas, | 294 SkCanvas* canvas, |
| 325 const gfx::RectF& dest_rect, | 295 const gfx::RectF& dest_rect, |
| 326 uint8 alpha, | 296 uint8 alpha, |
| 327 SkXfermode::Mode mode, | 297 SkXfermode::Mode mode, |
| 328 VideoRotation video_rotation, | 298 VideoRotation video_rotation, |
| 329 const Context3D& context_3d) { | 299 const Context3D& context_3d) { |
| 330 if (alpha == 0) { | 300 if (alpha == 0) { |
| 331 return; | 301 return; |
| 332 } | 302 } |
| 333 | 303 |
| 334 SkRect dest; | 304 SkRect dest; |
| 335 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom()); | 305 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom()); |
| 336 | 306 |
| 337 SkPaint paint; | 307 SkPaint paint; |
| 338 paint.setAlpha(alpha); | 308 paint.setAlpha(alpha); |
| 339 | 309 |
| 340 // Paint black rectangle if there isn't a frame available or the | 310 // Paint black rectangle if there isn't a frame available or the |
| 341 // frame has an unexpected format. | 311 // frame has an unexpected format. |
| 342 if (!video_frame.get() || video_frame->natural_size().IsEmpty() || | 312 if (!video_frame.get() || video_frame->natural_size().IsEmpty() || |
| 343 !(media::IsYuvPlanar(video_frame->format()) || | 313 !(media::IsYuvPlanar(video_frame->format()) || |
| 344 video_frame->HasTextures())) { | 314 video_frame->HasTextures())) { |
| 345 canvas->drawRect(dest, paint); | 315 canvas->drawRect(dest, paint); |
| 346 canvas->flush(); | 316 canvas->flush(); |
| 347 return; | 317 return; |
| 348 } | 318 } |
| 349 | 319 |
| 350 SkBitmap* target_frame = nullptr; | 320 gpu::gles2::GLES2Interface* gl = context_3d.gl; |
| 351 | 321 |
| 352 if (video_frame->HasTextures()) { | 322 // This might be wrong, two different videos could be drawn to the same |
| 353 // Draw HW Video on both SW and HW Canvas. | 323 // canvas and the two different videoframes could have the same timestamp. |
| 354 // In SW Canvas case, rely on skia drawing Ganesh SkBitmap on SW SkCanvas. | 324 if (last_image_ && video_frame->timestamp() == last_timestamp_) { |
|
DaleCurtis
2015/08/07 17:10:39
Seems pretty likely to be wrong, but the old code
Daniele Castagna
2015/08/12 00:41:39
I misunderstood this. Initially I thought there wa
| |
| 355 if (accelerated_last_frame_.isNull() || | 325 // |last_image_| can be reused. |
| 356 video_frame->timestamp() != accelerated_last_frame_timestamp_) { | 326 // |video_generator_| will be set only if the last cache videoframe was |
| 357 DCHECK(context_3d.gl); | 327 // a software videoframe. |
| 328 if (video_generator_) | |
| 329 video_generator_->set_frame(video_frame.get()); | |
| 330 } else { | |
| 331 ResetCache(); | |
| 332 // Generate a new image. | |
| 333 if (video_frame->HasTextures()) { | |
| 358 DCHECK(context_3d.gr_context); | 334 DCHECK(context_3d.gr_context); |
| 359 if (accelerated_generator_) { | 335 DCHECK(gl); |
| 360 // Reset SkBitmap used in SWVideo-to-HWCanvas path. | 336 if (media::VideoFrame::NumPlanes(video_frame->format()) == 1) { |
| 361 accelerated_last_frame_.reset(); | 337 last_image_ = NewSkImageFromVideoFrameNative(video_frame, context_3d); |
| 362 accelerated_generator_ = nullptr; | 338 } else { |
| 339 last_image_ = | |
| 340 NewSkImageFromVideoFrameYUVTextures(video_frame, context_3d); | |
| 363 } | 341 } |
| 364 | 342 } else { |
| 365 if (media::VideoFrame::NumPlanes(video_frame->format()) == 1) { | 343 video_generator_ = new VideoImageGenerator(video_frame.get()); |
| 366 accelerated_last_image_.reset(); | 344 last_image_ = skia::AdoptRef(SkImage::NewFromGenerator(video_generator_)); |
| 367 if (!CopyVideoFrameSingleTextureToSkBitmap( | |
| 368 video_frame.get(), &accelerated_last_frame_, context_3d)) { | |
| 369 NOTREACHED(); | |
| 370 return; | |
| 371 } | |
| 372 DCHECK(video_frame->visible_rect().width() == | |
| 373 accelerated_last_frame_.width() && | |
| 374 video_frame->visible_rect().height() == | |
| 375 accelerated_last_frame_.height()); | |
| 376 } else { | |
| 377 accelerated_last_image_ = CreateSkImageFromVideoFrameYUVTextures( | |
| 378 video_frame.get(), context_3d); | |
| 379 DCHECK(accelerated_last_image_); | |
| 380 } | |
| 381 accelerated_last_frame_timestamp_ = video_frame->timestamp(); | |
| 382 } | 345 } |
| 383 target_frame = &accelerated_last_frame_; | 346 last_timestamp_ = video_frame->timestamp(); |
| 384 accelerated_frame_deleting_timer_.Reset(); | |
| 385 } else if (canvas->getGrContext()) { | |
| 386 if (accelerated_last_frame_.isNull() || | |
| 387 video_frame->timestamp() != accelerated_last_frame_timestamp_) { | |
| 388 // Draw SW Video on HW Canvas. | |
| 389 if (!accelerated_generator_ && !accelerated_last_frame_.isNull()) { | |
| 390 // Reset SkBitmap used in HWVideo-to-HWCanvas path. | |
| 391 accelerated_last_frame_.reset(); | |
| 392 } | |
| 393 accelerated_generator_ = new VideoImageGenerator(video_frame); | |
| 394 | |
| 395 // Note: This takes ownership of |accelerated_generator_|. | |
| 396 if (!SkInstallDiscardablePixelRef(accelerated_generator_, | |
| 397 &accelerated_last_frame_)) { | |
| 398 NOTREACHED(); | |
| 399 return; | |
| 400 } | |
| 401 DCHECK(video_frame->visible_rect().width() == | |
| 402 accelerated_last_frame_.width() && | |
| 403 video_frame->visible_rect().height() == | |
| 404 accelerated_last_frame_.height()); | |
| 405 | |
| 406 accelerated_last_frame_timestamp_ = video_frame->timestamp(); | |
| 407 } else if (accelerated_generator_) { | |
| 408 accelerated_generator_->set_frame(video_frame); | |
| 409 } | |
| 410 target_frame = &accelerated_last_frame_; | |
| 411 accelerated_frame_deleting_timer_.Reset(); | |
| 412 } else { | |
| 413 // Draw SW Video on SW Canvas. | |
| 414 DCHECK(video_frame->IsMappable()); | |
| 415 if (last_frame_.isNull() || | |
| 416 video_frame->timestamp() != last_frame_timestamp_) { | |
| 417 // Check if |bitmap| needs to be (re)allocated. | |
| 418 if (last_frame_.isNull() || | |
| 419 last_frame_.width() != video_frame->visible_rect().width() || | |
| 420 last_frame_.height() != video_frame->visible_rect().height()) { | |
| 421 last_frame_.allocN32Pixels(video_frame->visible_rect().width(), | |
| 422 video_frame->visible_rect().height()); | |
| 423 last_frame_.setIsVolatile(true); | |
| 424 } | |
| 425 last_frame_.lockPixels(); | |
| 426 ConvertVideoFrameToRGBPixels( | |
| 427 video_frame, last_frame_.getPixels(), last_frame_.rowBytes()); | |
| 428 last_frame_.notifyPixelsChanged(); | |
| 429 last_frame_.unlockPixels(); | |
| 430 last_frame_timestamp_ = video_frame->timestamp(); | |
| 431 } | |
| 432 target_frame = &last_frame_; | |
| 433 frame_deleting_timer_.Reset(); | |
| 434 } | 347 } |
| 348 last_image_deleting_timer_.Reset(); | |
| 435 | 349 |
| 436 paint.setXfermodeMode(mode); | 350 paint.setXfermodeMode(mode); |
| 437 paint.setFilterQuality(kLow_SkFilterQuality); | 351 paint.setFilterQuality(kLow_SkFilterQuality); |
| 438 | 352 |
| 439 const bool need_transform = | 353 const bool need_transform = |
| 440 video_rotation != VIDEO_ROTATION_0 || | 354 video_rotation != VIDEO_ROTATION_0 || |
| 441 dest_rect.size() != video_frame->visible_rect().size() || | 355 dest_rect.size() != video_frame->visible_rect().size() || |
| 442 !dest_rect.origin().IsOrigin(); | 356 !dest_rect.origin().IsOrigin(); |
| 443 if (need_transform) { | 357 if (need_transform) { |
| 444 canvas->save(); | 358 canvas->save(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 461 } | 375 } |
| 462 canvas->rotate(angle); | 376 canvas->rotate(angle); |
| 463 | 377 |
| 464 gfx::SizeF rotated_dest_size = dest_rect.size(); | 378 gfx::SizeF rotated_dest_size = dest_rect.size(); |
| 465 if (video_rotation == VIDEO_ROTATION_90 || | 379 if (video_rotation == VIDEO_ROTATION_90 || |
| 466 video_rotation == VIDEO_ROTATION_270) { | 380 video_rotation == VIDEO_ROTATION_270) { |
| 467 rotated_dest_size = | 381 rotated_dest_size = |
| 468 gfx::SizeF(rotated_dest_size.height(), rotated_dest_size.width()); | 382 gfx::SizeF(rotated_dest_size.height(), rotated_dest_size.width()); |
| 469 } | 383 } |
| 470 canvas->scale( | 384 canvas->scale( |
| 471 SkFloatToScalar(rotated_dest_size.width() / target_frame->width()), | 385 SkFloatToScalar(rotated_dest_size.width() / last_image_->width()), |
| 472 SkFloatToScalar(rotated_dest_size.height() / target_frame->height())); | 386 SkFloatToScalar(rotated_dest_size.height() / last_image_->height())); |
| 473 canvas->translate(-SkFloatToScalar(target_frame->width() * 0.5f), | 387 canvas->translate(-SkFloatToScalar(last_image_->width() * 0.5f), |
| 474 -SkFloatToScalar(target_frame->height() * 0.5f)); | 388 -SkFloatToScalar(last_image_->height() * 0.5f)); |
| 475 } | 389 } |
| 476 if (accelerated_last_image_) { | 390 canvas->drawImage(last_image_.get(), 0, 0, &paint); |
| 477 canvas->drawImage(accelerated_last_image_.get(), 0, 0, &paint); | 391 |
| 478 } else { | |
| 479 canvas->drawBitmap(*target_frame, 0, 0, &paint); | |
| 480 } | |
| 481 if (need_transform) | 392 if (need_transform) |
| 482 canvas->restore(); | 393 canvas->restore(); |
| 394 // Make sure to flush so we can remove the videoframe from the generator. | |
| 483 canvas->flush(); | 395 canvas->flush(); |
| 484 // SkCanvas::flush() causes the generator to generate SkImage, so delete | 396 |
| 485 // |video_frame| not to be outlived. | 397 // TODO(dcastagna): here we're assuming |video_generator_| will still be valid |
| 486 if (canvas->getGrContext() && accelerated_generator_) | 398 // after SkImage::NewFromGenerator took the ownership. |
| 487 accelerated_generator_->set_frame(nullptr); | 399 // Fix this once https://code.google.com/p/skia/issues/detail?id=3870 is |
| 400 // addressed. | |
| 401 if (video_generator_) | |
| 402 video_generator_->set_frame(nullptr); | |
| 403 | |
| 404 if (!ShouldCacheVideoFrameSkImage(video_frame)) | |
| 405 ResetCache(); | |
| 406 | |
| 407 if (video_frame->HasTextures()) { | |
| 408 DCHECK(gl); | |
| 409 SyncPointClientImpl client(gl); | |
| 410 video_frame->UpdateReleaseSyncPoint(&client); | |
| 411 } | |
| 488 } | 412 } |
| 489 | 413 |
| 490 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame, | 414 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame, |
| 491 SkCanvas* canvas, | 415 SkCanvas* canvas, |
| 492 const Context3D& context_3d) { | 416 const Context3D& context_3d) { |
| 493 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff, | 417 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff, |
| 494 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0, context_3d); | 418 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0, context_3d); |
| 495 } | 419 } |
| 496 | 420 |
| 497 // static | 421 // static |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 637 unsigned int type, | 561 unsigned int type, |
| 638 bool premultiply_alpha, | 562 bool premultiply_alpha, |
| 639 bool flip_y) { | 563 bool flip_y) { |
| 640 DCHECK(video_frame); | 564 DCHECK(video_frame); |
| 641 DCHECK(video_frame->HasTextures()); | 565 DCHECK(video_frame->HasTextures()); |
| 642 DCHECK_EQ(1u, VideoFrame::NumPlanes(video_frame->format())); | 566 DCHECK_EQ(1u, VideoFrame::NumPlanes(video_frame->format())); |
| 643 | 567 |
| 644 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0); | 568 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0); |
| 645 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D || | 569 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D || |
| 646 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB || | 570 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB || |
| 647 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES); | 571 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES) |
| 572 << mailbox_holder.texture_target; | |
| 648 | 573 |
| 649 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point); | 574 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point); |
| 650 uint32 source_texture = gl->CreateAndConsumeTextureCHROMIUM( | 575 uint32 source_texture = gl->CreateAndConsumeTextureCHROMIUM( |
| 651 mailbox_holder.texture_target, mailbox_holder.mailbox.name); | 576 mailbox_holder.texture_target, mailbox_holder.mailbox.name); |
| 652 | 577 |
| 653 // The video is stored in a unmultiplied format, so premultiply | 578 // The video is stored in a unmultiplied format, so premultiply |
| 654 // if necessary. | 579 // if necessary. |
| 655 // Application itself needs to take care of setting the right |flip_y| | 580 // Application itself needs to take care of setting the right |flip_y| |
| 656 // value down to get the expected result. | 581 // value down to get the expected result. |
| 657 // "flip_y == true" means to reverse the video orientation while | 582 // "flip_y == true" means to reverse the video orientation while |
| 658 // "flip_y == false" means to keep the intrinsic orientation. | 583 // "flip_y == false" means to keep the intrinsic orientation. |
| 659 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture, | 584 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture, |
| 660 internal_format, type, | 585 internal_format, type, |
| 661 flip_y, premultiply_alpha, false); | 586 flip_y, premultiply_alpha, false); |
| 662 | 587 |
| 663 gl->DeleteTextures(1, &source_texture); | 588 gl->DeleteTextures(1, &source_texture); |
| 664 gl->Flush(); | 589 gl->Flush(); |
| 665 | 590 |
| 666 SyncPointClientImpl client(gl); | 591 SyncPointClientImpl client(gl); |
| 667 video_frame->UpdateReleaseSyncPoint(&client); | 592 video_frame->UpdateReleaseSyncPoint(&client); |
| 668 } | 593 } |
| 669 | 594 |
| 670 void SkCanvasVideoRenderer::ResetLastFrame() { | 595 void SkCanvasVideoRenderer::ResetCache() { |
| 671 last_frame_.reset(); | 596 // Clear cached values. |
| 672 last_frame_timestamp_ = media::kNoTimestamp(); | 597 video_generator_ = nullptr; |
| 673 } | 598 last_image_ = nullptr; |
| 674 | 599 last_timestamp_ = kNoTimestamp(); |
| 675 void SkCanvasVideoRenderer::ResetAcceleratedLastFrame() { | |
| 676 accelerated_last_image_.reset(); | |
| 677 accelerated_last_frame_.reset(); | |
| 678 accelerated_generator_ = nullptr; | |
| 679 accelerated_last_frame_timestamp_ = media::kNoTimestamp(); | |
| 680 } | 600 } |
| 681 | 601 |
| 682 } // namespace media | 602 } // namespace media |
| OLD | NEW |