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