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 "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h" | 5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 source_y, source_width, source_height); | 610 source_y, source_width, source_height); |
611 } | 611 } |
612 | 612 |
613 decoder->RestoreTextureState(source_id); | 613 decoder->RestoreTextureState(source_id); |
614 decoder->RestoreTextureState(dest_id); | 614 decoder->RestoreTextureState(dest_id); |
615 decoder->RestoreTextureUnitBindings(0); | 615 decoder->RestoreTextureUnitBindings(0); |
616 decoder->RestoreActiveTexture(); | 616 decoder->RestoreActiveTexture(); |
617 decoder->RestoreFramebufferBindings(); | 617 decoder->RestoreFramebufferBindings(); |
618 } | 618 } |
619 | 619 |
| 620 // Convert RGBA/UNSIGNED_BYTE source to RGB/UNSIGNED_BYTE destination. |
| 621 void convertToRGB(const uint8_t* source, |
| 622 uint8_t* destination, |
| 623 unsigned pixelsPerRow) { |
| 624 for (unsigned i = 0; i < pixelsPerRow; ++i) { |
| 625 destination[0] = source[0]; |
| 626 destination[1] = source[1]; |
| 627 destination[2] = source[2]; |
| 628 source += 4; |
| 629 destination += 3; |
| 630 } |
| 631 } |
| 632 |
| 633 // Convert RGBA/UNSIGNED_BYTE source to RGB/FLOAT destination. |
| 634 void convertToRGBFloat(const uint8_t* source, |
| 635 float* destination, |
| 636 unsigned pixelsPerRow) { |
| 637 const float scaleFactor = 1.0f / 255.0f; |
| 638 for (unsigned i = 0; i < pixelsPerRow; ++i) { |
| 639 destination[0] = source[0] * scaleFactor; |
| 640 destination[1] = source[1] * scaleFactor; |
| 641 destination[2] = source[2] * scaleFactor; |
| 642 source += 4; |
| 643 destination += 3; |
| 644 } |
| 645 } |
| 646 |
| 647 // Prepare the image data to be uploaded to a texture in pixel unpack buffer. |
| 648 void prepareUnpackBuffer(GLuint buffer[2], |
| 649 bool is_es, |
| 650 GLenum format, |
| 651 GLenum type, |
| 652 GLsizei width, |
| 653 GLsizei height) { |
| 654 uint32_t pixel_num = width * height; |
| 655 glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer[0]); |
| 656 |
| 657 // Result of glReadPixels with format == GL_RGB and type == GL_UNSIGNED_BYTE |
| 658 // from read framebuffer in RGBA fromat is not correct on desktop core |
| 659 // profile on both Linux Mesa and Linux NVIDIA. This may be a driver bug. |
| 660 bool is_rgb_unsigned_byte = format == GL_RGB && type == GL_UNSIGNED_BYTE; |
| 661 if ((!is_es && !is_rgb_unsigned_byte) || |
| 662 (format == GL_RGBA && type == GL_UNSIGNED_BYTE)) { |
| 663 uint32_t bytes_per_group = |
| 664 gpu::gles2::GLES2Util::ComputeImageGroupSize(format, type); |
| 665 glBufferData(GL_PIXEL_PACK_BUFFER, pixel_num * bytes_per_group, 0, |
| 666 GL_STATIC_READ); |
| 667 glReadPixels(0, 0, width, height, format, type, 0); |
| 668 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer[0]); |
| 669 return; |
| 670 } |
| 671 |
| 672 uint32_t bytes_per_group = |
| 673 gpu::gles2::GLES2Util::ComputeImageGroupSize(GL_RGBA, GL_UNSIGNED_BYTE); |
| 674 uint32_t buf_size = pixel_num * bytes_per_group; |
| 675 |
| 676 if (format == GL_RGB && type == GL_FLOAT) { |
| 677 glBufferData(GL_PIXEL_PACK_BUFFER, buf_size, 0, GL_STREAM_READ); |
| 678 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 679 |
| 680 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); |
| 681 std::unique_ptr<uint8_t[]> pixels(new uint8_t[width * height * 4]); |
| 682 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); |
| 683 |
| 684 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer[1]); |
| 685 bytes_per_group = |
| 686 gpu::gles2::GLES2Util::ComputeImageGroupSize(format, type); |
| 687 buf_size = pixel_num * bytes_per_group; |
| 688 /* |
| 689 glBufferData(GL_PIXEL_UNPACK_BUFFER, buf_size, 0, GL_STATIC_DRAW); |
| 690 void* data = |
| 691 glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, buf_size, GL_MAP_WRITE_BIT); |
| 692 convertToRGBFloat((uint8_t*)pixels, (float*)data, pixel_num); |
| 693 glUnmapBuffer(GL_PIXEL_PACK_BUFFER); |
| 694 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 695 */ |
| 696 |
| 697 std::unique_ptr<float[]> data(new float[width * height * 3]); |
| 698 convertToRGBFloat(pixels.get(), data.get(), pixel_num); |
| 699 glBufferData(GL_PIXEL_UNPACK_BUFFER, buf_size, data.get(), GL_STATIC_DRAW); |
| 700 return; |
| 701 } |
| 702 |
| 703 if (format == GL_RGB && type == GL_UNSIGNED_BYTE) { |
| 704 glBufferData(GL_PIXEL_PACK_BUFFER, buf_size, 0, GL_DYNAMIC_DRAW); |
| 705 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 706 void* pixels = glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, buf_size, |
| 707 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT); |
| 708 void* data = pixels; |
| 709 convertToRGB((uint8_t*)pixels, (uint8_t*)data, pixel_num); |
| 710 glUnmapBuffer(GL_PIXEL_PACK_BUFFER); |
| 711 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer[0]); |
| 712 return; |
| 713 } |
| 714 |
| 715 NOTREACHED(); |
| 716 } |
| 717 |
| 718 void DoReadbackAndTexImage(bool is_tex_image, |
| 719 const gpu::gles2::GLES2Decoder* decoder, |
| 720 GLenum source_target, |
| 721 GLuint source_id, |
| 722 GLint source_level, |
| 723 GLenum dest_target, |
| 724 GLuint dest_id, |
| 725 GLint dest_level, |
| 726 GLenum dest_internal_format, |
| 727 GLint xoffset, |
| 728 GLint yoffset, |
| 729 GLsizei width, |
| 730 GLsizei height, |
| 731 GLuint framebuffer) { |
| 732 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), source_target); |
| 733 GLenum dest_binding_target = |
| 734 gpu::gles2::GLES2Util::GLFaceTargetToTextureTarget(dest_target); |
| 735 DCHECK(dest_binding_target == GL_TEXTURE_2D || |
| 736 dest_binding_target == GL_TEXTURE_CUBE_MAP); |
| 737 DCHECK(source_level == 0 || decoder->GetFeatureInfo()->IsES3Capable()); |
| 738 if (BindFramebufferTexture2D(source_target, source_id, source_level, |
| 739 framebuffer)) { |
| 740 glBindTexture(dest_binding_target, dest_id); |
| 741 glTexParameterf(dest_binding_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 742 glTexParameterf(dest_binding_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 743 glTexParameteri(dest_binding_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 744 glTexParameteri(dest_binding_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 745 |
| 746 GLenum format = GL_RGBA; |
| 747 GLenum type = GL_UNSIGNED_BYTE; |
| 748 switch (dest_internal_format) { |
| 749 case GL_RGB9_E5: |
| 750 format = GL_RGB; |
| 751 type = GL_FLOAT; |
| 752 break; |
| 753 case GL_SRGB_EXT: |
| 754 case GL_SRGB8: |
| 755 format = GL_RGB; |
| 756 break; |
| 757 case GL_RGB5_A1: |
| 758 case GL_SRGB_ALPHA_EXT: |
| 759 case GL_SRGB8_ALPHA8: |
| 760 break; |
| 761 default: |
| 762 NOTREACHED(); |
| 763 break; |
| 764 } |
| 765 |
| 766 // TODO(qiankun.miao@intel.com): PIXEL_PACK_BUFFER and PIXEL_UNPACK_BUFFER |
| 767 // are not supported in ES2. |
| 768 bool is_es = decoder->GetFeatureInfo()->gl_version_info().is_es; |
| 769 DCHECK(!is_es || decoder->GetFeatureInfo()->gl_version_info().is_es3); |
| 770 |
| 771 uint32_t buffer_num = is_es && format == GL_RGB && type == GL_FLOAT ? 2 : 1; |
| 772 GLuint buffer[2]; |
| 773 glGenBuffersARB(buffer_num, buffer); |
| 774 prepareUnpackBuffer(buffer, is_es, format, type, width, height); |
| 775 |
| 776 if (is_tex_image) { |
| 777 glTexImage2D(dest_target, dest_level, dest_internal_format, width, height, |
| 778 0, format, type, 0); |
| 779 } else { |
| 780 glTexSubImage2D(dest_target, dest_level, xoffset, yoffset, width, height, |
| 781 format, type, 0); |
| 782 } |
| 783 glDeleteBuffersARB(buffer_num, buffer); |
| 784 } |
| 785 |
| 786 decoder->RestoreTextureState(source_id); |
| 787 decoder->RestoreTextureState(dest_id); |
| 788 decoder->RestoreTextureUnitBindings(0); |
| 789 decoder->RestoreActiveTexture(); |
| 790 decoder->RestoreFramebufferBindings(); |
| 791 decoder->RestoreBufferBindings(); |
| 792 } |
| 793 |
620 } // namespace | 794 } // namespace |
621 | 795 |
622 namespace gpu { | 796 namespace gpu { |
623 namespace gles2 { | 797 namespace gles2 { |
624 | 798 |
625 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager() | 799 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager() |
626 : initialized_(false), | 800 : initialized_(false), |
627 nv_egl_stream_consumer_external_(false), | 801 nv_egl_stream_consumer_external_(false), |
628 vertex_shaders_(kNumVertexShaders, 0u), | 802 vertex_shaders_(kNumVertexShaders, 0u), |
629 fragment_shaders_(kNumFragmentShaders, 0u), | 803 fragment_shaders_(kNumFragmentShaders, 0u), |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 width, height, framebuffer_); | 902 width, height, framebuffer_); |
729 return; | 903 return; |
730 } | 904 } |
731 | 905 |
732 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. | 906 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. |
733 GLuint dest_texture = dest_id; | 907 GLuint dest_texture = dest_id; |
734 GLuint intermediate_texture = 0; | 908 GLuint intermediate_texture = 0; |
735 GLint original_dest_level = dest_level; | 909 GLint original_dest_level = dest_level; |
736 GLenum original_dest_target = dest_target; | 910 GLenum original_dest_target = dest_target; |
737 GLenum original_internal_format = dest_internal_format; | 911 GLenum original_internal_format = dest_internal_format; |
738 if (method == DRAW_AND_COPY) { | 912 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
739 GLenum adjusted_internal_format = | 913 GLenum adjusted_internal_format = |
740 getIntermediateFormat(dest_internal_format); | 914 method == DRAW_AND_READBACK |
| 915 ? GL_RGBA |
| 916 : getIntermediateFormat(dest_internal_format); |
741 dest_target = GL_TEXTURE_2D; | 917 dest_target = GL_TEXTURE_2D; |
742 glGenTextures(1, &intermediate_texture); | 918 glGenTextures(1, &intermediate_texture); |
743 glBindTexture(dest_target, intermediate_texture); | 919 glBindTexture(dest_target, intermediate_texture); |
744 GLenum format = TextureManager::ExtractFormatFromStorageFormat( | 920 GLenum format = TextureManager::ExtractFormatFromStorageFormat( |
745 adjusted_internal_format); | 921 adjusted_internal_format); |
746 GLenum type = | 922 GLenum type = |
747 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); | 923 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); |
748 | 924 |
749 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, | 925 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, |
750 format, type, nullptr); | 926 format, type, nullptr); |
751 dest_texture = intermediate_texture; | 927 dest_texture = intermediate_texture; |
752 dest_level = 0; | 928 dest_level = 0; |
753 dest_internal_format = adjusted_internal_format; | 929 dest_internal_format = adjusted_internal_format; |
754 } | 930 } |
755 // Use kIdentityMatrix if no transform passed in. | 931 // Use kIdentityMatrix if no transform passed in. |
756 DoCopyTextureWithTransform( | 932 DoCopyTextureWithTransform( |
757 decoder, source_target, source_id, source_level, source_internal_format, | 933 decoder, source_target, source_id, source_level, source_internal_format, |
758 dest_target, dest_texture, dest_level, dest_internal_format, width, | 934 dest_target, dest_texture, dest_level, dest_internal_format, width, |
759 height, flip_y, premultiply_alpha, unpremultiply_alpha, kIdentityMatrix); | 935 height, flip_y, premultiply_alpha, unpremultiply_alpha, kIdentityMatrix); |
760 | 936 |
761 if (method == DRAW_AND_COPY) { | 937 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
762 source_level = 0; | 938 source_level = 0; |
763 DoCopyTexImage2D(decoder, dest_target, intermediate_texture, source_level, | 939 if (method == DRAW_AND_COPY) { |
764 original_dest_target, dest_id, original_dest_level, | 940 DoCopyTexImage2D(decoder, dest_target, intermediate_texture, source_level, |
765 original_internal_format, width, height, framebuffer_); | 941 original_dest_target, dest_id, original_dest_level, |
| 942 original_internal_format, width, height, framebuffer_); |
| 943 } else if (method == DRAW_AND_READBACK) { |
| 944 DoReadbackAndTexImage(true, decoder, dest_target, intermediate_texture, |
| 945 source_level, original_dest_target, dest_id, |
| 946 original_dest_level, original_internal_format, 0, 0, |
| 947 width, height, framebuffer_); |
| 948 } |
766 glDeleteTextures(1, &intermediate_texture); | 949 glDeleteTextures(1, &intermediate_texture); |
767 } | 950 } |
768 } | 951 } |
769 | 952 |
770 void CopyTextureCHROMIUMResourceManager::DoCopySubTexture( | 953 void CopyTextureCHROMIUMResourceManager::DoCopySubTexture( |
771 const gles2::GLES2Decoder* decoder, | 954 const gles2::GLES2Decoder* decoder, |
772 GLenum source_target, | 955 GLenum source_target, |
773 GLuint source_id, | 956 GLuint source_id, |
774 GLint source_level, | 957 GLint source_level, |
775 GLenum source_internal_format, | 958 GLenum source_internal_format, |
(...skipping 22 matching lines...) Expand all Loading... |
798 return; | 981 return; |
799 } | 982 } |
800 | 983 |
801 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. | 984 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. |
802 GLint dest_xoffset = xoffset; | 985 GLint dest_xoffset = xoffset; |
803 GLint dest_yoffset = yoffset; | 986 GLint dest_yoffset = yoffset; |
804 GLuint dest_texture = dest_id; | 987 GLuint dest_texture = dest_id; |
805 GLint original_dest_level = dest_level; | 988 GLint original_dest_level = dest_level; |
806 GLenum original_dest_target = dest_target; | 989 GLenum original_dest_target = dest_target; |
807 GLuint intermediate_texture = 0; | 990 GLuint intermediate_texture = 0; |
808 if (method == DRAW_AND_COPY) { | 991 GLenum original_internal_format = dest_internal_format; |
| 992 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
809 GLenum adjusted_internal_format = | 993 GLenum adjusted_internal_format = |
810 getIntermediateFormat(dest_internal_format); | 994 method == DRAW_AND_READBACK |
| 995 ? GL_RGBA |
| 996 : getIntermediateFormat(dest_internal_format); |
811 dest_target = GL_TEXTURE_2D; | 997 dest_target = GL_TEXTURE_2D; |
812 glGenTextures(1, &intermediate_texture); | 998 glGenTextures(1, &intermediate_texture); |
813 glBindTexture(dest_target, intermediate_texture); | 999 glBindTexture(dest_target, intermediate_texture); |
814 GLenum format = TextureManager::ExtractFormatFromStorageFormat( | 1000 GLenum format = TextureManager::ExtractFormatFromStorageFormat( |
815 adjusted_internal_format); | 1001 adjusted_internal_format); |
816 GLenum type = | 1002 GLenum type = |
817 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); | 1003 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); |
818 | 1004 |
819 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, | 1005 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, |
820 format, type, nullptr); | 1006 format, type, nullptr); |
821 dest_texture = intermediate_texture; | 1007 dest_texture = intermediate_texture; |
822 dest_level = 0; | 1008 dest_level = 0; |
823 dest_internal_format = adjusted_internal_format; | 1009 dest_internal_format = adjusted_internal_format; |
824 dest_xoffset = 0; | 1010 dest_xoffset = 0; |
825 dest_yoffset = 0; | 1011 dest_yoffset = 0; |
826 dest_width = width; | 1012 dest_width = width; |
827 dest_height = height; | 1013 dest_height = height; |
828 } | 1014 } |
829 | 1015 |
830 DoCopySubTextureWithTransform( | 1016 DoCopySubTextureWithTransform( |
831 decoder, source_target, source_id, source_level, source_internal_format, | 1017 decoder, source_target, source_id, source_level, source_internal_format, |
832 dest_target, dest_texture, dest_level, dest_internal_format, dest_xoffset, | 1018 dest_target, dest_texture, dest_level, dest_internal_format, dest_xoffset, |
833 dest_yoffset, x, y, width, height, dest_width, dest_height, source_width, | 1019 dest_yoffset, x, y, width, height, dest_width, dest_height, source_width, |
834 source_height, flip_y, premultiply_alpha, unpremultiply_alpha, | 1020 source_height, flip_y, premultiply_alpha, unpremultiply_alpha, |
835 kIdentityMatrix); | 1021 kIdentityMatrix); |
836 | 1022 |
837 if (method == DRAW_AND_COPY) { | 1023 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
838 source_level = 0; | 1024 source_level = 0; |
839 DoCopyTexSubImage2D(decoder, dest_target, intermediate_texture, | 1025 if (method == DRAW_AND_COPY) { |
840 source_level, original_dest_target, dest_id, | 1026 DoCopyTexSubImage2D(decoder, dest_target, intermediate_texture, |
841 original_dest_level, xoffset, yoffset, 0, 0, width, | 1027 source_level, original_dest_target, dest_id, |
842 height, framebuffer_); | 1028 original_dest_level, xoffset, yoffset, 0, 0, width, |
| 1029 height, framebuffer_); |
| 1030 } else if (method == DRAW_AND_READBACK) { |
| 1031 DoReadbackAndTexImage(false, decoder, dest_target, intermediate_texture, |
| 1032 source_level, original_dest_target, dest_id, |
| 1033 original_dest_level, original_internal_format, |
| 1034 xoffset, yoffset, width, height, framebuffer_); |
| 1035 } |
843 glDeleteTextures(1, &intermediate_texture); | 1036 glDeleteTextures(1, &intermediate_texture); |
844 } | 1037 } |
845 } | 1038 } |
846 | 1039 |
847 void CopyTextureCHROMIUMResourceManager::DoCopySubTextureWithTransform( | 1040 void CopyTextureCHROMIUMResourceManager::DoCopySubTextureWithTransform( |
848 const gles2::GLES2Decoder* decoder, | 1041 const gles2::GLES2Decoder* decoder, |
849 GLenum source_target, | 1042 GLenum source_target, |
850 GLuint source_id, | 1043 GLuint source_id, |
851 GLint source_level, | 1044 GLint source_level, |
852 GLenum source_internal_format, | 1045 GLenum source_internal_format, |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 decoder->RestoreTextureUnitBindings(0); | 1326 decoder->RestoreTextureUnitBindings(0); |
1134 decoder->RestoreActiveTexture(); | 1327 decoder->RestoreActiveTexture(); |
1135 decoder->RestoreProgramBindings(); | 1328 decoder->RestoreProgramBindings(); |
1136 decoder->RestoreBufferBindings(); | 1329 decoder->RestoreBufferBindings(); |
1137 decoder->RestoreFramebufferBindings(); | 1330 decoder->RestoreFramebufferBindings(); |
1138 decoder->RestoreGlobalState(); | 1331 decoder->RestoreGlobalState(); |
1139 } | 1332 } |
1140 | 1333 |
1141 } // namespace gles2 | 1334 } // namespace gles2 |
1142 } // namespace gpu | 1335 } // namespace gpu |
OLD | NEW |