1 // Copyright 2014 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.
5 #include "gpu/command_buffer/service/texture_definition.h"
9 #include "base/lazy_instance.h"
10 #include "base/memory/linked_ptr.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/thread_local.h"
14 #include "gpu/command_buffer/service/texture_manager.h"
15 #include "ui/gl/gl_image.h"
16 #include "ui/gl/gl_implementation.h"
17 #include "ui/gl/scoped_binders.h"
19 #if !defined(OS_MACOSX)
20 #include "ui/gl/gl_surface_egl.h"
28 class GLImageSync
: public gfx::GLImage
{
30 explicit GLImageSync(const scoped_refptr
<NativeImageBuffer
>& buffer
,
31 const gfx::Size
& size
);
34 void Destroy(bool have_context
) override
;
35 gfx::Size
GetSize() override
;
36 unsigned GetInternalFormat() override
;
37 bool BindTexImage(unsigned target
) override
;
38 void ReleaseTexImage(unsigned target
) override
;
39 bool CopyTexSubImage(unsigned target
,
40 const gfx::Point
& offset
,
41 const gfx::Rect
& rect
) override
;
42 void WillUseTexImage() override
;
43 void WillModifyTexImage() override
;
44 void DidModifyTexImage() override
;
45 void DidUseTexImage() override
;
46 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
48 gfx::OverlayTransform transform
,
49 const gfx::Rect
& bounds_rect
,
50 const gfx::RectF
& crop_rect
) override
;
53 ~GLImageSync() override
;
56 scoped_refptr
<NativeImageBuffer
> buffer_
;
59 DISALLOW_COPY_AND_ASSIGN(GLImageSync
);
62 GLImageSync::GLImageSync(const scoped_refptr
<NativeImageBuffer
>& buffer
,
63 const gfx::Size
& size
)
64 : buffer_(buffer
), size_(size
) {
66 buffer
->AddClient(this);
69 GLImageSync::~GLImageSync() {
71 buffer_
->RemoveClient(this);
74 void GLImageSync::Destroy(bool have_context
) {
77 gfx::Size
GLImageSync::GetSize() {
81 unsigned GLImageSync::GetInternalFormat() {
85 bool GLImageSync::BindTexImage(unsigned target
) {
90 void GLImageSync::ReleaseTexImage(unsigned target
) {
94 bool GLImageSync::CopyTexSubImage(unsigned target
,
95 const gfx::Point
& offset
,
96 const gfx::Rect
& rect
) {
100 void GLImageSync::WillUseTexImage() {
103 void GLImageSync::DidUseTexImage() {
106 void GLImageSync::WillModifyTexImage() {
109 void GLImageSync::DidModifyTexImage() {
112 bool GLImageSync::ScheduleOverlayPlane(gfx::AcceleratedWidget widget
,
114 gfx::OverlayTransform transform
,
115 const gfx::Rect
& bounds_rect
,
116 const gfx::RectF
& crop_rect
) {
121 #if !defined(OS_MACOSX)
122 class NativeImageBufferEGL
: public NativeImageBuffer
{
124 static scoped_refptr
<NativeImageBufferEGL
> Create(GLuint texture_id
);
127 NativeImageBufferEGL(EGLDisplay display
, EGLImageKHR image
);
128 ~NativeImageBufferEGL() override
;
129 void AddClient(gfx::GLImage
* client
) override
;
130 void RemoveClient(gfx::GLImage
* client
) override
;
131 bool IsClient(gfx::GLImage
* client
) override
;
132 void BindToTexture(GLenum target
) const override
;
134 const EGLDisplay egl_display_
;
135 const EGLImageKHR egl_image_
;
140 explicit ClientInfo(gfx::GLImage
* client
);
143 gfx::GLImage
* client
;
144 bool needs_wait_before_read
;
146 std::list
<ClientInfo
> client_infos_
;
147 gfx::GLImage
* write_client_
;
149 DISALLOW_COPY_AND_ASSIGN(NativeImageBufferEGL
);
152 scoped_refptr
<NativeImageBufferEGL
> NativeImageBufferEGL::Create(
154 EGLDisplay egl_display
= gfx::GLSurfaceEGL::GetHardwareDisplay();
155 EGLContext egl_context
= eglGetCurrentContext();
157 DCHECK_NE(EGL_NO_CONTEXT
, egl_context
);
158 DCHECK_NE(EGL_NO_DISPLAY
, egl_display
);
159 DCHECK(glIsTexture(texture_id
));
161 DCHECK(gfx::g_driver_egl
.ext
.b_EGL_KHR_image_base
&&
162 gfx::g_driver_egl
.ext
.b_EGL_KHR_gl_texture_2D_image
&&
163 gfx::g_driver_gl
.ext
.b_GL_OES_EGL_image
);
165 const EGLint egl_attrib_list
[] = {
166 EGL_GL_TEXTURE_LEVEL_KHR
, 0, EGL_IMAGE_PRESERVED_KHR
, EGL_TRUE
, EGL_NONE
};
167 EGLClientBuffer egl_buffer
= reinterpret_cast<EGLClientBuffer
>(texture_id
);
168 EGLenum egl_target
= EGL_GL_TEXTURE_2D_KHR
;
170 EGLImageKHR egl_image
= eglCreateImageKHR(
171 egl_display
, egl_context
, egl_target
, egl_buffer
, egl_attrib_list
);
173 if (egl_image
== EGL_NO_IMAGE_KHR
) {
174 LOG(ERROR
) << "eglCreateImageKHR for cross-thread sharing failed: 0x"
175 << std::hex
<< eglGetError();
179 return new NativeImageBufferEGL(egl_display
, egl_image
);
182 NativeImageBufferEGL::ClientInfo::ClientInfo(gfx::GLImage
* client
)
183 : client(client
), needs_wait_before_read(true) {}
185 NativeImageBufferEGL::ClientInfo::~ClientInfo() {}
187 NativeImageBufferEGL::NativeImageBufferEGL(EGLDisplay display
,
189 : NativeImageBuffer(),
190 egl_display_(display
),
192 write_client_(NULL
) {
193 DCHECK(egl_display_
!= EGL_NO_DISPLAY
);
194 DCHECK(egl_image_
!= EGL_NO_IMAGE_KHR
);
197 NativeImageBufferEGL::~NativeImageBufferEGL() {
198 DCHECK(client_infos_
.empty());
199 if (egl_image_
!= EGL_NO_IMAGE_KHR
)
200 eglDestroyImageKHR(egl_display_
, egl_image_
);
203 void NativeImageBufferEGL::AddClient(gfx::GLImage
* client
) {
204 base::AutoLock
lock(lock_
);
205 client_infos_
.push_back(ClientInfo(client
));
208 void NativeImageBufferEGL::RemoveClient(gfx::GLImage
* client
) {
209 base::AutoLock
lock(lock_
);
210 if (write_client_
== client
)
211 write_client_
= NULL
;
212 for (std::list
<ClientInfo
>::iterator it
= client_infos_
.begin();
213 it
!= client_infos_
.end();
215 if (it
->client
== client
) {
216 client_infos_
.erase(it
);
223 bool NativeImageBufferEGL::IsClient(gfx::GLImage
* client
) {
224 base::AutoLock
lock(lock_
);
225 for (std::list
<ClientInfo
>::iterator it
= client_infos_
.begin();
226 it
!= client_infos_
.end();
228 if (it
->client
== client
)
234 void NativeImageBufferEGL::BindToTexture(GLenum target
) const {
235 DCHECK(egl_image_
!= EGL_NO_IMAGE_KHR
);
236 glEGLImageTargetTexture2DOES(target
, egl_image_
);
237 DCHECK_EQ(static_cast<EGLint
>(EGL_SUCCESS
), eglGetError());
238 DCHECK_EQ(static_cast<GLenum
>(GL_NO_ERROR
), glGetError());
243 class NativeImageBufferStub
: public NativeImageBuffer
{
245 NativeImageBufferStub() : NativeImageBuffer() {}
248 ~NativeImageBufferStub() override
{}
249 void AddClient(gfx::GLImage
* client
) override
{}
250 void RemoveClient(gfx::GLImage
* client
) override
{}
251 bool IsClient(gfx::GLImage
* client
) override
{ return true; }
252 void BindToTexture(GLenum target
) const override
{}
254 DISALLOW_COPY_AND_ASSIGN(NativeImageBufferStub
);
257 bool g_avoid_egl_target_texture_reuse
= false;
260 base::LazyInstance
<base::ThreadLocalBoolean
> g_inside_scoped_update_texture
;
263 } // anonymous namespace
266 scoped_refptr
<NativeImageBuffer
> NativeImageBuffer::Create(GLuint texture_id
) {
267 switch (gfx::GetGLImplementation()) {
268 #if !defined(OS_MACOSX)
269 case gfx::kGLImplementationEGLGLES2
:
270 return NativeImageBufferEGL::Create(texture_id
);
272 case gfx::kGLImplementationMockGL
:
273 return new NativeImageBufferStub
;
281 void TextureDefinition::AvoidEGLTargetTextureReuse() {
282 g_avoid_egl_target_texture_reuse
= true;
285 ScopedUpdateTexture::ScopedUpdateTexture() {
287 DCHECK(!g_inside_scoped_update_texture
.Get().Get());
288 g_inside_scoped_update_texture
.Get().Set(true);
292 ScopedUpdateTexture::~ScopedUpdateTexture() {
294 DCHECK(g_inside_scoped_update_texture
.Get().Get());
295 g_inside_scoped_update_texture
.Get().Set(false);
297 // We have to make sure the changes are visible to other clients in this share
298 // group. As far as the clients are concerned, the mailbox semantics only
299 // demand a single flush from the client after changes are first made,
300 // and it is not visible to them when another share group boundary is crossed.
301 // We could probably track this and be a bit smarter about when to flush
306 TextureDefinition::LevelInfo::LevelInfo()
318 TextureDefinition::LevelInfo::LevelInfo(GLenum target
,
319 GLenum internal_format
,
328 internal_format(internal_format
),
337 TextureDefinition::LevelInfo::~LevelInfo() {}
339 TextureDefinition::TextureDefinition()
350 TextureDefinition::TextureDefinition(
352 unsigned int version
,
353 const scoped_refptr
<NativeImageBuffer
>& image_buffer
)
355 target_(texture
->target()),
356 image_buffer_(image_buffer
),
357 min_filter_(texture
->min_filter()),
358 mag_filter_(texture
->mag_filter()),
359 wrap_s_(texture
->wrap_s()),
360 wrap_t_(texture
->wrap_t()),
361 usage_(texture
->usage()),
362 immutable_(texture
->IsImmutable()),
363 defined_(texture
->IsDefined()) {
364 DCHECK_IMPLIES(image_buffer_
.get(), defined_
);
365 if (!image_buffer_
.get() && defined_
) {
366 image_buffer_
= NativeImageBuffer::Create(texture
->service_id());
367 DCHECK(image_buffer_
.get());
370 const Texture::FaceInfo
& first_face
= texture
->face_infos_
[0];
371 if (image_buffer_
.get()) {
372 scoped_refptr
<gfx::GLImage
> gl_image(
373 new GLImageSync(image_buffer_
,
374 gfx::Size(first_face
.level_infos
[0].width
,
375 first_face
.level_infos
[0].height
)));
376 texture
->SetLevelImage(NULL
, target_
, 0, gl_image
.get());
379 const Texture::LevelInfo
& level
= first_face
.level_infos
[0];
380 level_info_
= LevelInfo(level
.target
, level
.internal_format
, level
.width
,
381 level
.height
, level
.depth
, level
.border
, level
.format
,
382 level
.type
, level
.cleared
);
385 TextureDefinition::~TextureDefinition() {
388 Texture
* TextureDefinition::CreateTexture() const {
390 glGenTextures(1, &texture_id
);
392 Texture
* texture(new Texture(texture_id
));
393 ScopedUpdateTexture scoped_update_texture
;
394 UpdateTextureInternal(texture
);
399 void TextureDefinition::UpdateTextureInternal(Texture
* texture
) const {
401 DCHECK(g_inside_scoped_update_texture
.Get().Get());
403 gfx::ScopedTextureBinder
texture_binder(target_
, texture
->service_id());
404 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, min_filter_
);
405 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mag_filter_
);
406 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrap_s_
);
407 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrap_t_
);
408 if (image_buffer_
.get())
409 image_buffer_
->BindToTexture(target_
);
412 texture
->face_infos_
.resize(1);
413 texture
->face_infos_
[0].level_infos
.resize(1);
414 texture
->SetLevelInfo(NULL
, level_info_
.target
, 0,
415 level_info_
.internal_format
, level_info_
.width
,
416 level_info_
.height
, level_info_
.depth
,
417 level_info_
.border
, level_info_
.format
,
418 level_info_
.type
, level_info_
.cleared
);
421 if (image_buffer_
.get()) {
422 texture
->SetLevelImage(
428 gfx::Size(level_info_
.width
, level_info_
.height
)));
431 texture
->target_
= target_
;
432 texture
->SetImmutable(immutable_
);
433 texture
->min_filter_
= min_filter_
;
434 texture
->mag_filter_
= mag_filter_
;
435 texture
->wrap_s_
= wrap_s_
;
436 texture
->wrap_t_
= wrap_t_
;
437 texture
->usage_
= usage_
;
440 void TextureDefinition::UpdateTexture(Texture
* texture
) const {
441 GLuint old_service_id
= 0u;
442 if (image_buffer_
.get() && g_avoid_egl_target_texture_reuse
) {
443 GLuint service_id
= 0u;
444 glGenTextures(1, &service_id
);
445 old_service_id
= texture
->service_id();
446 texture
->SetServiceId(service_id
);
448 DCHECK_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), target_
);
450 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &bound_id
);
451 if (bound_id
== static_cast<GLint
>(old_service_id
)) {
452 glBindTexture(target_
, service_id
);
456 UpdateTextureInternal(texture
);
458 if (old_service_id
) {
459 glDeleteTextures(1, &old_service_id
);
463 bool TextureDefinition::Matches(const Texture
* texture
) const {
464 DCHECK(target_
== texture
->target());
465 if (texture
->min_filter_
!= min_filter_
||
466 texture
->mag_filter_
!= mag_filter_
||
467 texture
->wrap_s_
!= wrap_s_
||
468 texture
->wrap_t_
!= wrap_t_
||
469 texture
->SafeToRenderFrom() != SafeToRenderFrom()) {
473 // Texture became defined.
474 if (!image_buffer_
.get() && texture
->IsDefined())
477 // All structural changes should have orphaned the texture.
478 if (image_buffer_
.get() && !texture
->GetLevelImage(texture
->target(), 0))
484 bool TextureDefinition::SafeToRenderFrom() const {
485 return level_info_
.cleared
;