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 | |
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 const int kBytesPerPixel = 4; | |
24 | |
25 const int kDefaultUpdateBufferCapacity = 2048 * 2048 * 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 void* 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 GlRenderLayer::UpdateTexture(const void* subtexture, | |
Sergey Ulanov
2016/07/09 01:01:20
Use uint8_t* for the buffer?
Yuwei
2016/07/11 22:38:24
Done.
| |
59 int offset_x, | |
60 int offset_y, | |
61 int width, | |
62 int height, | |
63 int pixel_stride) { | |
64 DCHECK(thread_checker_.CalledOnValidThread()); | |
65 CHECK(texture_set_); | |
Sergey Ulanov
2016/07/09 01:01:20
Do you really need CHECK() instead of DCHECK()?
Yuwei
2016/07/11 22:38:24
Not sure when CHECK() will be useful...
Sergey Ulanov
2016/07/13 04:42:11
I CHECK is useful in two cases:
- when violation
Yuwei
2016/07/13 18:14:16
Acknowledged.
| |
66 CHECK(width > 0 && height > 0); | |
Sergey Ulanov
2016/07/09 01:01:20
DCHECK?
Yuwei
2016/07/11 22:38:24
Done.
| |
67 glActiveTexture(GL_TEXTURE0 + texture_id_); | |
68 glBindTexture(GL_TEXTURE_2D, texture_handle_); | |
69 | |
70 bool loosely_packed = pixel_stride > 0 && pixel_stride != width; | |
71 | |
72 const void* buffer_to_update = subtexture; | |
73 | |
74 if (loosely_packed) { | |
75 if (canvas_->GetGlVersion() >= 3) { | |
76 glPixelStorei(GL_UNPACK_ROW_LENGTH, pixel_stride); | |
77 } else { | |
78 // Doesn't support GL_UNPACK_ROW_LENGTH. Manually packs the data. | |
79 int required_size = width * height * kBytesPerPixel; | |
80 if (update_buffer_size_ < required_size) { | |
81 if (required_size < kDefaultUpdateBufferCapacity) { | |
82 update_buffer_size_ = kDefaultUpdateBufferCapacity; | |
83 } else { | |
84 update_buffer_size_ = required_size; | |
85 } | |
86 update_buffer_.reset(new uint8_t[update_buffer_size_]); | |
87 } | |
88 PackDirtyRegion(update_buffer_.get(), subtexture, width, height, | |
Sergey Ulanov
2016/07/09 01:01:20
This can be avoided when stride == pixel_size*widt
Yuwei
2016/07/09 01:21:24
Isn't it covered by line 70?
Sergey Ulanov
2016/07/13 04:42:11
yes, sorry, I missed it.
| |
89 pixel_stride); | |
90 buffer_to_update = update_buffer_.get(); | |
91 } | |
92 } | |
93 | |
94 glTexSubImage2D(GL_TEXTURE_2D, 0, offset_x, offset_y, width, height, GL_RGBA, | |
95 GL_UNSIGNED_BYTE, buffer_to_update); | |
96 | |
97 if (loosely_packed && canvas_->GetGlVersion() >= 3) { | |
98 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); | |
99 } | |
100 glBindTexture(GL_TEXTURE_2D, 0); | |
101 } | |
102 | |
103 void GlRenderLayer::SetVertexPositions(const float positions[8]) { | |
104 DCHECK(thread_checker_.CalledOnValidThread()); | |
105 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); | |
106 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(kVertices) / 2, positions); | |
107 glBindBuffer(GL_ARRAY_BUFFER, 0); | |
108 } | |
109 | |
110 void GlRenderLayer::SetTextureVisibleArea(const float positions[8]) { | |
111 DCHECK(thread_checker_.CalledOnValidThread()); | |
112 glBindBuffer(GL_ARRAY_BUFFER, buffer_handle_); | |
113 glBufferSubData(GL_ARRAY_BUFFER, sizeof(kVertices) / 2, sizeof(kVertices) / 2, | |
114 positions); | |
115 glBindBuffer(GL_ARRAY_BUFFER, 0); | |
116 } | |
117 | |
118 void GlRenderLayer::Draw() { | |
119 DCHECK(thread_checker_.CalledOnValidThread()); | |
120 CHECK(texture_set_); | |
121 canvas_->DrawTexture(texture_id_, texture_handle_, buffer_handle_); | |
122 } | |
123 | |
124 | |
Sergey Ulanov
2016/07/09 01:01:20
remove extra empty line
Yuwei
2016/07/11 22:38:24
Obsolete.
| |
125 // static | |
126 void GlRenderLayer::PackDirtyRegion(void* dest, | |
Sergey Ulanov
2016/07/09 01:01:20
Doesn't need to be class member.
Yuwei
2016/07/11 22:38:24
Done. Made it inline function.
| |
127 const void* source, | |
128 int width, | |
129 int height, | |
130 int stride) { | |
131 for (int i = 0; i < height; i++) { | |
132 const uint8_t* current_source = | |
133 reinterpret_cast<const uint8_t*>(source) + kBytesPerPixel * stride * i; | |
Sergey Ulanov
2016/07/09 01:01:20
You wouldn't need this cast with uint8_t* used for
Yuwei
2016/07/11 22:38:24
Done.
| |
134 uint8_t* current_dest = | |
135 reinterpret_cast<uint8_t*>(dest) + kBytesPerPixel * width * i; | |
Sergey Ulanov
2016/07/09 01:01:20
Instead of using multiplication to calculate posit
Yuwei
2016/07/11 22:38:24
Done.
| |
136 memcpy(current_dest, current_source, width * kBytesPerPixel); | |
137 } | |
138 } | |
139 | |
140 } // namespace remoting | |
OLD | NEW |