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

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

Issue 2121043002: 16 bpp video stream capture, render and WebGL usage - Realsense R200 & SR300 support. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tests: cc, skcanvas_video_renderer, wrtcrecorder... Fake capture supports Y16. Created 4 years, 2 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
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/renderers/skcanvas_video_renderer.h" 5 #include "media/renderers/skcanvas_video_renderer.h"
6 6
7 #include <GLES3/gl3.h>
7 #include <limits> 8 #include <limits>
8 9
9 #include "base/macros.h" 10 #include "base/macros.h"
10 #include "gpu/GLES2/gl2extchromium.h" 11 #include "gpu/GLES2/gl2extchromium.h"
11 #include "gpu/command_buffer/client/gles2_interface.h" 12 #include "gpu/command_buffer/client/gles2_interface.h"
12 #include "gpu/command_buffer/common/mailbox_holder.h" 13 #include "gpu/command_buffer/common/mailbox_holder.h"
14 #include "media/base/data_buffer.h"
13 #include "media/base/video_frame.h" 15 #include "media/base/video_frame.h"
14 #include "media/base/yuv_convert.h" 16 #include "media/base/yuv_convert.h"
15 #include "skia/ext/texture_handle.h" 17 #include "skia/ext/texture_handle.h"
16 #include "third_party/libyuv/include/libyuv.h" 18 #include "third_party/libyuv/include/libyuv.h"
17 #include "third_party/skia/include/core/SkCanvas.h" 19 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkImage.h" 20 #include "third_party/skia/include/core/SkImage.h"
19 #include "third_party/skia/include/core/SkImageGenerator.h" 21 #include "third_party/skia/include/core/SkImageGenerator.h"
20 #include "third_party/skia/include/gpu/GrContext.h" 22 #include "third_party/skia/include/gpu/GrContext.h"
21 #include "third_party/skia/include/gpu/GrPaint.h" 23 #include "third_party/skia/include/gpu/GrPaint.h"
22 #include "third_party/skia/include/gpu/GrTexture.h" 24 #include "third_party/skia/include/gpu/GrTexture.h"
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 return img; 160 return img;
159 } 161 }
160 162
161 // Creates a SkImage from a |video_frame| backed by native resources. 163 // Creates a SkImage from a |video_frame| backed by native resources.
162 // The SkImage will take ownership of the underlying resource. 164 // The SkImage will take ownership of the underlying resource.
163 sk_sp<SkImage> NewSkImageFromVideoFrameNative(VideoFrame* video_frame, 165 sk_sp<SkImage> NewSkImageFromVideoFrameNative(VideoFrame* video_frame,
164 const Context3D& context_3d) { 166 const Context3D& context_3d) {
165 DCHECK(PIXEL_FORMAT_ARGB == video_frame->format() || 167 DCHECK(PIXEL_FORMAT_ARGB == video_frame->format() ||
166 PIXEL_FORMAT_XRGB == video_frame->format() || 168 PIXEL_FORMAT_XRGB == video_frame->format() ||
167 PIXEL_FORMAT_NV12 == video_frame->format() || 169 PIXEL_FORMAT_NV12 == video_frame->format() ||
168 PIXEL_FORMAT_UYVY == video_frame->format()); 170 PIXEL_FORMAT_UYVY == video_frame->format() ||
171 PIXEL_FORMAT_Y8 == video_frame->format() ||
172 PIXEL_FORMAT_Y16 == video_frame->format());
169 173
170 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0); 174 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
171 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D || 175 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D ||
172 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB || 176 mailbox_holder.texture_target == GL_TEXTURE_RECTANGLE_ARB ||
173 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES) 177 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES)
174 << mailbox_holder.texture_target; 178 << mailbox_holder.texture_target;
175 179
176 gpu::gles2::GLES2Interface* gl = context_3d.gl; 180 gpu::gles2::GLES2Interface* gl = context_3d.gl;
177 unsigned source_texture = 0; 181 unsigned source_texture = 0;
178 if (mailbox_holder.texture_target != GL_TEXTURE_2D) { 182 if (mailbox_holder.texture_target != GL_TEXTURE_2D) {
(...skipping 23 matching lines...) Expand all
202 desc.fTextureHandle = 206 desc.fTextureHandle =
203 skia::GrGLTextureInfoToGrBackendObject(source_texture_info); 207 skia::GrGLTextureInfoToGrBackendObject(source_texture_info);
204 return SkImage::MakeFromAdoptedTexture(context_3d.gr_context, desc); 208 return SkImage::MakeFromAdoptedTexture(context_3d.gr_context, desc);
205 } 209 }
206 210
207 } // anonymous namespace 211 } // anonymous namespace
208 212
209 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU. 213 // Generates an RGB image from a VideoFrame. Convert YUV to RGB plain on GPU.
210 class VideoImageGenerator : public SkImageGenerator { 214 class VideoImageGenerator : public SkImageGenerator {
211 public: 215 public:
212 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame) 216 VideoImageGenerator(const scoped_refptr<VideoFrame>& frame, SkColorType type)
213 : SkImageGenerator( 217 : SkImageGenerator(SkImageInfo::Make(frame->visible_rect().width(),
214 SkImageInfo::MakeN32Premul(frame->visible_rect().width(), 218 frame->visible_rect().height(),
215 frame->visible_rect().height())), 219 type,
220 kPremul_SkAlphaType)),
216 frame_(frame) { 221 frame_(frame) {
217 DCHECK(!frame_->HasTextures()); 222 DCHECK(!frame_->HasTextures());
218 } 223 }
219 ~VideoImageGenerator() override {} 224 ~VideoImageGenerator() override {}
220 225
221 protected: 226 protected:
222 bool onGetPixels(const SkImageInfo& info, 227 bool onGetPixels(const SkImageInfo& info,
223 void* pixels, 228 void* pixels,
224 size_t row_bytes, 229 size_t row_bytes,
225 SkPMColor ctable[], 230 SkPMColor ctable[],
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 SkRect dest; 349 SkRect dest;
345 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom()); 350 dest.set(dest_rect.x(), dest_rect.y(), dest_rect.right(), dest_rect.bottom());
346 351
347 SkPaint paint; 352 SkPaint paint;
348 paint.setAlpha(alpha); 353 paint.setAlpha(alpha);
349 354
350 // Paint black rectangle if there isn't a frame available or the 355 // Paint black rectangle if there isn't a frame available or the
351 // frame has an unexpected format. 356 // frame has an unexpected format.
352 if (!video_frame.get() || video_frame->natural_size().IsEmpty() || 357 if (!video_frame.get() || video_frame->natural_size().IsEmpty() ||
353 !(media::IsYuvPlanar(video_frame->format()) || 358 !(media::IsYuvPlanar(video_frame->format()) ||
359 video_frame->format() == media::PIXEL_FORMAT_Y16 ||
354 video_frame->HasTextures())) { 360 video_frame->HasTextures())) {
355 canvas->drawRect(dest, paint); 361 canvas->drawRect(dest, paint);
356 canvas->flush(); 362 canvas->flush();
357 return; 363 return;
358 } 364 }
359 365
360 gpu::gles2::GLES2Interface* gl = context_3d.gl; 366 gpu::gles2::GLES2Interface* gl = context_3d.gl;
361 if (!UpdateLastImage(video_frame, context_3d)) 367 if (!UpdateLastImage(video_frame, context_3d))
362 return; 368 return;
363 369
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 for (int row = 0; row < video_frame->rows(plane); row++) { 489 for (int row = 0; row < video_frame->rows(plane); row++) {
484 for (int x = 0; x < width; x++) { 490 for (int x = 0; x < width; x++) {
485 dst[x] = src[x] >> shift; 491 dst[x] = src[x] >> shift;
486 } 492 }
487 src += video_frame->stride(plane) / 2; 493 src += video_frame->stride(plane) / 2;
488 dst += ret->stride(plane); 494 dst += ret->stride(plane);
489 } 495 }
490 } 496 }
491 return ret; 497 return ret;
492 } 498 }
499
500 void ConvertY16ToRGBA(const VideoFrame* video_frame,
501 void* rgba_pixels,
502 size_t rgba_row_bytes) {
503 const uint8_t* source =
504 reinterpret_cast<const uint8_t*>(video_frame->visible_data(0));
505 uint8_t* out = reinterpret_cast<uint8_t*>(rgba_pixels);
506 const size_t stride = video_frame->stride(0);
507 for (int i = 0; i < video_frame->visible_rect().height(); ++i) {
508 const uint16_t* row = reinterpret_cast<const uint16_t*>(source);
509 uint32_t* rgba = reinterpret_cast<uint32_t*>(out);
510 for (const uint16_t* row_end = row + video_frame->visible_rect().width();
511 row < row_end;)
512 *rgba++ = *row++;
513 out += rgba_row_bytes;
514 source += stride;
515 }
516 }
517
518 void FlipAndConvertY16(const uint8_t* input,
519 uint8_t* output,
520 unsigned format,
521 unsigned type,
522 bool flip_y,
523 size_t row_bytes,
524 size_t stride,
525 size_t output_row_bytes,
526 size_t height) {
527 DCHECK(input != output);
528 for (size_t i = 0; i < height; ++i) {
529 const uint16_t* in = reinterpret_cast<const uint16_t*>(input + i * stride);
530 uint8_t* out = flip_y ? output + output_row_bytes * (height - i - 1)
531 : output + output_row_bytes * height;
532 if ((format == GL_RG && type == GL_UNSIGNED_BYTE) ||
533 (format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT)) {
534 memcpy(out, input + i * stride, row_bytes);
535 } else if (format == GL_RED && type == GL_FLOAT) {
536 float* out_row = reinterpret_cast<float*>(out);
537 const uint16_t* in_end = in + row_bytes / 2;
538 while (in < in_end)
539 *out_row++ = *in++ / 65536.f;
540 } else {
541 NOTREACHED();
542 }
543 }
544 }
545
546 bool isSingleRedComponentFormat(unsigned internal_format) {
547 switch (internal_format) {
548 case GL_R8:
549 case GL_R16F:
550 case GL_R32F:
551 case GL_R8UI:
552 case GL_R8I:
553 case GL_R16UI:
554 case GL_R16I:
555 case GL_R32UI:
556 case GL_R32I:
557 return true;
558 default:
559 return false;
560 }
561 }
493 } 562 }
494 563
495 // static 564 // static
496 void SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels( 565 void SkCanvasVideoRenderer::ConvertVideoFrameToRGBPixels(
497 const VideoFrame* video_frame, 566 const VideoFrame* video_frame,
498 void* rgb_pixels, 567 void* rgb_pixels,
499 size_t row_bytes) { 568 size_t row_bytes) {
500 if (!video_frame->IsMappable()) { 569 if (!video_frame->IsMappable()) {
501 NOTREACHED() << "Cannot extract pixels from non-CPU frame formats."; 570 NOTREACHED() << "Cannot extract pixels from non-CPU frame formats.";
502 return; 571 return;
503 } 572 }
504 if (!media::IsYuvPlanar(video_frame->format())) {
505 NOTREACHED() << "Non YUV formats are not supported";
506 return;
507 }
508 573
509 switch (video_frame->format()) { 574 switch (video_frame->format()) {
510 case PIXEL_FORMAT_YV12: 575 case PIXEL_FORMAT_YV12:
511 case PIXEL_FORMAT_I420: 576 case PIXEL_FORMAT_I420:
512 if (CheckColorSpace(video_frame, COLOR_SPACE_JPEG)) { 577 if (CheckColorSpace(video_frame, COLOR_SPACE_JPEG)) {
513 LIBYUV_J420_TO_ARGB(video_frame->visible_data(VideoFrame::kYPlane), 578 LIBYUV_J420_TO_ARGB(video_frame->visible_data(VideoFrame::kYPlane),
514 video_frame->stride(VideoFrame::kYPlane), 579 video_frame->stride(VideoFrame::kYPlane),
515 video_frame->visible_data(VideoFrame::kUPlane), 580 video_frame->visible_data(VideoFrame::kUPlane),
516 video_frame->stride(VideoFrame::kUPlane), 581 video_frame->stride(VideoFrame::kUPlane),
517 video_frame->visible_data(VideoFrame::kVPlane), 582 video_frame->visible_data(VideoFrame::kVPlane),
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 case PIXEL_FORMAT_YUV420P10: 652 case PIXEL_FORMAT_YUV420P10:
588 case PIXEL_FORMAT_YUV422P10: 653 case PIXEL_FORMAT_YUV422P10:
589 case PIXEL_FORMAT_YUV444P10: { 654 case PIXEL_FORMAT_YUV444P10: {
590 scoped_refptr<VideoFrame> temporary_frame = 655 scoped_refptr<VideoFrame> temporary_frame =
591 DownShiftHighbitVideoFrame(video_frame); 656 DownShiftHighbitVideoFrame(video_frame);
592 ConvertVideoFrameToRGBPixels(temporary_frame.get(), rgb_pixels, 657 ConvertVideoFrameToRGBPixels(temporary_frame.get(), rgb_pixels,
593 row_bytes); 658 row_bytes);
594 break; 659 break;
595 } 660 }
596 661
662 case PIXEL_FORMAT_Y16:
663 ConvertY16ToRGBA(video_frame, rgb_pixels, row_bytes);
664 break;
665
597 case PIXEL_FORMAT_NV12: 666 case PIXEL_FORMAT_NV12:
598 case PIXEL_FORMAT_NV21: 667 case PIXEL_FORMAT_NV21:
599 case PIXEL_FORMAT_UYVY: 668 case PIXEL_FORMAT_UYVY:
600 case PIXEL_FORMAT_YUY2: 669 case PIXEL_FORMAT_YUY2:
601 case PIXEL_FORMAT_ARGB: 670 case PIXEL_FORMAT_ARGB:
602 case PIXEL_FORMAT_XRGB: 671 case PIXEL_FORMAT_XRGB:
603 case PIXEL_FORMAT_RGB24: 672 case PIXEL_FORMAT_RGB24:
604 case PIXEL_FORMAT_RGB32: 673 case PIXEL_FORMAT_RGB32:
605 case PIXEL_FORMAT_MJPEG: 674 case PIXEL_FORMAT_MJPEG:
606 case PIXEL_FORMAT_MT21: 675 case PIXEL_FORMAT_MT21:
676 case PIXEL_FORMAT_Y8:
607 case PIXEL_FORMAT_UNKNOWN: 677 case PIXEL_FORMAT_UNKNOWN:
608 NOTREACHED(); 678 NOTREACHED() << "Only YUV formats and Y16 are supported.";
609 } 679 }
610 } 680 }
611 681
612 // static 682 // static
613 void SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture( 683 void SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
614 gpu::gles2::GLES2Interface* gl, 684 gpu::gles2::GLES2Interface* gl,
615 VideoFrame* video_frame, 685 VideoFrame* video_frame,
616 unsigned int texture, 686 unsigned int texture,
617 unsigned int internal_format, 687 unsigned int internal_format,
618 unsigned int type, 688 unsigned int type,
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 // canvas context. 768 // canvas context.
699 const GLuint64 dest_fence_sync = destination_gl->InsertFenceSyncCHROMIUM(); 769 const GLuint64 dest_fence_sync = destination_gl->InsertFenceSyncCHROMIUM();
700 destination_gl->ShallowFlushCHROMIUM(); 770 destination_gl->ShallowFlushCHROMIUM();
701 gpu::SyncToken dest_sync_token; 771 gpu::SyncToken dest_sync_token;
702 destination_gl->GenSyncTokenCHROMIUM(dest_fence_sync, 772 destination_gl->GenSyncTokenCHROMIUM(dest_fence_sync,
703 dest_sync_token.GetData()); 773 dest_sync_token.GetData());
704 canvas_gl->WaitSyncTokenCHROMIUM(dest_sync_token.GetConstData()); 774 canvas_gl->WaitSyncTokenCHROMIUM(dest_sync_token.GetConstData());
705 775
706 SyncTokenClientImpl client(canvas_gl); 776 SyncTokenClientImpl client(canvas_gl);
707 video_frame->UpdateReleaseSyncToken(&client); 777 video_frame->UpdateReleaseSyncToken(&client);
778 } else if (video_frame->format() == PIXEL_FORMAT_Y16 &&
779 isSingleRedComponentFormat(internal_format)) {
780 CopyRG8ToRedTextureData(context_3d, destination_gl, video_frame.get(),
781 texture, internal_format, type, flip_y);
708 } else { 782 } else {
709 CopyVideoFrameSingleTextureToGLTexture(destination_gl, video_frame.get(), 783 CopyVideoFrameSingleTextureToGLTexture(destination_gl, video_frame.get(),
710 texture, internal_format, type, 784 texture, internal_format, type,
711 premultiply_alpha, flip_y); 785 premultiply_alpha, flip_y);
712 } 786 }
713
714 return true; 787 return true;
715 } 788 }
716 789
790 bool SkCanvasVideoRenderer::TexImageImpl(const char* functionID,
791 unsigned target,
792 gpu::gles2::GLES2Interface* gl,
793 VideoFrame* frame,
794 int level,
795 int internalformat,
796 unsigned format,
797 unsigned type,
798 int xoffset,
799 int yoffset,
800 int zoffset,
801 bool flip_y,
802 bool premultiplyAlpha) {
803 DCHECK(frame);
804 DCHECK(!frame->HasTextures());
805
806 bool has_alpha = false;
807 bool need_conversion = false;
808 unsigned output_bits_per_pixel;
809 switch (frame->format()) {
810 case PIXEL_FORMAT_Y16:
811 // Allow reinterpreting RG8 buffer here as R16 or FLOAT.
812 if ((format != GL_RED_INTEGER || type != GL_UNSIGNED_SHORT) &&
813 (format != GL_RG || type != GL_UNSIGNED_BYTE) &&
814 (format != GL_RED || type != GL_FLOAT))
815 return false;
816 need_conversion =
817 !(format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT) &&
818 !(format == GL_RG && type == GL_UNSIGNED_BYTE);
819 output_bits_per_pixel = (type == GL_FLOAT) ? 32 : 16;
820 break;
821 case PIXEL_FORMAT_Y8:
822 if (format != GL_RED || type != GL_UNSIGNED_BYTE)
823 return false;
824 output_bits_per_pixel = 8;
825 break;
826 default:
827 return false;
828 }
829 need_conversion =
830 need_conversion || (frame->stride(0) != frame->row_bytes(0));
831 unsigned source_bits_per_pixel =
832 VideoFrame::PlaneBitsPerPixel(frame->format(), 0);
833 DCHECK_EQ(source_bits_per_pixel % 8, 0U);
834
835 if (has_alpha && premultiplyAlpha) {
836 NOTREACHED() << "Premultiply alpha is not supported.";
837 return false;
838 }
839 if (xoffset || yoffset || zoffset) {
840 NOTREACHED() << "Offsets are not supported.";
841 return false;
842 }
843
844 // Handle other than Y16 formats here only if there is no repacking required.
845 // The reason for this is that this method handles Y16 specifics and provides
846 // optimization (compared to default implementation in
847 // WebGLRenderingContextBase::texImageImpl) for situations where there is no
848 // need to do repacking pixel data.
849 if (frame->format() != PIXEL_FORMAT_Y16 && (flip_y || need_conversion))
850 return false;
851
852 uint8_t* data;
853 scoped_refptr<DataBuffer> temp_buffer;
854 size_t width = frame->visible_rect().width();
855 size_t height = frame->visible_rect().height();
856 if (flip_y || need_conversion) {
857 size_t output_row_bytes =
858 frame->row_bytes(0) * output_bits_per_pixel / source_bits_per_pixel;
859 temp_buffer = new DataBuffer(output_row_bytes * height);
860 data = temp_buffer->writable_data();
861 DCHECK_EQ(frame->format(), PIXEL_FORMAT_Y16);
862 FlipAndConvertY16(frame->visible_data(0), data, format, type, flip_y,
863 frame->row_bytes(0), frame->stride(0), output_row_bytes,
864 height);
865 } else {
866 data = frame->visible_data(0);
867 }
868
869 if (!strcmp(functionID, "texImage2D")) {
870 gl->TexImage2D(target, level, internalformat, width, height, 0, format,
871 type, data);
872 } else if (!strcmp(functionID, "texSubImage2D")) {
873 gl->TexSubImage2D(target, level, xoffset, yoffset, width, height, format,
874 type, data);
875 } else {
876 DCHECK(!strcmp(functionID, "texSubImage3D"));
877 gl->TexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height,
878 1, format, type, data);
879 }
880 return true;
881 }
882
883 void SkCanvasVideoRenderer::CopyRG8ToRedTextureData(
884 const Context3D& context_3d,
885 gpu::gles2::GLES2Interface* target_gl,
886 VideoFrame* frame,
887 unsigned texture,
888 unsigned internal_format,
889 unsigned type,
890 bool flip_y) {
891 // TODO(astojilj): After GLES 3.2, use glCopyImageSubData for RG8 to R16UI
892 // conversion. glCopyImageSubData is already available as extension on some of
893 // the Android devices (GL_EXT_COPY_IMAGE).
894 gpu::gles2::GLES2Interface* local_gl = context_3d.gl;
895
896 // Get source texture on local_gl.
897 unsigned source_texture;
898 {
899 const gpu::MailboxHolder& mailbox_holder = frame->mailbox_holder(0);
900 DCHECK(mailbox_holder.texture_target == GL_TEXTURE_2D)
901 << mailbox_holder.texture_target;
902
903 local_gl->WaitSyncTokenCHROMIUM(mailbox_holder.sync_token.GetConstData());
904 source_texture = local_gl->CreateAndConsumeTextureCHROMIUM(
905 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
906 }
907
908 // Reinterpret RG8 to R32F on local_gl. Later, do the copy on target_gl.
909 unsigned intermediate_texture;
910 local_gl->GenTextures(1, &intermediate_texture);
911 DoCopyRG8ToRedTextureData(context_3d, source_texture, intermediate_texture,
912 GL_R32F, GL_FLOAT, frame->visible_rect().width(),
913 frame->visible_rect().height(), flip_y);
914 local_gl->DeleteTextures(1, &source_texture);
915
916 // Get intermediate_texture to target_gl.
917 {
918 gpu::MailboxHolder mailbox_holder;
919 mailbox_holder.texture_target = GL_TEXTURE_2D;
920 local_gl->GenMailboxCHROMIUM(mailbox_holder.mailbox.name);
921 local_gl->ProduceTextureDirectCHROMIUM(intermediate_texture,
922 mailbox_holder.texture_target,
923 mailbox_holder.mailbox.name);
924 // Wait for mailbox creation on local context before consuming it and
925 // copying from it on the consumer context.
926 const GLuint64 fence_sync = local_gl->InsertFenceSyncCHROMIUM();
927 local_gl->ShallowFlushCHROMIUM();
928 local_gl->GenSyncTokenCHROMIUM(fence_sync,
929 mailbox_holder.sync_token.GetData());
930
931 target_gl->WaitSyncTokenCHROMIUM(mailbox_holder.sync_token.GetConstData());
932 uint32_t target_intermediate_texture =
933 target_gl->CreateAndConsumeTextureCHROMIUM(
934 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
935
936 local_gl->DeleteTextures(1, &intermediate_texture);
937 intermediate_texture = target_intermediate_texture;
938 }
939
940 // Copy intermediate_texture to texture.
941 target_gl->CopyTextureCHROMIUM(intermediate_texture, texture, internal_format,
942 type, false, false, false);
943 target_gl->DeleteTextures(1, &intermediate_texture);
944
945 // Undo gr_context state changes introduced in this function.
946 context_3d.gr_context->resetContext(
947 kMisc_GrGLBackendState | kBlend_GrGLBackendState |
948 kView_GrGLBackendState | kStencil_GrGLBackendState |
949 kVertex_GrGLBackendState | kProgram_GrGLBackendState |
950 kTextureBinding_GrGLBackendState);
951 SyncTokenClientImpl client(target_gl);
952 frame->UpdateReleaseSyncToken(&client);
953 }
954
955 void SkCanvasVideoRenderer::DoCopyRG8ToRedTextureData(
956 const Context3D& context_3d,
957 unsigned source_texture,
958 unsigned texture,
959 unsigned internal_format,
960 unsigned type,
961 unsigned width,
962 unsigned height,
963 bool flip_y) {
964 gpu::gles2::GLES2Interface* gl = context_3d.gl;
965
966 if (cached_gl_resources_gr_context_id_ != context_3d.gr_context->uniqueID()) {
967 rg8_to_red_program_ = 0;
968 rg8_to_red_vertices_buffer_ = 0;
969 cached_gl_resources_gr_context_id_ = context_3d.gr_context->uniqueID();
970 }
971
972 gl->EnableVertexAttribArray(0);
973 if (!rg8_to_red_vertices_buffer_) {
974 gl->GenBuffers(1, &rg8_to_red_vertices_buffer_);
975 gl->BindBuffer(GL_ARRAY_BUFFER, rg8_to_red_vertices_buffer_);
976 const GLfloat vertices[] = {-1.f, -1.f, 1.f, -1.f, -1.0f, 1.f, 1.f, 1.f};
977 gl->BufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
978 gl->VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
979 }
980
981 if (!rg8_to_red_program_) {
982 rg8_to_red_program_ = gl->CreateProgram();
983 GLuint vertex_shader = gl->CreateShader(GL_VERTEX_SHADER);
984 std::string source = std::string(
985 "\
986 #ifdef GL_ES\n\
987 precision mediump float;\n\
988 #define TexCoordPrecision mediump\n\
989 #else\n\
990 #define TexCoordPrecision\n\
991 #endif\n\
992 uniform vec2 coord_transform;\n\
993 attribute vec2 a_pos;\n\
994 varying TexCoordPrecision vec2 v_uv;\n\
995 void main(void) {\n\
996 gl_Position = vec4(a_pos, 0, 1.0);\n\
997 v_uv = (a_pos * coord_transform) + 0.5;\n\
998 }\n");
999 const char* vertex_source = source.c_str();
1000 gl->ShaderSource(vertex_shader, 1, &vertex_source, 0);
1001 gl->CompileShader(vertex_shader);
1002 gl->AttachShader(rg8_to_red_program_, vertex_shader);
1003
1004 GLuint fragment_shader = gl->CreateShader(GL_FRAGMENT_SHADER);
1005 source = std::string(
1006 "\
1007 #ifdef GL_ES\n\
1008 precision mediump float;\n\
1009 #define TexCoordPrecision mediump\n\
1010 #else\n\
1011 #define TexCoordPrecision\n\
1012 #endif\n\
1013 uniform sampler2D u_sampler;\n\
1014 varying TexCoordPrecision vec2 v_uv;\n\
1015 void main(void) {\n\
1016 vec4 color = texture2D(u_sampler, v_uv);\n\
1017 gl_FragColor.r = (color.r * 0.00390625) + color.g;\n\
1018 }\n");
1019 const char* fragment_source = source.c_str();
1020 gl->ShaderSource(fragment_shader, 1, &fragment_source, 0);
1021 gl->CompileShader(fragment_shader);
1022
1023 gl->AttachShader(rg8_to_red_program_, fragment_shader);
1024
1025 gl->BindAttribLocation(rg8_to_red_program_, 0, "a_pos");
1026 gl->LinkProgram(rg8_to_red_program_);
1027 }
1028
1029 gl->UseProgram(rg8_to_red_program_);
1030 gl->Uniform1i(gl->GetUniformLocation(rg8_to_red_program_, "u_sampler"), 0);
1031 gl->Uniform2f(gl->GetUniformLocation(rg8_to_red_program_, "coord_transform"),
1032 0.5, flip_y ? -0.5 : 0.5);
1033
1034 GLuint fbo;
1035 gl->GenFramebuffers(1, &fbo);
1036 gl->ActiveTexture(GL_TEXTURE0);
1037 unsigned target = GL_TEXTURE_2D;
1038 gl->BindTexture(target, texture);
1039 gl->TexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RED,
1040 type, 0);
1041
1042 gl->TexParameterf(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1043 gl->TexParameterf(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1044 gl->TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1045 gl->TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1046 gl->BindFramebuffer(GL_FRAMEBUFFER, fbo);
1047 gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
1048 texture, 0);
1049
1050 gl->BindTexture(target, source_texture);
1051 gl->TexParameterf(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1052 gl->TexParameterf(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1053 gl->TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1054 gl->TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1055
1056 gl->Disable(GL_DEPTH_TEST);
1057 gl->Disable(GL_STENCIL_TEST);
1058 gl->Disable(GL_CULL_FACE);
1059 gl->ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1060 gl->DepthMask(GL_FALSE);
1061 gl->Disable(GL_BLEND);
1062 gl->Disable(GL_SCISSOR_TEST);
1063 gl->Viewport(0, 0, width, height);
1064 gl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1065 gl->Flush();
1066 gl->BindFramebuffer(GL_FRAMEBUFFER, 0);
1067 gl->DeleteFramebuffers(1, &fbo);
1068 }
1069
717 void SkCanvasVideoRenderer::ResetCache() { 1070 void SkCanvasVideoRenderer::ResetCache() {
718 DCHECK(thread_checker_.CalledOnValidThread()); 1071 DCHECK(thread_checker_.CalledOnValidThread());
719 // Clear cached values. 1072 // Clear cached values.
720 last_image_ = nullptr; 1073 last_image_ = nullptr;
721 last_timestamp_ = kNoTimestamp; 1074 last_timestamp_ = kNoTimestamp;
722 } 1075 }
723 1076
724 bool SkCanvasVideoRenderer::UpdateLastImage( 1077 bool SkCanvasVideoRenderer::UpdateLastImage(
725 const scoped_refptr<VideoFrame>& video_frame, 1078 const scoped_refptr<VideoFrame>& video_frame,
726 const Context3D& context_3d) { 1079 const Context3D& context_3d) {
727 if (!last_image_ || video_frame->timestamp() != last_timestamp_) { 1080 if (!last_image_ || video_frame->timestamp() != last_timestamp_) {
728 ResetCache(); 1081 ResetCache();
729 // Generate a new image. 1082 // Generate a new image.
730 // Note: Skia will hold onto |video_frame| via |video_generator| only when 1083 // Note: Skia will hold onto |video_frame| via |video_generator| only when
731 // |video_frame| is software. 1084 // |video_frame| is software.
732 // Holding |video_frame| longer than this call when using GPUVideoDecoder 1085 // Holding |video_frame| longer than this call when using GPUVideoDecoder
733 // could cause problems since the pool of VideoFrames has a fixed size. 1086 // could cause problems since the pool of VideoFrames has a fixed size.
734 if (video_frame->HasTextures()) { 1087 if (video_frame->HasTextures()) {
735 DCHECK(context_3d.gr_context); 1088 DCHECK(context_3d.gr_context);
736 DCHECK(context_3d.gl); 1089 DCHECK(context_3d.gl);
737 if (media::VideoFrame::NumPlanes(video_frame->format()) > 1) { 1090 if (media::VideoFrame::NumPlanes(video_frame->format()) > 1) {
738 last_image_ = 1091 last_image_ =
739 NewSkImageFromVideoFrameYUVTextures(video_frame.get(), context_3d); 1092 NewSkImageFromVideoFrameYUVTextures(video_frame.get(), context_3d);
740 } else { 1093 } else {
741 last_image_ = 1094 last_image_ =
742 NewSkImageFromVideoFrameNative(video_frame.get(), context_3d); 1095 NewSkImageFromVideoFrameNative(video_frame.get(), context_3d);
743 } 1096 }
744 } else { 1097 } else {
745 auto* video_generator = new VideoImageGenerator(video_frame); 1098 auto* video_generator = new VideoImageGenerator(
1099 video_frame, (video_frame->format() == media::PIXEL_FORMAT_Y16)
1100 ? kRGBA_8888_SkColorType
1101 : kN32_SkColorType);
746 last_image_ = SkImage::MakeFromGenerator(video_generator); 1102 last_image_ = SkImage::MakeFromGenerator(video_generator);
747 } 1103 }
748 if (!last_image_) // Couldn't create the SkImage. 1104 if (!last_image_) // Couldn't create the SkImage.
749 return false; 1105 return false;
750 last_timestamp_ = video_frame->timestamp(); 1106 last_timestamp_ = video_frame->timestamp();
751 } 1107 }
752 last_image_deleting_timer_.Reset(); 1108 last_image_deleting_timer_.Reset();
753 DCHECK(!!last_image_); 1109 DCHECK(!!last_image_);
754 return true; 1110 return true;
755 } 1111 }
756 1112
757 } // namespace media 1113 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698