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_STATIC_READ); |
| 678 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 679 void* pixels = |
| 680 glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, buf_size, GL_MAP_READ_BIT); |
| 681 |
| 682 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer[1]); |
| 683 bytes_per_group = |
| 684 gpu::gles2::GLES2Util::ComputeImageGroupSize(format, type); |
| 685 buf_size = pixel_num * bytes_per_group; |
| 686 glBufferData(GL_PIXEL_UNPACK_BUFFER, buf_size, 0, GL_STATIC_DRAW); |
| 687 void* data = |
| 688 glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, buf_size, GL_MAP_WRITE_BIT); |
| 689 convertToRGBFloat((uint8_t*)pixels, (float*)data, pixel_num); |
| 690 glUnmapBuffer(GL_PIXEL_PACK_BUFFER); |
| 691 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 692 return; |
| 693 } |
| 694 |
| 695 if (format == GL_RGB && type == GL_UNSIGNED_BYTE) { |
| 696 glBufferData(GL_PIXEL_PACK_BUFFER, buf_size, 0, GL_DYNAMIC_DRAW); |
| 697 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 698 void* pixels = glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, buf_size, |
| 699 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT); |
| 700 void* data = pixels; |
| 701 convertToRGB((uint8_t*)pixels, (uint8_t*)data, pixel_num); |
| 702 glUnmapBuffer(GL_PIXEL_PACK_BUFFER); |
| 703 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer[0]); |
| 704 return; |
| 705 } |
| 706 |
| 707 NOTREACHED(); |
| 708 } |
| 709 |
| 710 void DoReadbackAndTexImage(bool is_tex_image, |
| 711 const gpu::gles2::GLES2Decoder* decoder, |
| 712 GLenum source_target, |
| 713 GLuint source_id, |
| 714 GLint source_level, |
| 715 GLenum dest_target, |
| 716 GLuint dest_id, |
| 717 GLint dest_level, |
| 718 GLenum dest_internal_format, |
| 719 GLint xoffset, |
| 720 GLint yoffset, |
| 721 GLsizei width, |
| 722 GLsizei height, |
| 723 GLuint framebuffer) { |
| 724 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), source_target); |
| 725 GLenum dest_binding_target = |
| 726 gpu::gles2::GLES2Util::GLFaceTargetToTextureTarget(dest_target); |
| 727 DCHECK(dest_binding_target == GL_TEXTURE_2D || |
| 728 dest_binding_target == GL_TEXTURE_CUBE_MAP); |
| 729 DCHECK(source_level == 0 || decoder->GetFeatureInfo()->IsES3Capable()); |
| 730 if (BindFramebufferTexture2D(source_target, source_id, source_level, |
| 731 framebuffer)) { |
| 732 glBindTexture(dest_binding_target, dest_id); |
| 733 glTexParameterf(dest_binding_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 734 glTexParameterf(dest_binding_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 735 glTexParameteri(dest_binding_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 736 glTexParameteri(dest_binding_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 737 |
| 738 GLenum format = GL_RGBA; |
| 739 GLenum type = GL_UNSIGNED_BYTE; |
| 740 switch (dest_internal_format) { |
| 741 case GL_RGB9_E5: |
| 742 format = GL_RGB; |
| 743 type = GL_FLOAT; |
| 744 break; |
| 745 case GL_SRGB_EXT: |
| 746 case GL_SRGB8: |
| 747 format = GL_RGB; |
| 748 break; |
| 749 case GL_RGB5_A1: |
| 750 case GL_SRGB_ALPHA_EXT: |
| 751 case GL_SRGB8_ALPHA8: |
| 752 break; |
| 753 default: |
| 754 NOTREACHED(); |
| 755 break; |
| 756 } |
| 757 |
| 758 // TODO(qiankun.miao@intel.com): PIXEL_PACK_BUFFER and PIXEL_UNPACK_BUFFER |
| 759 // are not supported in ES2. |
| 760 bool is_es = decoder->GetFeatureInfo()->gl_version_info().is_es; |
| 761 DCHECK(!is_es || decoder->GetFeatureInfo()->gl_version_info().is_es3); |
| 762 |
| 763 uint32_t buffer_num = is_es && format == GL_RGB && type == GL_FLOAT ? 2 : 1; |
| 764 GLuint buffer[2]; |
| 765 glGenBuffersARB(buffer_num, buffer); |
| 766 prepareUnpackBuffer(buffer, is_es, format, type, width, height); |
| 767 |
| 768 if (is_tex_image) { |
| 769 glTexImage2D(dest_target, dest_level, dest_internal_format, width, height, |
| 770 0, format, type, 0); |
| 771 } else { |
| 772 glTexSubImage2D(dest_target, dest_level, xoffset, yoffset, width, height, |
| 773 format, type, 0); |
| 774 } |
| 775 glDeleteBuffersARB(buffer_num, buffer); |
| 776 } |
| 777 |
| 778 decoder->RestoreTextureState(source_id); |
| 779 decoder->RestoreTextureState(dest_id); |
| 780 decoder->RestoreTextureUnitBindings(0); |
| 781 decoder->RestoreActiveTexture(); |
| 782 decoder->RestoreFramebufferBindings(); |
| 783 decoder->RestoreBufferBindings(); |
| 784 } |
| 785 |
620 } // namespace | 786 } // namespace |
621 | 787 |
622 namespace gpu { | 788 namespace gpu { |
623 namespace gles2 { | 789 namespace gles2 { |
624 | 790 |
625 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager() | 791 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager() |
626 : initialized_(false), | 792 : initialized_(false), |
627 nv_egl_stream_consumer_external_(false), | 793 nv_egl_stream_consumer_external_(false), |
628 vertex_shaders_(kNumVertexShaders, 0u), | 794 vertex_shaders_(kNumVertexShaders, 0u), |
629 fragment_shaders_(kNumFragmentShaders, 0u), | 795 fragment_shaders_(kNumFragmentShaders, 0u), |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 width, height, framebuffer_); | 894 width, height, framebuffer_); |
729 return; | 895 return; |
730 } | 896 } |
731 | 897 |
732 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. | 898 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. |
733 GLuint dest_texture = dest_id; | 899 GLuint dest_texture = dest_id; |
734 GLuint intermediate_texture = 0; | 900 GLuint intermediate_texture = 0; |
735 GLint original_dest_level = dest_level; | 901 GLint original_dest_level = dest_level; |
736 GLenum original_dest_target = dest_target; | 902 GLenum original_dest_target = dest_target; |
737 GLenum original_internal_format = dest_internal_format; | 903 GLenum original_internal_format = dest_internal_format; |
738 if (method == DRAW_AND_COPY) { | 904 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
739 GLenum adjusted_internal_format = | 905 GLenum adjusted_internal_format = |
740 getIntermediateFormat(dest_internal_format); | 906 method == DRAW_AND_READBACK |
| 907 ? GL_RGBA |
| 908 : getIntermediateFormat(dest_internal_format); |
741 dest_target = GL_TEXTURE_2D; | 909 dest_target = GL_TEXTURE_2D; |
742 glGenTextures(1, &intermediate_texture); | 910 glGenTextures(1, &intermediate_texture); |
743 glBindTexture(dest_target, intermediate_texture); | 911 glBindTexture(dest_target, intermediate_texture); |
744 GLenum format = TextureManager::ExtractFormatFromStorageFormat( | 912 GLenum format = TextureManager::ExtractFormatFromStorageFormat( |
745 adjusted_internal_format); | 913 adjusted_internal_format); |
746 GLenum type = | 914 GLenum type = |
747 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); | 915 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); |
748 | 916 |
749 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, | 917 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, |
750 format, type, nullptr); | 918 format, type, nullptr); |
751 dest_texture = intermediate_texture; | 919 dest_texture = intermediate_texture; |
752 dest_level = 0; | 920 dest_level = 0; |
753 dest_internal_format = adjusted_internal_format; | 921 dest_internal_format = adjusted_internal_format; |
754 } | 922 } |
755 // Use kIdentityMatrix if no transform passed in. | 923 // Use kIdentityMatrix if no transform passed in. |
756 DoCopyTextureWithTransform( | 924 DoCopyTextureWithTransform( |
757 decoder, source_target, source_id, source_level, source_internal_format, | 925 decoder, source_target, source_id, source_level, source_internal_format, |
758 dest_target, dest_texture, dest_level, dest_internal_format, width, | 926 dest_target, dest_texture, dest_level, dest_internal_format, width, |
759 height, flip_y, premultiply_alpha, unpremultiply_alpha, kIdentityMatrix); | 927 height, flip_y, premultiply_alpha, unpremultiply_alpha, kIdentityMatrix); |
760 | 928 |
761 if (method == DRAW_AND_COPY) { | 929 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
762 source_level = 0; | 930 source_level = 0; |
763 DoCopyTexImage2D(decoder, dest_target, intermediate_texture, source_level, | 931 if (method == DRAW_AND_COPY) { |
764 original_dest_target, dest_id, original_dest_level, | 932 DoCopyTexImage2D(decoder, dest_target, intermediate_texture, source_level, |
765 original_internal_format, width, height, framebuffer_); | 933 original_dest_target, dest_id, original_dest_level, |
| 934 original_internal_format, width, height, framebuffer_); |
| 935 } else if (method == DRAW_AND_READBACK) { |
| 936 DoReadbackAndTexImage(true, decoder, dest_target, intermediate_texture, |
| 937 source_level, original_dest_target, dest_id, |
| 938 original_dest_level, original_internal_format, 0, 0, |
| 939 width, height, framebuffer_); |
| 940 } |
766 glDeleteTextures(1, &intermediate_texture); | 941 glDeleteTextures(1, &intermediate_texture); |
767 } | 942 } |
768 } | 943 } |
769 | 944 |
770 void CopyTextureCHROMIUMResourceManager::DoCopySubTexture( | 945 void CopyTextureCHROMIUMResourceManager::DoCopySubTexture( |
771 const gles2::GLES2Decoder* decoder, | 946 const gles2::GLES2Decoder* decoder, |
772 GLenum source_target, | 947 GLenum source_target, |
773 GLuint source_id, | 948 GLuint source_id, |
774 GLint source_level, | 949 GLint source_level, |
775 GLenum source_internal_format, | 950 GLenum source_internal_format, |
(...skipping 22 matching lines...) Expand all Loading... |
798 return; | 973 return; |
799 } | 974 } |
800 | 975 |
801 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. | 976 // Draw to level 0 of an intermediate GL_TEXTURE_2D texture. |
802 GLint dest_xoffset = xoffset; | 977 GLint dest_xoffset = xoffset; |
803 GLint dest_yoffset = yoffset; | 978 GLint dest_yoffset = yoffset; |
804 GLuint dest_texture = dest_id; | 979 GLuint dest_texture = dest_id; |
805 GLint original_dest_level = dest_level; | 980 GLint original_dest_level = dest_level; |
806 GLenum original_dest_target = dest_target; | 981 GLenum original_dest_target = dest_target; |
807 GLuint intermediate_texture = 0; | 982 GLuint intermediate_texture = 0; |
808 if (method == DRAW_AND_COPY) { | 983 GLenum original_internal_format = dest_internal_format; |
| 984 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
809 GLenum adjusted_internal_format = | 985 GLenum adjusted_internal_format = |
810 getIntermediateFormat(dest_internal_format); | 986 method == DRAW_AND_READBACK |
| 987 ? GL_RGBA |
| 988 : getIntermediateFormat(dest_internal_format); |
811 dest_target = GL_TEXTURE_2D; | 989 dest_target = GL_TEXTURE_2D; |
812 glGenTextures(1, &intermediate_texture); | 990 glGenTextures(1, &intermediate_texture); |
813 glBindTexture(dest_target, intermediate_texture); | 991 glBindTexture(dest_target, intermediate_texture); |
814 GLenum format = TextureManager::ExtractFormatFromStorageFormat( | 992 GLenum format = TextureManager::ExtractFormatFromStorageFormat( |
815 adjusted_internal_format); | 993 adjusted_internal_format); |
816 GLenum type = | 994 GLenum type = |
817 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); | 995 TextureManager::ExtractTypeFromStorageFormat(adjusted_internal_format); |
818 | 996 |
819 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, | 997 glTexImage2D(dest_target, 0, adjusted_internal_format, width, height, 0, |
820 format, type, nullptr); | 998 format, type, nullptr); |
821 dest_texture = intermediate_texture; | 999 dest_texture = intermediate_texture; |
822 dest_level = 0; | 1000 dest_level = 0; |
823 dest_internal_format = adjusted_internal_format; | 1001 dest_internal_format = adjusted_internal_format; |
824 dest_xoffset = 0; | 1002 dest_xoffset = 0; |
825 dest_yoffset = 0; | 1003 dest_yoffset = 0; |
826 dest_width = width; | 1004 dest_width = width; |
827 dest_height = height; | 1005 dest_height = height; |
828 } | 1006 } |
829 | 1007 |
830 DoCopySubTextureWithTransform( | 1008 DoCopySubTextureWithTransform( |
831 decoder, source_target, source_id, source_level, source_internal_format, | 1009 decoder, source_target, source_id, source_level, source_internal_format, |
832 dest_target, dest_texture, dest_level, dest_internal_format, dest_xoffset, | 1010 dest_target, dest_texture, dest_level, dest_internal_format, dest_xoffset, |
833 dest_yoffset, x, y, width, height, dest_width, dest_height, source_width, | 1011 dest_yoffset, x, y, width, height, dest_width, dest_height, source_width, |
834 source_height, flip_y, premultiply_alpha, unpremultiply_alpha, | 1012 source_height, flip_y, premultiply_alpha, unpremultiply_alpha, |
835 kIdentityMatrix); | 1013 kIdentityMatrix); |
836 | 1014 |
837 if (method == DRAW_AND_COPY) { | 1015 if (method == DRAW_AND_COPY || method == DRAW_AND_READBACK) { |
838 source_level = 0; | 1016 source_level = 0; |
839 DoCopyTexSubImage2D(decoder, dest_target, intermediate_texture, | 1017 if (method == DRAW_AND_COPY) { |
840 source_level, original_dest_target, dest_id, | 1018 DoCopyTexSubImage2D(decoder, dest_target, intermediate_texture, |
841 original_dest_level, xoffset, yoffset, 0, 0, width, | 1019 source_level, original_dest_target, dest_id, |
842 height, framebuffer_); | 1020 original_dest_level, xoffset, yoffset, 0, 0, width, |
| 1021 height, framebuffer_); |
| 1022 } else if (method == DRAW_AND_READBACK) { |
| 1023 DoReadbackAndTexImage(false, decoder, dest_target, intermediate_texture, |
| 1024 source_level, original_dest_target, dest_id, |
| 1025 original_dest_level, original_internal_format, |
| 1026 xoffset, yoffset, width, height, framebuffer_); |
| 1027 } |
843 glDeleteTextures(1, &intermediate_texture); | 1028 glDeleteTextures(1, &intermediate_texture); |
844 } | 1029 } |
845 } | 1030 } |
846 | 1031 |
847 void CopyTextureCHROMIUMResourceManager::DoCopySubTextureWithTransform( | 1032 void CopyTextureCHROMIUMResourceManager::DoCopySubTextureWithTransform( |
848 const gles2::GLES2Decoder* decoder, | 1033 const gles2::GLES2Decoder* decoder, |
849 GLenum source_target, | 1034 GLenum source_target, |
850 GLuint source_id, | 1035 GLuint source_id, |
851 GLint source_level, | 1036 GLint source_level, |
852 GLenum source_internal_format, | 1037 GLenum source_internal_format, |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 decoder->RestoreTextureUnitBindings(0); | 1318 decoder->RestoreTextureUnitBindings(0); |
1134 decoder->RestoreActiveTexture(); | 1319 decoder->RestoreActiveTexture(); |
1135 decoder->RestoreProgramBindings(); | 1320 decoder->RestoreProgramBindings(); |
1136 decoder->RestoreBufferBindings(); | 1321 decoder->RestoreBufferBindings(); |
1137 decoder->RestoreFramebufferBindings(); | 1322 decoder->RestoreFramebufferBindings(); |
1138 decoder->RestoreGlobalState(); | 1323 decoder->RestoreGlobalState(); |
1139 } | 1324 } |
1140 | 1325 |
1141 } // namespace gles2 | 1326 } // namespace gles2 |
1142 } // namespace gpu | 1327 } // namespace gpu |
OLD | NEW |