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

Side by Side Diff: media/blink/skcanvas_video_renderer.cc

Issue 1144323003: media: Refactor SkCanvasVideoRenderer to use SkImages and not SkBitmaps. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Invalidate cache when switching hw/sw canvas. Created 5 years, 4 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
« no previous file with comments | « media/blink/skcanvas_video_renderer.h ('k') | media/blink/skcanvas_video_renderer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Creates a SkImage from a |video_frame| backed by native resources.
173 SkBitmap* bitmap, 132 // The SkImage will take ownership of the underlying resource.
174 const Context3D& context_3d) { 133 skia::RefPtr<SkImage> NewSkImageFromVideoFrameNative(
175 // Check if we could reuse existing texture based bitmap. 134 const scoped_refptr<VideoFrame>& video_frame,
176 // Otherwise, release existing texture based bitmap and allocate 135 const Context3D& context_3d) {
177 // a new one based on video size. 136 DCHECK_EQ(PIXEL_FORMAT_ARGB, video_frame->format());
178 if (!IsSkBitmapProperlySizedTexture(bitmap, 137
179 video_frame->visible_rect().size())) { 138 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
180 if (!AllocateSkBitmapTexture(context_3d.gr_context, bitmap, 139 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D ||
181 video_frame->visible_rect().size())) { 140 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB ||
182 return false; 141 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES)
183 } 142 << mailbox_holder.texture_target;
143
144 gpu::gles2::GLES2Interface* gl = context_3d.gl;
145 unsigned source_texture = 0;
146 if (mailbox_holder.texture_target != GL_TEXTURE_2D) {
147 // TODO(dcastagna): At the moment Skia doesn't support targets different
148 // than GL_TEXTURE_2D. Avoid this copy once
149 // https://code.google.com/p/skia/issues/detail?id=3868 is addressed.
150 gl->GenTextures(1, &source_texture);
151 DCHECK(source_texture);
152 gl->BindTexture(GL_TEXTURE_2D, source_texture);
153 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
154 gl, video_frame.get(), source_texture, GL_RGBA, GL_UNSIGNED_BYTE, true,
155 false);
156 } else {
157 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point);
158 source_texture = gl->CreateAndConsumeTextureCHROMIUM(
159 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
dshwang 2015/08/03 17:51:53 As mentioned above, we should cache in order to re
Daniele Castagna 2015/08/03 20:52:21 OK, now the cache gets invalidated after we draw t
184 } 160 }
185 161 GrBackendTextureDesc desc;
186 unsigned texture_id = 162 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
187 static_cast<unsigned>((bitmap->getTexture())->getTextureHandle()); 163 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
188 // If CopyVideoFrameSingleTextureToGLTexture() changes the state of the 164 desc.fWidth = video_frame->coded_size().width();
189 // |texture_id|, it's needed to invalidate the state cached in skia, 165 desc.fHeight = video_frame->coded_size().height();
190 // but currently the state isn't changed. 166 desc.fConfig = kRGBA_8888_GrPixelConfig;
191 167 desc.fTextureHandle = source_texture;
192 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture( 168 return skia::AdoptRef(
193 context_3d.gl, video_frame, texture_id, GL_RGBA, GL_UNSIGNED_BYTE, true, 169 SkImage::NewFromAdoptedTexture(context_3d.gr_context, desc));
194 false);
195 bitmap->notifyPixelsChanged();
196 return true;
197 } 170 }
198 171
199 } // anonymous namespace 172 } // anonymous namespace
200 173
201 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU. 174 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU.
202 class VideoImageGenerator : public SkImageGenerator { 175 class VideoImageGenerator : public SkImageGenerator {
203 public: 176 public:
204 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame) 177 VideoImageGenerator(VideoFrame* frame)
205 : SkImageGenerator( 178 : SkImageGenerator(
206 SkImageInfo::MakeN32Premul(frame->visible_rect().width(), 179 SkImageInfo::MakeN32Premul(frame->visible_rect().width(),
207 frame->visible_rect().height())) 180 frame->visible_rect().height())),
208 , frame_(frame) { 181 frame_(frame) {}
209 DCHECK(frame_.get());
210 }
211 ~VideoImageGenerator() override {} 182 ~VideoImageGenerator() override {}
212 183
213 void set_frame(const scoped_refptr<VideoFrame>& frame) { frame_ = frame; } 184 void set_frame(VideoFrame* frame) { frame_ = frame; }
214 185
215 protected: 186 protected:
216 bool onGetPixels(const SkImageInfo& info, 187 bool onGetPixels(const SkImageInfo& info,
217 void* pixels, 188 void* pixels,
218 size_t row_bytes, 189 size_t row_bytes,
219 SkPMColor ctable[], 190 SkPMColor ctable[],
220 int* ctable_count) override { 191 int* ctable_count) override {
221 if (!frame_.get()) 192 if (!frame_)
222 return false; 193 return false;
223 // If skia couldn't do the YUV conversion on GPU, we will on CPU. 194 // If skia couldn't do the YUV conversion on GPU, we will on CPU.
224 SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels( 195 SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels(
225 frame_, pixels, row_bytes); 196 frame_, pixels, row_bytes);
226 return true; 197 return true;
227 } 198 }
228 199
229 bool onGetYUV8Planes(SkISize sizes[3], 200 bool onGetYUV8Planes(SkISize sizes[3],
230 void* planes[3], 201 void* planes[3],
231 size_t row_bytes[3], 202 size_t row_bytes[3],
232 SkYUVColorSpace* color_space) override { 203 SkYUVColorSpace* color_space) override {
233 if (!frame_.get() || !media::IsYuvPlanar(frame_->format()) || 204 if (!frame_ || !media::IsYuvPlanar(frame_->format()) ||
234 // TODO(rileya): Skia currently doesn't support YUVA conversion. Remove 205 // 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 206 // this case once it does. As-is we will fall back on the pure-software
236 // path in this case. 207 // path in this case.
237 frame_->format() == PIXEL_FORMAT_YV12A) { 208 frame_->format() == PIXEL_FORMAT_YV12A) {
238 return false; 209 return false;
239 } 210 }
240 211
241 if (color_space) { 212 if (color_space) {
242 if (CheckColorSpace(frame_, COLOR_SPACE_JPEG)) 213 if (CheckColorSpace(frame_, COLOR_SPACE_JPEG))
243 *color_space = kJPEG_SkYUVColorSpace; 214 *color_space = kJPEG_SkYUVColorSpace;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 in_line += in_line_stride; 260 in_line += in_line_stride;
290 out_line += out_line_stride; 261 out_line += out_line_stride;
291 } 262 }
292 } 263 }
293 } 264 }
294 } 265 }
295 return true; 266 return true;
296 } 267 }
297 268
298 private: 269 private:
299 scoped_refptr<VideoFrame> frame_; 270 VideoFrame* frame_;
300 271
301 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator); 272 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoImageGenerator);
302 }; 273 };
303 274
304 SkCanvasVideoRenderer::SkCanvasVideoRenderer() 275 SkCanvasVideoRenderer::SkCanvasVideoRenderer()
305 : last_frame_timestamp_(media::kNoTimestamp()), 276 : last_image_drawn_on_hw_(false),
306 frame_deleting_timer_( 277 last_image_deleting_timer_(
307 FROM_HERE, 278 FROM_HERE,
308 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay), 279 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
309 this, 280 this,
310 &SkCanvasVideoRenderer::ResetLastFrame), 281 &SkCanvasVideoRenderer::ResetCache) {}
311 accelerated_generator_(nullptr), 282
312 accelerated_last_frame_timestamp_(media::kNoTimestamp()), 283 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {
313 accelerated_frame_deleting_timer_( 284 ResetCache();
314 FROM_HERE,
315 base::TimeDelta::FromSeconds(kTemporaryResourceDeletionDelay),
316 this,
317 &SkCanvasVideoRenderer::ResetAcceleratedLastFrame) {
318 last_frame_.setIsVolatile(true);
319 } 285 }
320 286
321 SkCanvasVideoRenderer::~SkCanvasVideoRenderer() {}
322
323 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame, 287 void SkCanvasVideoRenderer::Paint(const scoped_refptr<VideoFrame>& video_frame,
324 SkCanvas* canvas, 288 SkCanvas* canvas,
325 const gfx::RectF& dest_rect, 289 const gfx::RectF& dest_rect,
326 uint8 alpha, 290 uint8 alpha,
327 SkXfermode::Mode mode, 291 SkXfermode::Mode mode,
328 VideoRotation video_rotation, 292 VideoRotation video_rotation,
329 const Context3D& context_3d) { 293 const Context3D& context_3d) {
330 if (alpha == 0) { 294 if (alpha == 0) {
331 return; 295 return;
332 } 296 }
333 297
334 SkRect dest; 298 SkRect dest;
335 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom()); 299 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom());
336 300
337 SkPaint paint; 301 SkPaint paint;
338 paint.setAlpha(alpha); 302 paint.setAlpha(alpha);
339 303
340 // Paint black rectangle if there isn't a frame available or the 304 // Paint black rectangle if there isn't a frame available or the
341 // frame has an unexpected format. 305 // frame has an unexpected format.
342 if (!video_frame.get() || video_frame->natural_size().IsEmpty() || 306 if (!video_frame.get() || video_frame->natural_size().IsEmpty() ||
343 !(media::IsYuvPlanar(video_frame->format()) || 307 !(media::IsYuvPlanar(video_frame->format()) ||
344 video_frame->HasTextures())) { 308 video_frame->HasTextures())) {
345 canvas->drawRect(dest, paint); 309 canvas->drawRect(dest, paint);
346 canvas->flush(); 310 canvas->flush();
347 return; 311 return;
348 } 312 }
349 313
350 SkBitmap* target_frame = nullptr; 314 gpu::gles2::GLES2Interface* gl = context_3d.gl;
315 bool hardware_canvas = canvas->getGrContext() != nullptr;
351 316
352 if (video_frame->HasTextures()) { 317 // This might be wrong, two different videos could be drawn to the same
353 // Draw HW Video on both SW and HW Canvas. 318 // 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. 319 if (last_image_ && video_frame->timestamp() == last_timestamp_ &&
355 if (accelerated_last_frame_.isNull() || 320 last_image_drawn_on_hw_ == hardware_canvas) {
356 video_frame->timestamp() != accelerated_last_frame_timestamp_) { 321 // |last_image_| can be reused.
357 DCHECK(context_3d.gl); 322 // |video_generator_| will be set only if the last cache videoframe was
323 // a software videoframe.
324 if (video_generator_)
325 video_generator_->set_frame(video_frame.get());
326 } else {
327 ResetCache();
328 // Generate a new image.
329 if (video_frame->HasTextures()) {
358 DCHECK(context_3d.gr_context); 330 DCHECK(context_3d.gr_context);
359 if (accelerated_generator_) { 331 DCHECK(gl);
360 // Reset SkBitmap used in SWVideo-to-HWCanvas path. 332 if (media::VideoFrame::NumPlanes(video_frame->format()) == 1) {
361 accelerated_last_frame_.reset(); 333 last_image_ = NewSkImageFromVideoFrameNative(video_frame, context_3d);
362 accelerated_generator_ = nullptr; 334 } else {
335 last_image_ =
336 NewSkImageFromVideoFrameYUVTextures(video_frame, context_3d);
363 } 337 }
364 338 } else {
365 if (media::VideoFrame::NumPlanes(video_frame->format()) == 1) { 339 video_generator_ = new VideoImageGenerator(video_frame.get());
366 accelerated_last_image_.reset(); 340 last_image_ = skia::AdoptRef(SkImage::NewFromGenerator(video_generator_));
dshwang 2015/08/03 17:51:52 It caused perf regression. when it used SkBitmap,
bsalomon 2015/08/03 18:12:44 SkImage will recycle GrTextures internally. Skia i
dshwang 2015/08/04 09:51:36 In this case, skia cannot recycle the texture. New
bsalomon 2015/08/04 13:57:56 I see, but wasn't it doing that both before and af
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 } 341 }
383 target_frame = &accelerated_last_frame_; 342 last_timestamp_ = video_frame->timestamp();
384 accelerated_frame_deleting_timer_.Reset(); 343 last_image_drawn_on_hw_ = hardware_canvas;
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()) {
dshwang 2015/08/03 17:51:52 in sw case, reused |last_frame_| if size is same.
Daniele Castagna 2015/08/03 20:52:21 We can't do that with SkImage API. Skia might reus
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 } 344 }
345 last_image_deleting_timer_.Reset();
435 346
436 paint.setXfermodeMode(mode); 347 paint.setXfermodeMode(mode);
437 paint.setFilterQuality(kLow_SkFilterQuality); 348 paint.setFilterQuality(kLow_SkFilterQuality);
438 349
439 const bool need_transform = 350 const bool need_transform =
440 video_rotation != VIDEO_ROTATION_0 || 351 video_rotation != VIDEO_ROTATION_0 ||
441 dest_rect.size() != video_frame->visible_rect().size() || 352 dest_rect.size() != video_frame->visible_rect().size() ||
442 !dest_rect.origin().IsOrigin(); 353 !dest_rect.origin().IsOrigin();
443 if (need_transform) { 354 if (need_transform) {
444 canvas->save(); 355 canvas->save();
(...skipping 16 matching lines...) Expand all
461 } 372 }
462 canvas->rotate(angle); 373 canvas->rotate(angle);
463 374
464 gfx::SizeF rotated_dest_size = dest_rect.size(); 375 gfx::SizeF rotated_dest_size = dest_rect.size();
465 if (video_rotation == VIDEO_ROTATION_90 || 376 if (video_rotation == VIDEO_ROTATION_90 ||
466 video_rotation == VIDEO_ROTATION_270) { 377 video_rotation == VIDEO_ROTATION_270) {
467 rotated_dest_size = 378 rotated_dest_size =
468 gfx::SizeF(rotated_dest_size.height(), rotated_dest_size.width()); 379 gfx::SizeF(rotated_dest_size.height(), rotated_dest_size.width());
469 } 380 }
470 canvas->scale( 381 canvas->scale(
471 SkFloatToScalar(rotated_dest_size.width() / target_frame->width()), 382 SkFloatToScalar(rotated_dest_size.width() / last_image_->width()),
472 SkFloatToScalar(rotated_dest_size.height() / target_frame->height())); 383 SkFloatToScalar(rotated_dest_size.height() / last_image_->height()));
473 canvas->translate(-SkFloatToScalar(target_frame->width() * 0.5f), 384 canvas->translate(-SkFloatToScalar(last_image_->width() * 0.5f),
474 -SkFloatToScalar(target_frame->height() * 0.5f)); 385 -SkFloatToScalar(last_image_->height() * 0.5f));
475 } 386 }
476 if (accelerated_last_image_) { 387 canvas->drawImage(last_image_.get(), 0, 0, &paint);
477 canvas->drawImage(accelerated_last_image_.get(), 0, 0, &paint); 388
478 } else {
479 canvas->drawBitmap(*target_frame, 0, 0, &paint);
480 }
481 if (need_transform) 389 if (need_transform)
482 canvas->restore(); 390 canvas->restore();
391 // Make sure to flush so we can remove the videoframe from the generator.
483 canvas->flush(); 392 canvas->flush();
484 // SkCanvas::flush() causes the generator to generate SkImage, so delete 393 if (video_frame->HasTextures()) {
485 // |video_frame| not to be outlived. 394 DCHECK(gl);
486 if (canvas->getGrContext() && accelerated_generator_) 395 SyncPointClientImpl client(gl);
487 accelerated_generator_->set_frame(nullptr); 396 video_frame->UpdateReleaseSyncPoint(&client);
397 }
398
399 // TODO(dcastagna): here we're assuming |video_generator_| will still be valid
400 // after SkImage::NewFromGenerator took the ownership.
401 // Fix this once https://code.google.com/p/skia/issues/detail?id=3870 is
402 // addressed.
403 if (video_generator_)
404 video_generator_->set_frame(nullptr);
488 } 405 }
489 406
490 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame, 407 void SkCanvasVideoRenderer::Copy(const scoped_refptr<VideoFrame>& video_frame,
491 SkCanvas* canvas, 408 SkCanvas* canvas,
492 const Context3D& context_3d) { 409 const Context3D& context_3d) {
493 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff, 410 Paint(video_frame, canvas, video_frame->visible_rect(), 0xff,
494 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0, context_3d); 411 SkXfermode::kSrc_Mode, media::VIDEO_ROTATION_0, context_3d);
495 } 412 }
496 413
497 // static 414 // static
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 unsigned int type, 554 unsigned int type,
638 bool premultiply_alpha, 555 bool premultiply_alpha,
639 bool flip_y) { 556 bool flip_y) {
640 DCHECK(video_frame); 557 DCHECK(video_frame);
641 DCHECK(video_frame->HasTextures()); 558 DCHECK(video_frame->HasTextures());
642 DCHECK_EQ(1u, VideoFrame::NumPlanes(video_frame->format())); 559 DCHECK_EQ(1u, VideoFrame::NumPlanes(video_frame->format()));
643 560
644 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0); 561 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
645 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D || 562 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D ||
646 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB || 563 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB ||
647 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES); 564 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES)
565 << mailbox_holder.texture_target;
648 566
649 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point); 567 gl->WaitSyncPointCHROMIUM(mailbox_holder.sync_point);
650 uint32 source_texture = gl->CreateAndConsumeTextureCHROMIUM( 568 uint32 source_texture = gl->CreateAndConsumeTextureCHROMIUM(
651 mailbox_holder.texture_target, mailbox_holder.mailbox.name); 569 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
652 570
653 // The video is stored in a unmultiplied format, so premultiply 571 // The video is stored in a unmultiplied format, so premultiply
654 // if necessary. 572 // if necessary.
655 // Application itself needs to take care of setting the right |flip_y| 573 // Application itself needs to take care of setting the right |flip_y|
656 // value down to get the expected result. 574 // value down to get the expected result.
657 // "flip_y == true" means to reverse the video orientation while 575 // "flip_y == true" means to reverse the video orientation while
658 // "flip_y == false" means to keep the intrinsic orientation. 576 // "flip_y == false" means to keep the intrinsic orientation.
659 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture, 577 gl->CopyTextureCHROMIUM(GL_TEXTURE_2D, source_texture, texture,
660 internal_format, type, 578 internal_format, type,
661 flip_y, premultiply_alpha, false); 579 flip_y, premultiply_alpha, false);
662 580
663 gl->DeleteTextures(1, &source_texture); 581 gl->DeleteTextures(1, &source_texture);
664 gl->Flush(); 582 gl->Flush();
665 583
666 SyncPointClientImpl client(gl); 584 SyncPointClientImpl client(gl);
667 video_frame->UpdateReleaseSyncPoint(&client); 585 video_frame->UpdateReleaseSyncPoint(&client);
668 } 586 }
669 587
670 void SkCanvasVideoRenderer::ResetLastFrame() { 588 void SkCanvasVideoRenderer::ResetCache() {
671 last_frame_.reset(); 589 // Clear cached values.
672 last_frame_timestamp_ = media::kNoTimestamp(); 590 video_generator_ = nullptr;
673 } 591 last_image_ = nullptr;
674 592 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 } 593 }
681 594
682 } // namespace media 595 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/skcanvas_video_renderer.h ('k') | media/blink/skcanvas_video_renderer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698