yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 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 | |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 5 | #ifndef REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_ |
| 6 | #define REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_ |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 7 | |
yuweih | e4c0f78 | 2016-08-10 21:41:04 | [diff] [blame] | 8 | #include <array> |
| 9 | #include <memory> |
| 10 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 11 | #include "base/macros.h" |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 14 | #include "remoting/client/display/sys_opengl.h" |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 15 | |
| 16 | namespace remoting { |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 17 | |
| 18 | class Canvas; |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 19 | |
| 20 | // This class is for drawing a texture on the canvas. Must be deleted before the |
| 21 | // canvas is deleted. |
| 22 | class GlRenderLayer { |
| 23 | public: |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 24 | static const int kBytesPerPixel = 4; |
| 25 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 26 | // texture_id: An integer in range [0, GL_MAX_TEXTURE_IMAGE_UNITS], defining |
| 27 | // which slot to store the texture. |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 28 | GlRenderLayer(int texture_id, base::WeakPtr<Canvas> canvas); |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 29 | ~GlRenderLayer(); |
| 30 | |
| 31 | // Sets the texture (RGBA 8888) to be drawn. Please use UpdateTexture() if the |
| 32 | // texture size isn't changed. |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 33 | // 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. |
yuweih | cf627b98 | 2016-08-09 22:13:25 | [diff] [blame] | 36 | 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(). |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 41 | void UpdateTexture(const uint8_t* subtexture, |
| 42 | int offset_x, |
| 43 | int offset_y, |
| 44 | int width, |
| 45 | int height, |
| 46 | int stride); |
| 47 | |
yuweih | 8d1912fd | 2016-08-08 21:26:51 | [diff] [blame] | 48 | // Sets the positions of four vertices of the texture in pixel with respect to |
| 49 | // the canvas. |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 50 | // 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 | |
yuweih | 8d1912fd | 2016-08-08 21:26:51 | [diff] [blame] | 54 | // 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. |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 57 | // 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(). |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 62 | void Draw(float alpha_multiplier); |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 63 | |
yuweih | cf627b98 | 2016-08-09 22:13:25 | [diff] [blame] | 64 | // true if the texture is already set by calling SetTexture(). |
| 65 | bool is_texture_set() { return texture_set_; } |
| 66 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 67 | private: |
yuweih | cf627b98 | 2016-08-09 22:13:25 | [diff] [blame] | 68 | // 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 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 82 | int texture_id_; |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 83 | base::WeakPtr<Canvas> canvas_; |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 84 | |
| 85 | GLuint texture_handle_; |
| 86 | GLuint buffer_handle_; |
| 87 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 88 | bool texture_set_ = false; |
| 89 | |
yuweih | 8d1912fd | 2016-08-08 21:26:51 | [diff] [blame] | 90 | bool vertex_position_set_ = false; |
| 91 | |
yuweih | 2c0936a | 2016-07-13 20:30:12 | [diff] [blame] | 92 | // 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 |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 102 | #endif // REMOTING_CLIENT_DISPLAY_GL_RENDER_LAYER_H_ |