blob: e330039a14ae0426d6c98aa9ab1f309548762714 [file] [log] [blame]
yuweih2c0936a2016-07-13 20:30:121// 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
nicholss9949f5b2017-01-05 19:29:175#ifndef REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_
6#define REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_
yuweih2c0936a2016-07-13 20:30:127
yuweihe4c0f782016-08-10 21:41:048#include <array>
9#include <memory>
10
yuweih2c0936a2016-07-13 20:30:1211#include "base/macros.h"
nicholssf62cc8a2017-01-12 16:08:4112#include "base/memory/weak_ptr.h"
yuweih2c0936a2016-07-13 20:30:1213#include "base/threading/thread_checker.h"
nicholss9949f5b2017-01-05 19:29:1714#include "remoting/client/display/sys_opengl.h"
yuweih2c0936a2016-07-13 20:30:1215
16namespace remoting {
nicholssf62cc8a2017-01-12 16:08:4117
18class Canvas;
yuweih2c0936a2016-07-13 20:30:1219
20// This class is for drawing a texture on the canvas. Must be deleted before the
21// canvas is deleted.
22class GlRenderLayer {
23 public:
yuweih2495ff02016-07-21 23:38:0324 static const int kBytesPerPixel = 4;
25
yuweih2c0936a2016-07-13 20:30:1226 // texture_id: An integer in range [0, GL_MAX_TEXTURE_IMAGE_UNITS], defining
27 // which slot to store the texture.
nicholssf62cc8a2017-01-12 16:08:4128 GlRenderLayer(int texture_id, base::WeakPtr<Canvas> canvas);
yuweih2c0936a2016-07-13 20:30:1229 ~GlRenderLayer();
30
31 // Sets the texture (RGBA 8888) to be drawn. Please use UpdateTexture() if the
32 // texture size isn't changed.
yuweih2c0936a2016-07-13 20:30:1233 // stride: byte distance between two rows in |subtexture|.
34 // If |stride| is 0 or |stride| == |width|*kBytesPerPixel, |subtexture| will
35 // be treated as tightly packed.
yuweihcf627b982016-08-09 22:13:2536 void SetTexture(const uint8_t* texture, int width, int height, int stride);
37
38 // Updates a subregion (RGBA 8888) of the texture. Can only be called after
39 // SetTexture() has been called.
40 // stride: See SetTexture().
yuweih2c0936a2016-07-13 20:30:1241 void UpdateTexture(const uint8_t* subtexture,
42 int offset_x,
43 int offset_y,
44 int width,
45 int height,
46 int stride);
47
yuweih8d1912fd2016-08-08 21:26:5148 // Sets the positions of four vertices of the texture in pixel with respect to
49 // the canvas.
yuweih2c0936a2016-07-13 20:30:1250 // positions: [ x_upperleft, y_upperleft, x_lowerleft, y_lowerleft,
51 // x_upperright, y_upperright, x_lowerright, y_lowerright ]
52 void SetVertexPositions(const std::array<float, 8>& positions);
53
yuweih8d1912fd2016-08-08 21:26:5154 // Sets the visible area of the texture in percentage of the width and height
55 // of the texture. The default values are (0, 0), (0, 1), (1, 0), (1, 1),
56 // i.e. showing the whole texture.
yuweih2c0936a2016-07-13 20:30:1257 // positions: [ x_upperleft, y_upperleft, x_lowerleft, y_lowerleft,
58 // x_upperright, y_upperright, x_lowerright, y_lowerright ]
59 void SetTextureVisibleArea(const std::array<float, 8>& positions);
60
61 // Draws the texture on the canvas. Texture must be set before calling Draw().
yuweih2495ff02016-07-21 23:38:0362 void Draw(float alpha_multiplier);
yuweih2c0936a2016-07-13 20:30:1263
yuweihcf627b982016-08-09 22:13:2564 // true if the texture is already set by calling SetTexture().
65 bool is_texture_set() { return texture_set_; }
66
yuweih2c0936a2016-07-13 20:30:1267 private:
yuweihcf627b982016-08-09 22:13:2568 // Returns pointer to the texture buffer that can be used by glTexImage2d or
69 // glTexSubImage2d. The returned value will be |data| if the texture can be
70 // used without manual packing, otherwise the data will be manually packed and
71 // the pointer to |update_buffer_| will be returned.
72 // should_reset_row_length: Pointer to a bool that will be set by the
73 // function. If this is true, the user need to call
74 // glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) after
75 // updating the texture to reset the stride.
76 const uint8_t* PrepareTextureBuffer(const uint8_t* data,
77 int width,
78 int height,
79 int stride,
80 bool* should_reset_row_length);
81
yuweih2c0936a2016-07-13 20:30:1282 int texture_id_;
nicholssf62cc8a2017-01-12 16:08:4183 base::WeakPtr<Canvas> canvas_;
yuweih2c0936a2016-07-13 20:30:1284
85 GLuint texture_handle_;
86 GLuint buffer_handle_;
87
yuweih2c0936a2016-07-13 20:30:1288 bool texture_set_ = false;
89
yuweih8d1912fd2016-08-08 21:26:5190 bool vertex_position_set_ = false;
91
yuweih2c0936a2016-07-13 20:30:1292 // Used in OpenGL ES 2 context which doesn't support GL_UNPACK_ROW_LENGTH to
93 // tightly pack dirty regions before sending them to GPU.
94 std::unique_ptr<uint8_t[]> update_buffer_;
95 int update_buffer_size_ = 0;
96
97 base::ThreadChecker thread_checker_;
98 DISALLOW_COPY_AND_ASSIGN(GlRenderLayer);
99};
100
101} // namespace remoting
nicholss9949f5b2017-01-05 19:29:17102#endif // REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_