Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: ui/gl/async_pixel_transfer_delegate_android.cc

Issue 11428140: gpu: Add async pixel transfer interface, stub and tests. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added memory dup/mmap Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "ui/gl/async_pixel_transfer_delegate_android.h"
6
7 #include "base/bind.h"
8 #include "base/cancelable_callback.h"
9 #include "base/debug/trace_event.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/process_util.h"
14 #include "base/shared_memory.h"
15 #include "base/threading/thread.h"
16 #include "build/build_config.h"
17 #include "third_party/angle/include/EGL/egl.h"
18 #include "third_party/angle/include/EGL/eglext.h"
19 #include "ui/gfx/point.h"
20 #include "ui/gfx/size.h"
21 #include "ui/gl/async_pixel_transfer_delegate.h"
22 #include "ui/gl/egl_util.h"
23 #include "ui/gl/gl_bindings.h"
24 #include "ui/gl/gl_context.h"
25 #include "ui/gl/gl_surface_egl.h"
26 #include <sys/resource.h>
27
28 using base::SharedMemory;
29 using base::SharedMemoryHandle;
30
31 namespace gfx {
32
33 namespace {
34
35 bool CheckErrors(const char* file, int line) {
36 EGLint eglerror;
37 GLenum glerror;
38 bool success = true;
39 while ((eglerror = eglGetError()) != EGL_SUCCESS) {
40 LOG(ERROR) << "Async transfer eglerror at "
41 << file << ":" << line << " " << eglerror;
42 success = false;
43 }
44 while ((glerror = glGetError()) != GL_NO_ERROR) {
45 LOG(ERROR) << "Async transfer openglerror at "
46 << file << ":" << line << " " << glerror;
47 success = false;
48 }
49 return success;
50 }
51 #define CHK() CheckErrors(__FILE__, __LINE__)
52
53 // We duplicate shared memory to avoid use-after-free issues. This could also
54 // be solved by ref-counting something, or with a destruction callback. There
55 // wasn't an obvious hook or ref-counted container, so for now we dup/mmap.
56 SharedMemory* DuplicateSharedMemory(SharedMemory* shared_memory, uint32 size) {
57 // Duplicate the handle.
58 SharedMemoryHandle duped_shared_memory_handle;
59 if (!shared_memory->ShareToProcess(
60 base::GetCurrentProcessHandle(),
61 &duped_shared_memory_handle))
62 return NULL;
63 scoped_ptr<SharedMemory> duped_shared_memory(
64 new SharedMemory(duped_shared_memory_handle, false));
65 // Map the shared memory into this process. This validates the size.
66 if (!duped_shared_memory->Map(size))
67 return NULL;
68 return duped_shared_memory.release();
69 }
70
71 // Gets the address of the data from shared memory.
72 void* GetAddress(SharedMemory* shared_memory,
73 uint32 shm_size,
74 uint32 shm_data_offset,
75 uint32 shm_data_size) {
76 // Memory bounds have already been validated, so there
77 // is just DCHECKS here.
78 DCHECK(shared_memory);
79 DCHECK(shared_memory->memory());
80 DCHECK(shm_data_offset + shm_data_size <= shm_size);
greggman 2012/12/06 06:52:00 DCHECK_LE?
epenner 2012/12/08 03:15:04 Done.
81 return static_cast<int8*>(shared_memory->memory()) + shm_data_offset;
82 }
83
84 } // namespace
85
86
87 // Class which holds async pixel transfers state (EGLImage).
88 // The EGLImage is accessed by either thread, but the everything
89 // else (and the lifetime of this object) is controlled only by the
90 // main GPU thread.
91 class AsyncTransferStateAndroid : public AsyncPixelTransferState {
92 public:
93 AsyncTransferStateAndroid(GLuint texture_id)
94 : texture_id_(texture_id),
95 needs_bind_(false),
96 transfer_in_progress_(false),
97 egl_image_(0) {}
98
99 virtual ~AsyncTransferStateAndroid() {
100 if (egl_image_) {
greggman 2012/12/06 06:52:00 nit: As al pointed out. there's 3 spaces of indent
epenner 2012/12/08 03:15:04 Done.
101 EGLDisplay display = eglGetCurrentDisplay();
102 eglDestroyImageKHR(display, egl_image_);
103 }
104 }
105
106 // implement AsyncPixelTransferState:
107 virtual bool TransferIsInProgress() {
108 return transfer_in_progress_;
109 }
110
111 virtual void BindTexture(GLenum target) {
112 glBindTexture(target, texture_id_);
113 if (needs_bind_)
114 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image_);
115 needs_bind_ = false;
116 }
117
118 // Completion callbacks.
119 void TexImage2DCompleted() {
120 needs_bind_ = true;
121 transfer_in_progress_ = false;
122 }
123 void TexSubImage2DCompleted() {
124 transfer_in_progress_ = false;
125 }
126
127 // The 'real' texture.
128 GLuint texture_id_;
129
130 // Indicates there is a new EGLImage and the 'real'
131 // texture needs to be bound to it as an EGLImage target.
132 bool needs_bind_;
133
134 // Indicates that an async transfer is in progress.
135 bool transfer_in_progress_;
136
137 // It would be nice if we could just create a new EGLImage for
138 // every upload, but I found that didn't work, so this stores
139 // one for the lifetime of the texture.
140 EGLImageKHR egl_image_;
141 };
142
143
144 // Class which handles async pixel transfers on Android (using
145 // EGLImageKHR and another upload thread)
146 class AsyncPixelTransferDelegateAndroid : public AsyncPixelTransferDelegate {
147 public:
148 AsyncPixelTransferDelegateAndroid();
149 virtual ~AsyncPixelTransferDelegateAndroid();
150
151 // implement AsyncPixelTransferDelegate:
152 virtual scoped_refptr<AsyncPixelTransferState>
153 CreatePixelTransferState(GLuint);
154
155 virtual void AsyncNotifyCompletion(
156 const base::Closure& task);
157
158 virtual void AsyncTexImage2D(
159 AsyncPixelTransferState*,
160 GLenum target,
161 GLint level,
162 GLenum internal_format,
163 GLsizei width,
164 GLsizei height,
165 GLint border,
166 GLenum format,
167 GLenum type,
168 SharedMemory*,
greggman 2012/12/06 06:52:00 should this be const?
epenner 2012/12/08 03:15:04 Done.
169 uint32 shm_size,
170 uint32 shm_data_offset,
171 uint32 shm_data_size);
172
173 virtual void AsyncTexSubImage2D(
174 AsyncPixelTransferState*,
175 GLenum target,
176 GLint level,
177 GLint xoffset,
178 GLint yoffset,
179 GLsizei width,
180 GLsizei height,
181 GLenum format,
182 GLenum type,
183 SharedMemory*,
greggman 2012/12/06 06:52:00 should this be const?
epenner 2012/12/08 03:15:04 Done.
184 uint32 shm_size,
185 uint32 shm_data_offset,
186 uint32 shm_data_size);
187
188 private:
189 void Initialize();
190 void Shutdown();
191 void PerformInitialize();
192 void PerformShutdown();
193
194 struct MemoryParams {
greggman 2012/12/06 06:52:00 nit: type definition come before function declarat
epenner 2012/12/08 03:15:04 Done.
195 SharedMemory* shared_memory; uint32 shm_size;
196 uint32 shm_data_offset; uint32 shm_data_size;
197 };
198 struct AsyncTexImage2DParams {
199 GLenum target; GLint level; GLenum internal_format;
200 GLsizei width; GLsizei height; GLint border; GLenum format; GLenum type;
201 };
202 struct AsyncTexSubImage2DParams {
203 GLenum target; GLint level; GLint xoffset; GLint yoffset;
204 GLsizei width; GLsizei height; GLenum format; GLenum type;
205 };
206 void PerformAsyncTexImage2D(
207 AsyncTransferStateAndroid*,
208 AsyncTexImage2DParams,
209 MemoryParams);
210 void PerformAsyncTexSubImage2D(
211 AsyncTransferStateAndroid*,
212 AsyncTexSubImage2DParams,
213 MemoryParams);
214
215 scoped_ptr<base::Thread> thread_;
216 scoped_refptr<gfx::GLContext> thread_context_;
217 scoped_refptr<gfx::GLSurface> thread_surface_;
218
219 DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferDelegateAndroid);
220 };
221
222 // Lazy instance creation.
223 base::LazyInstance<AsyncPixelTransferDelegateAndroid>
224 g_async_pixel_transfer_delegate_ = LAZY_INSTANCE_INITIALIZER;
225
226 AsyncPixelTransferDelegate* AsyncPixelTransferDelegate::Get() {
227 return g_async_pixel_transfer_delegate_.Pointer();
228 }
229
230 scoped_refptr<AsyncPixelTransferState>
231 AsyncPixelTransferDelegateAndroid::
232 CreatePixelTransferState(GLuint texture_id) {
233 return make_scoped_refptr(static_cast<AsyncPixelTransferState*>(
234 new AsyncTransferStateAndroid(texture_id)));
235 }
236
237 AsyncPixelTransferDelegateAndroid::AsyncPixelTransferDelegateAndroid()
238 : thread_(new base::Thread("GPUAsyncTransferThread"))
239 {
240 Initialize();
241 }
242
243 AsyncPixelTransferDelegateAndroid::~AsyncPixelTransferDelegateAndroid()
244 {
245 Shutdown();
246 }
247
248 void AsyncPixelTransferDelegateAndroid::Initialize() {
249 // Start the thread and initialize on the thread.
250 thread_->Start();
251 thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
252 &AsyncPixelTransferDelegateAndroid::PerformInitialize,
253 base::Unretained(this)));
254 }
255
256 void AsyncPixelTransferDelegateAndroid::Shutdown() {
257 // Shutdown and wait for the thread to finish.
258 thread_->message_loop()->PostTask(FROM_HERE, base::Bind(
259 &AsyncPixelTransferDelegateAndroid::PerformShutdown,
260 base::Unretained(this)));
261 thread_->Stop();
262 }
263
264
265 namespace {
266 // Dummy function to measure completion on
267 // the upload thread.
268 void NoOp() {}
269 } // namespace
270
271 void AsyncPixelTransferDelegateAndroid::AsyncNotifyCompletion(
272 const base::Closure& task) {
273 // Post a no-op task to the upload thread followed
274 // by a reply to the callback. The reply will then occur after
275 // all async transfers are complete.
276 thread_->message_loop_proxy()->PostTaskAndReply(FROM_HERE,
277 base::Bind(&NoOp), task);
278 }
279
280 void AsyncPixelTransferDelegateAndroid::AsyncTexImage2D(
281 AsyncPixelTransferState* transfer_state,
282 GLenum target,
283 GLint level,
284 GLenum internal_format,
285 GLsizei width,
286 GLsizei height,
287 GLint border,
288 GLenum format,
289 GLenum type,
290 SharedMemory* shared_memory,
291 uint32 shm_size,
292 uint32 shm_data_offset,
293 uint32 shm_data_size) {
294 AsyncTransferStateAndroid* state =
295 static_cast<AsyncTransferStateAndroid*>(transfer_state);
296 DCHECK(state);
297 DCHECK(shared_memory);
298 DCHECK(state->texture_id_);
299 DCHECK(!state->transfer_in_progress_);
300 DCHECK(target == GL_TEXTURE_2D);
301 DCHECK(level == 0);
302
303 state->transfer_in_progress_ = true;
304
305 // Any existing EGLImage is made an 'orphan' by a call to texImage2D. We can
306 // delete the existing one safely since the client texture is not affected.
307 if (state->egl_image_) {
308 EGLDisplay display = eglGetCurrentDisplay();
309 eglDestroyImageKHR(display, state->egl_image_);
310 state->egl_image_ = 0;
311 }
312
313 AsyncTexImage2DParams tex_params = {target, level, internal_format,
314 width, height, border, format, type};
315 MemoryParams mem_params = {DuplicateSharedMemory(shared_memory, shm_size),
316 shm_size, shm_data_offset, shm_data_size};
317
318 thread_->message_loop_proxy()->PostTaskAndReply(FROM_HERE,
319 base::Bind(
320 &AsyncPixelTransferDelegateAndroid::PerformAsyncTexImage2D,
321 base::Unretained(this), // The delegate always outlives its tasks.
322 base::Unretained(state), // This is referenced in reply below.
323 tex_params,
324 mem_params),
325 base::Bind(
326 &AsyncTransferStateAndroid::TexImage2DCompleted,
327 state));
328 }
329
330 void AsyncPixelTransferDelegateAndroid::AsyncTexSubImage2D(
331 AsyncPixelTransferState* transfer_state,
332 GLenum target,
333 GLint level,
334 GLint xoffset,
335 GLint yoffset,
336 GLsizei width,
337 GLsizei height,
338 GLenum format,
339 GLenum type,
340 SharedMemory* shared_memory,
341 uint32 shm_size,
342 uint32 shm_data_offset,
343 uint32 shm_data_size) {
344 TRACE_EVENT2("gpu", "AsyncTexSubImage2D",
345 "width", width,
346 "height", height);
347 AsyncTransferStateAndroid* state =
348 static_cast<AsyncTransferStateAndroid*>(transfer_state);
349 DCHECK(state);
350 DCHECK(shared_memory);
351 DCHECK(state->texture_id_);
352 DCHECK(!state->transfer_in_progress_);
353 DCHECK(target == GL_TEXTURE_2D);
greggman 2012/12/06 06:52:00 DCHECK_EQ? You probably need DCHECK_EQ(static_ca
epenner 2012/12/08 03:15:04 Done.
354 DCHECK(level == 0);
greggman 2012/12/06 06:52:00 DCHECK_EQ? or DCHECK(!level)? The linter used to
epenner 2012/12/08 03:15:04 Done.
355
356 state->transfer_in_progress_ = true;
357
358 // Create the EGLImage if it hasn't already been created.
359 if (!state->egl_image_) {
360 EGLDisplay egl_display = eglGetCurrentDisplay();
361 EGLContext egl_context = eglGetCurrentContext();
362 EGLenum egl_target = EGL_GL_TEXTURE_2D_KHR;
363 EGLClientBuffer egl_buffer =
364 reinterpret_cast<EGLClientBuffer>(state->texture_id_);
365 EGLint egl_attrib_list[] = {
366 EGL_GL_TEXTURE_LEVEL_KHR, level, // mip-map level to reference.
367 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, // preserve the data in the texture.
368 EGL_NONE
369 };
370 state->egl_image_ = eglCreateImageKHR(
371 egl_display,
372 egl_context,
373 egl_target,
374 egl_buffer,
375 egl_attrib_list);
376 }
377
378 AsyncTexSubImage2DParams tex_params = {target, level, xoffset, yoffset,
379 width, height, format, type};
greggman 2012/12/06 06:52:00 nit: indentation is off
epenner 2012/12/08 03:15:04 Done.
380 MemoryParams mem_params = {DuplicateSharedMemory(shared_memory, shm_size),
381 shm_size, shm_data_offset, shm_data_size};
382
383 thread_->message_loop_proxy()->PostTaskAndReply(FROM_HERE,
384 base::Bind(
385 &AsyncPixelTransferDelegateAndroid::PerformAsyncTexSubImage2D,
386 base::Unretained(this), // The delegate always outlives its tasks.
387 base::Unretained(state), // This is referenced in reply below.
388 tex_params,
389 mem_params),
390 base::Bind(
391 &AsyncTransferStateAndroid::TexSubImage2DCompleted,
392 state));
393 }
394
395 void AsyncPixelTransferDelegateAndroid::PerformInitialize() {
396 DCHECK(!thread_surface_);
397 DCHECK(!thread_context_);
398
399 // Lower the thread priority for uploads.
400 // TODO: What's a good value? Without lowering this, uploads
401 // would often execute immediately and block the GPU thread.
402 setpriority(PRIO_PROCESS, base::PlatformThread::CurrentId(), 15);
403
404 GLShareGroup* share_group = NULL;
405 bool software = false;
406 thread_surface_ = new gfx::PbufferGLSurfaceEGL(software, gfx::Size(1,1));
407 thread_surface_->Initialize();
408 thread_context_ = gfx::GLContext::CreateGLContext(share_group,
409 thread_surface_,
410 gfx::PreferDiscreteGpu);
411 bool is_current = thread_context_->MakeCurrent(thread_surface_);
412
413 DCHECK(thread_surface_);
414 DCHECK(thread_context_);
415 DCHECK(is_current);
416 }
417
418 void AsyncPixelTransferDelegateAndroid::PerformShutdown() {
419 DCHECK(thread_surface_);
420 DCHECK(thread_context_);
421 thread_surface_ = NULL;
422 thread_context_->ReleaseCurrent(thread_surface_);
423 thread_context_ = NULL;
424 }
425
426 namespace {
427 void WaitForGlFence() {
428 // TODO: Fix bindings (link errors) to enable the code below.
429 // TODO: Should we only sync just before we report completion?
430
431 // Uploads usually finish on the CPU, but just in case add a fence
432 // and guarantee the upload has completed. Flush bit is set to
433 // insure we don't wait forever.
434 // EGLSyncKHR fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
greggman 2012/12/06 11:22:20 btw: If you want these functions I think you need
epenner 2012/12/08 03:15:04 Done.
435 // EGLint flags = EGL_SYNC_FLUSH_COMMANDS_BIT_KHR;
436 // EGLTimeKHR time = EGL_FOREVER_KHR;
437 // eglClientWaitSyncKHR(display, fence, flags, time);
438 glFinish();
439 }
440 } // namespace
441
442 void AsyncPixelTransferDelegateAndroid::PerformAsyncTexImage2D(
443 AsyncTransferStateAndroid* state,
444 AsyncTexImage2DParams t,
greggman 2012/12/06 06:52:00 nit: except for i for index single letter names ar
epenner 2012/12/08 03:15:04 I'll change if you want but I was following the de
445 MemoryParams m) {
446 // This is just to insure it is deleted. Could bind() do this?
447 scoped_ptr<SharedMemory> shared_memory = make_scoped_ptr(m.shared_memory);
448 void* data = GetAddress(m.shared_memory, m.shm_size,
449 m.shm_data_offset, m.shm_data_size);
450
451 // In texImage2D, we do everything on the upload thread.
452 // This is because texImage2D can incur the allocation cost, and
453 // it also 'orphans' any previous EGLImage bound to the texture.
454 DCHECK(0 == t.level);
greggman 2012/12/06 06:52:00 DCHECK_EQ?
epenner 2012/12/08 03:15:04 Done.
455 DCHECK(EGL_NO_IMAGE_KHR == state->egl_image_);
greggman 2012/12/06 06:52:00 DCHECK_EQ?
epenner 2012/12/08 03:15:04 Done.
456 TRACE_EVENT2("gpu", "performAsyncTexImage2D",
457 "width", t.width,
458 "height", t.height);
459
460 // Create a texture from the image and upload to it.
461 GLuint temp_texture = 0;
462 glGenTextures(1, &temp_texture);
463 glActiveTexture(GL_TEXTURE0);
464 glBindTexture(GL_TEXTURE_2D, temp_texture);
465 {
466 TRACE_EVENT0("gpu", "performAsyncTexSubImage2D glTexImage2D");
467 glTexImage2D(GL_TEXTURE_2D,
468 t.level, t.internal_format,
469 t.width, t.height,
470 t.border, t.format, t.type, data);
471 }
472
473 // Create the EGLImage, as texSubImage always 'orphan's a previous EGLImage.
474 EGLDisplay egl_display = eglGetCurrentDisplay();
475 EGLContext egl_context = eglGetCurrentContext();
476 EGLenum egl_target = EGL_GL_TEXTURE_2D_KHR;
477 EGLClientBuffer egl_buffer = (EGLClientBuffer) temp_texture;
478 EGLint egl_attrib_list[] = {
479 EGL_GL_TEXTURE_LEVEL_KHR, t.level, // mip-map level to reference.
480 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, // preserve the data in the texture.
481 EGL_NONE
482 };
483 state->egl_image_ = eglCreateImageKHR(
484 egl_display,
485 egl_context,
486 egl_target,
487 egl_buffer,
488 egl_attrib_list);
489 WaitForGlFence();
490
491 // We can delete this thread's texture as the real texture
492 // now contains the data.
493 glDeleteTextures(1, &temp_texture);
494 }
495
496 void AsyncPixelTransferDelegateAndroid::PerformAsyncTexSubImage2D(
497 AsyncTransferStateAndroid* state,
498 AsyncTexSubImage2DParams t,
499 MemoryParams m) {
500 // This is just to insure it is deleted. Could bind() do this?
501 scoped_ptr<SharedMemory> shared_memory = make_scoped_ptr(m.shared_memory);
502 void* data = GetAddress(m.shared_memory, m.shm_size,
503 m.shm_data_offset, m.shm_data_size);
504
505 // For a texSubImage, the texture must already have been
506 // created on the main thread, along with EGLImageKHR.
507 DCHECK(EGL_NO_IMAGE_KHR != state->egl_image_);
greggman 2012/12/06 06:52:00 DCHECK_EQ?
epenner 2012/12/08 03:15:04 Done.
508 DCHECK(0 == t.level);
greggman 2012/12/06 06:52:00 same
epenner 2012/12/08 03:15:04 Done.
509 TRACE_EVENT2("gpu", "performAsyncTexSubImage2D",
510 "width", t.width,
511 "height", t.height);
512
513 // Create a texture from the image and upload to it.
514 GLuint temp_texture = 0;
515 glGenTextures(1, &temp_texture);
516 glActiveTexture(GL_TEXTURE0);
517 glBindTexture(GL_TEXTURE_2D, temp_texture);
518 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, state->egl_image_);
519 {
520 TRACE_EVENT0("gpu", "performAsyncTexSubImage2D glTexSubImage2D");
521 glTexSubImage2D(GL_TEXTURE_2D,
522 t.level, t.xoffset, t.yoffset,
523 t.width, t.height, t.format, t.type, data);
524 }
525 WaitForGlFence();
526
527 // We can delete this thread's texture as the real texture
528 // now contains the data.
529 glDeleteTextures(1, &temp_texture);
530 }
531
532 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698