| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "remoting/client/gl_render_layer.h" | 5 #include "remoting/client/gl_render_layer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "remoting/client/gl_canvas.h" | 8 #include "remoting/client/gl_canvas.h" |
| 9 #include "remoting/client/gl_helpers.h" | 9 #include "remoting/client/gl_helpers.h" |
| 10 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 10 |
| 11 |
| 12 namespace remoting { |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 // Assign texture coordinates to buffers for use in shader program. | 16 // Assign texture coordinates to buffers for use in shader program. |
| 15 const float kVertices[] = { | 17 const float kVertices[] = { |
| 16 // Points order: upper-left, bottom-left, upper-right, bottom-right. | 18 // Points order: upper-left, bottom-left, upper-right, bottom-right. |
| 17 | 19 |
| 18 // Positions to draw the texture on the normalized canvas coordinate. | 20 // Positions to draw the texture on the normalized canvas coordinate. |
| 19 0, 0, 0, 1, 1, 0, 1, 1, | 21 0, 0, 0, 1, 1, 0, 1, 1, |
| 20 | 22 |
| 21 // Region of the texture to be used (normally the whole texture). | 23 // Region of the texture to be used (normally the whole texture). |
| 22 0, 0, 0, 1, 1, 0, 1, 1}; | 24 0, 0, 0, 1, 1, 0, 1, 1}; |
| 23 | 25 |
| 24 const int kDefaultUpdateBufferCapacity = | 26 const int kDefaultUpdateBufferCapacity = |
| 25 2048 * 2048 * webrtc::DesktopFrame::kBytesPerPixel; | 27 2048 * 2048 * GlRenderLayer::kBytesPerPixel; |
| 26 | 28 |
| 29 void PackDirtyRegion(uint8_t* dest, |
| 30 const uint8_t* source, |
| 31 int width, |
| 32 int height, |
| 33 int stride) { |
| 34 for (int i = 0; i < height; i++) { |
| 35 memcpy(dest, source, width * GlRenderLayer::kBytesPerPixel); |
| 36 source += stride; |
| 37 dest += GlRenderLayer::kBytesPerPixel * width; |
| 38 } |
| 27 } | 39 } |
| 28 | 40 |
| 29 namespace remoting { | 41 } // namespace |
| 30 | 42 |
| 31 GlRenderLayer::GlRenderLayer(int texture_id, GlCanvas* canvas) | 43 GlRenderLayer::GlRenderLayer(int texture_id, GlCanvas* canvas) |
| 32 : texture_id_(texture_id), canvas_(canvas) { | 44 : texture_id_(texture_id), canvas_(canvas) { |
| 33 texture_handle_ = CreateTexture(); | 45 texture_handle_ = CreateTexture(); |
| 34 buffer_handle_ = CreateBuffer(kVertices, sizeof(kVertices)); | 46 buffer_handle_ = CreateBuffer(kVertices, sizeof(kVertices)); |
| 35 } | 47 } |
| 36 | 48 |
| 37 GlRenderLayer::~GlRenderLayer() { | 49 GlRenderLayer::~GlRenderLayer() { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 glDeleteBuffers(1, &buffer_handle_); | 51 glDeleteBuffers(1, &buffer_handle_); |
| 40 glDeleteTextures(1, &texture_handle_); | 52 glDeleteTextures(1, &texture_handle_); |
| 41 } | 53 } |
| 42 | 54 |
| 43 void GlRenderLayer::SetTexture(const uint8_t* texture, int width, int height) { | 55 void GlRenderLayer::SetTexture(const uint8_t* texture, int width, int height) { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 CHECK(width > 0 && height > 0); | 57 CHECK(width > 0 && height > 0); |
| 46 texture_set_ = true; | 58 texture_set_ = true; |
| 47 glActiveTexture(GL_TEXTURE0 + texture_id_); | 59 glActiveTexture(GL_TEXTURE0 + texture_id_); |
| 48 glBindTexture(GL_TEXTURE_2D, texture_handle_); | 60 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
| 49 | 61 |
| 50 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, | 62 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, |
| 51 GL_UNSIGNED_BYTE, texture); | 63 GL_UNSIGNED_BYTE, texture); |
| 64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 54 | 68 |
| 55 glBindTexture(GL_TEXTURE_2D, 0); | 69 glBindTexture(GL_TEXTURE_2D, 0); |
| 56 } | 70 } |
| 57 | 71 |
| 58 void PackDirtyRegion(uint8_t* dest, | |
| 59 const uint8_t* source, | |
| 60 int width, | |
| 61 int height, | |
| 62 int stride) { | |
| 63 for (int i = 0; i < height; i++) { | |
| 64 memcpy(dest, source, width * webrtc::DesktopFrame::kBytesPerPixel); | |
| 65 source += stride; | |
| 66 dest += webrtc::DesktopFrame::kBytesPerPixel * width; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void GlRenderLayer::UpdateTexture(const uint8_t* subtexture, | 72 void GlRenderLayer::UpdateTexture(const uint8_t* subtexture, |
| 71 int offset_x, | 73 int offset_x, |
| 72 int offset_y, | 74 int offset_y, |
| 73 int width, | 75 int width, |
| 74 int height, | 76 int height, |
| 75 int stride) { | 77 int stride) { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 DCHECK(texture_set_); | 79 DCHECK(texture_set_); |
| 78 DCHECK(width > 0 && height > 0); | 80 DCHECK(width > 0 && height > 0); |
| 79 glActiveTexture(GL_TEXTURE0 + texture_id_); | 81 glActiveTexture(GL_TEXTURE0 + texture_id_); |
| 80 glBindTexture(GL_TEXTURE_2D, texture_handle_); | 82 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
| 81 | 83 |
| 82 bool stride_multiple_of_bytes_per_pixel = | 84 bool stride_multiple_of_bytes_per_pixel = stride % kBytesPerPixel == 0; |
| 83 stride % webrtc::DesktopFrame::kBytesPerPixel == 0; | |
| 84 bool loosely_packed = | 85 bool loosely_packed = |
| 85 !stride_multiple_of_bytes_per_pixel || | 86 !stride_multiple_of_bytes_per_pixel || |
| 86 (stride > 0 && stride != webrtc::DesktopFrame::kBytesPerPixel * width); | 87 (stride > 0 && stride != kBytesPerPixel * width); |
| 87 | 88 |
| 88 const void* buffer_to_update = subtexture; | 89 const void* buffer_to_update = subtexture; |
| 89 | 90 |
| 90 if (loosely_packed) { | 91 if (loosely_packed) { |
| 91 if (stride_multiple_of_bytes_per_pixel && canvas_->GetGlVersion() >= 3) { | 92 if (stride_multiple_of_bytes_per_pixel && canvas_->GetGlVersion() >= 3) { |
| 92 glPixelStorei(GL_UNPACK_ROW_LENGTH, | 93 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride / kBytesPerPixel); |
| 93 stride / webrtc::DesktopFrame::kBytesPerPixel); | |
| 94 } else { | 94 } else { |
| 95 // Doesn't support GL_UNPACK_ROW_LENGTH or stride not multiple of | 95 // Doesn't support GL_UNPACK_ROW_LENGTH or stride not multiple of |
| 96 // kBytesPerPixel. Manually pack the data. | 96 // kBytesPerPixel. Manually pack the data. |
| 97 int required_size = width * height * webrtc::DesktopFrame::kBytesPerPixel; | 97 int required_size = width * height * kBytesPerPixel; |
| 98 if (update_buffer_size_ < required_size) { | 98 if (update_buffer_size_ < required_size) { |
| 99 if (required_size < kDefaultUpdateBufferCapacity) { | 99 if (required_size < kDefaultUpdateBufferCapacity) { |
| 100 update_buffer_size_ = kDefaultUpdateBufferCapacity; | 100 update_buffer_size_ = kDefaultUpdateBufferCapacity; |
| 101 } else { | 101 } else { |
| 102 update_buffer_size_ = required_size; | 102 update_buffer_size_ = required_size; |
| 103 } | 103 } |
| 104 update_buffer_.reset(new uint8_t[update_buffer_size_]); | 104 update_buffer_.reset(new uint8_t[update_buffer_size_]); |
| 105 } | 105 } |
| 106 PackDirtyRegion(update_buffer_.get(), subtexture, width, height, stride); | 106 PackDirtyRegion(update_buffer_.get(), subtexture, width, height, stride); |
| 107 buffer_to_update = update_buffer_.get(); | 107 buffer_to_update = update_buffer_.get(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 127 | 127 |
| 128 void GlRenderLayer::SetTextureVisibleArea( | 128 void GlRenderLayer::SetTextureVisibleArea( |
| 129 const std::array<float, 8>& positions) { | 129 const std::array<float, 8>& positions) { |
| 130 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); | 131 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); |
| 132 glBufferSubData(GL_ARRAY_BUFFER, sizeof(kVertices) / 2, sizeof(kVertices) / 2, | 132 glBufferSubData(GL_ARRAY_BUFFER, sizeof(kVertices) / 2, sizeof(kVertices) / 2, |
| 133 positions.data()); | 133 positions.data()); |
| 134 glBindBuffer(GL_ARRAY_BUFFER, 0); | 134 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void GlRenderLayer::Draw() { | 137 void GlRenderLayer::Draw(float alpha_multiplier) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 DCHECK(texture_set_); | 139 DCHECK(texture_set_); |
| 140 canvas_->DrawTexture(texture_id_, texture_handle_, buffer_handle_); | 140 canvas_->DrawTexture(texture_id_, texture_handle_, buffer_handle_, |
| 141 alpha_multiplier); |
| 141 } | 142 } |
| 142 | 143 |
| 143 } // namespace remoting | 144 } // namespace remoting |
| OLD | NEW |