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/opengl/gl_render_layer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "remoting/client/opengl/gl_canvas.h" |
| 9 #include "remoting/client/opengl/gl_helpers.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Assign texture coordinates to buffers for use in shader program. |
| 14 const float kVertices[] = { |
| 15 // Points order: upper-left, bottom-left, upper-right, bottom-right. |
| 16 |
| 17 // Positions to draw the texture on the normalized canvas coordinate. |
| 18 0, 0, 0, 1, 1, 0, 1, 1, |
| 19 |
| 20 // Region of the texture to be used (normally the whole texture). |
| 21 0, 0, 0, 1, 1, 0, 1, 1}; |
| 22 } |
| 23 |
| 24 namespace remoting { |
| 25 |
| 26 GlRenderLayer::GlRenderLayer(int texture_id, GlCanvas* canvas) |
| 27 : texture_id_(texture_id), canvas_(canvas) { |
| 28 texture_handle_ = CreateTexture(); |
| 29 buffer_handle_ = CreateBuffer(kVertices, sizeof(kVertices)); |
| 30 } |
| 31 |
| 32 GlRenderLayer::~GlRenderLayer() { |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 34 glDeleteBuffers(1, &buffer_handle_); |
| 35 glDeleteTextures(1, &texture_handle_); |
| 36 } |
| 37 |
| 38 void GlRenderLayer::SetTexture(const void* texture, int width, int height) { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 CHECK(width > 0 && height > 0); |
| 41 glActiveTexture(GL_TEXTURE0 + texture_id_); |
| 42 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
| 43 |
| 44 if (width != texture_width_ || height != texture_height_) { |
| 45 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, |
| 46 GL_UNSIGNED_BYTE, texture); |
| 47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 49 texture_width_ = width; |
| 50 texture_height_ = height; |
| 51 } else { |
| 52 // Size not changed. Just update the texture. |
| 53 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, |
| 54 GL_UNSIGNED_BYTE, texture); |
| 55 } |
| 56 glBindTexture(GL_TEXTURE_2D, 0); |
| 57 } |
| 58 |
| 59 void GlRenderLayer::SetVertexPositions(const float positions[8]) { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); |
| 62 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(kVertices) / 2, positions); |
| 63 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 64 } |
| 65 |
| 66 void GlRenderLayer::SetTextureVisibleArea(const float positions[8]) { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); |
| 69 glBufferSubData(GL_ARRAY_BUFFER, sizeof(kVertices) / 2, sizeof(kVertices) / 2, |
| 70 positions); |
| 71 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 72 } |
| 73 |
| 74 void GlRenderLayer::Draw() { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 if (texture_width_ == 0 || texture_height_ == 0) { |
| 77 return; |
| 78 } |
| 79 canvas_->DrawTexture(texture_id_, texture_handle_, buffer_handle_); |
| 80 } |
| 81 |
| 82 } // namespace remoting |
OLD | NEW |