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/memory/linked_ptr.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "gpu/command_buffer/service/texture_manager.h"
13 #include "ui/gl/gl_image.h"
14 #include "ui/gl/gl_implementation.h"
15 #include "ui/gl/scoped_binders.h"
17 #if !defined(OS_MACOSX)
18 #include "ui/gl/gl_surface_egl.h"
26 class GLImageSync : public gfx::GLImage {
28 explicit GLImageSync(const scoped_refptr<NativeImageBuffer>& buffer,
29 const gfx::Size& size);
32 virtual void Destroy(bool have_context) OVERRIDE;
33 virtual gfx::Size GetSize() OVERRIDE;
34 virtual bool BindTexImage(unsigned target) OVERRIDE;
35 virtual void ReleaseTexImage(unsigned target) OVERRIDE;
36 virtual bool CopyTexImage(unsigned target) OVERRIDE;
37 virtual void WillUseTexImage() OVERRIDE;
38 virtual void WillModifyTexImage() OVERRIDE;
39 virtual void DidModifyTexImage() OVERRIDE;
40 virtual void DidUseTexImage() OVERRIDE;
41 virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
43 gfx::OverlayTransform transform,
44 const gfx::Rect& bounds_rect,
45 const gfx::RectF& crop_rect) OVERRIDE;
46 virtual void SetReleaseAfterUse() OVERRIDE;
49 virtual ~GLImageSync();
52 scoped_refptr<NativeImageBuffer> buffer_;
55 DISALLOW_COPY_AND_ASSIGN(GLImageSync);
58 GLImageSync::GLImageSync(const scoped_refptr<NativeImageBuffer>& buffer,
59 const gfx::Size& size)
60 : buffer_(buffer), size_(size) {
62 buffer->AddClient(this);
65 GLImageSync::~GLImageSync() {
67 buffer_->RemoveClient(this);
70 void GLImageSync::Destroy(bool have_context) {
73 gfx::Size GLImageSync::GetSize() {
77 bool GLImageSync::BindTexImage(unsigned target) {
82 void GLImageSync::ReleaseTexImage(unsigned target) {
86 bool GLImageSync::CopyTexImage(unsigned target) {
90 void GLImageSync::WillUseTexImage() {
93 void GLImageSync::DidUseTexImage() {
96 void GLImageSync::WillModifyTexImage() {
99 void GLImageSync::DidModifyTexImage() {
102 bool GLImageSync::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
104 gfx::OverlayTransform transform,
105 const gfx::Rect& bounds_rect,
106 const gfx::RectF& crop_rect) {
111 void GLImageSync::SetReleaseAfterUse() {
115 #if !defined(OS_MACOSX)
116 class NativeImageBufferEGL : public NativeImageBuffer {
118 static scoped_refptr<NativeImageBufferEGL> Create(GLuint texture_id);
121 NativeImageBufferEGL(EGLDisplay display, EGLImageKHR image);
122 virtual ~NativeImageBufferEGL();
123 virtual void AddClient(gfx::GLImage* client) OVERRIDE;
124 virtual void RemoveClient(gfx::GLImage* client) OVERRIDE;
125 virtual bool IsClient(gfx::GLImage* client) OVERRIDE;
126 virtual void BindToTexture(GLenum target) OVERRIDE;
128 EGLDisplay egl_display_;
129 EGLImageKHR egl_image_;
134 ClientInfo(gfx::GLImage* client);
137 gfx::GLImage* client;
138 bool needs_wait_before_read;
140 std::list<ClientInfo> client_infos_;
141 gfx::GLImage* write_client_;
143 DISALLOW_COPY_AND_ASSIGN(NativeImageBufferEGL);
146 scoped_refptr<NativeImageBufferEGL> NativeImageBufferEGL::Create(
148 EGLDisplay egl_display = gfx::GLSurfaceEGL::GetHardwareDisplay();
149 EGLContext egl_context = eglGetCurrentContext();
151 DCHECK_NE(EGL_NO_CONTEXT, egl_context);
152 DCHECK_NE(EGL_NO_DISPLAY, egl_display);
153 DCHECK(glIsTexture(texture_id));
155 DCHECK(gfx::g_driver_egl.ext.b_EGL_KHR_image_base &&
156 gfx::g_driver_egl.ext.b_EGL_KHR_gl_texture_2D_image &&
157 gfx::g_driver_gl.ext.b_GL_OES_EGL_image);
159 const EGLint egl_attrib_list[] = {
160 EGL_GL_TEXTURE_LEVEL_KHR, 0, EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
161 EGLClientBuffer egl_buffer = reinterpret_cast<EGLClientBuffer>(texture_id);
162 EGLenum egl_target = EGL_GL_TEXTURE_2D_KHR; // TODO
164 EGLImageKHR egl_image = eglCreateImageKHR(
165 egl_display, egl_context, egl_target, egl_buffer, egl_attrib_list);
167 if (egl_image == EGL_NO_IMAGE_KHR)
170 return new NativeImageBufferEGL(egl_display, egl_image);
173 NativeImageBufferEGL::ClientInfo::ClientInfo(gfx::GLImage* client)
174 : client(client), needs_wait_before_read(true) {}
176 NativeImageBufferEGL::ClientInfo::~ClientInfo() {}
178 NativeImageBufferEGL::NativeImageBufferEGL(EGLDisplay display,
180 : NativeImageBuffer(),
181 egl_display_(display),
183 write_client_(NULL) {
184 DCHECK(egl_display_ != EGL_NO_DISPLAY);
185 DCHECK(egl_image_ != EGL_NO_IMAGE_KHR);
188 NativeImageBufferEGL::~NativeImageBufferEGL() {
189 DCHECK(client_infos_.empty());
190 if (egl_image_ != EGL_NO_IMAGE_KHR)
191 eglDestroyImageKHR(egl_display_, egl_image_);
194 void NativeImageBufferEGL::AddClient(gfx::GLImage* client) {
195 base::AutoLock lock(lock_);
196 client_infos_.push_back(ClientInfo(client));
199 void NativeImageBufferEGL::RemoveClient(gfx::GLImage* client) {
200 base::AutoLock lock(lock_);
201 if (write_client_ == client)
202 write_client_ = NULL;
203 for (std::list<ClientInfo>::iterator it = client_infos_.begin();
204 it != client_infos_.end();
206 if (it->client == client) {
207 client_infos_.erase(it);
214 bool NativeImageBufferEGL::IsClient(gfx::GLImage* client) {
215 base::AutoLock lock(lock_);
216 for (std::list<ClientInfo>::iterator it = client_infos_.begin();
217 it != client_infos_.end();
219 if (it->client == client)
225 void NativeImageBufferEGL::BindToTexture(GLenum target) {
226 DCHECK(egl_image_ != EGL_NO_IMAGE_KHR);
227 glEGLImageTargetTexture2DOES(target, egl_image_);
228 DCHECK_EQ(static_cast<EGLint>(EGL_SUCCESS), eglGetError());
229 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
234 class NativeImageBufferStub : public NativeImageBuffer {
236 NativeImageBufferStub() : NativeImageBuffer() {}
239 virtual ~NativeImageBufferStub() {}
240 virtual void AddClient(gfx::GLImage* client) OVERRIDE {}
241 virtual void RemoveClient(gfx::GLImage* client) OVERRIDE {}
242 virtual bool IsClient(gfx::GLImage* client) OVERRIDE { return true; }
243 virtual void BindToTexture(GLenum target) OVERRIDE {}
245 DISALLOW_COPY_AND_ASSIGN(NativeImageBufferStub);
248 } // anonymous namespace
251 scoped_refptr<NativeImageBuffer> NativeImageBuffer::Create(GLuint texture_id) {
252 switch (gfx::GetGLImplementation()) {
253 #if !defined(OS_MACOSX)
254 case gfx::kGLImplementationEGLGLES2:
255 return NativeImageBufferEGL::Create(texture_id);
257 case gfx::kGLImplementationMockGL:
258 return new NativeImageBufferStub;
265 TextureDefinition::LevelInfo::LevelInfo(GLenum target,
266 GLenum internal_format,
275 internal_format(internal_format),
284 TextureDefinition::LevelInfo::~LevelInfo() {}
286 TextureDefinition::TextureDefinition(
289 unsigned int version,
290 const scoped_refptr<NativeImageBuffer>& image_buffer)
293 image_buffer_(image_buffer.get()
295 : NativeImageBuffer::Create(texture->service_id())),
296 min_filter_(texture->min_filter()),
297 mag_filter_(texture->mag_filter()),
298 wrap_s_(texture->wrap_s()),
299 wrap_t_(texture->wrap_t()),
300 usage_(texture->usage()),
301 immutable_(texture->IsImmutable()) {
303 DCHECK(!texture->level_infos_.empty());
304 DCHECK(!texture->level_infos_[0].empty());
305 DCHECK(!texture->NeedsMips());
306 DCHECK(texture->level_infos_[0][0].width);
307 DCHECK(texture->level_infos_[0][0].height);
309 scoped_refptr<gfx::GLImage> gl_image(
310 new GLImageSync(image_buffer_,
311 gfx::Size(texture->level_infos_[0][0].width,
312 texture->level_infos_[0][0].height)));
313 texture->SetLevelImage(NULL, target, 0, gl_image.get());
316 level_infos_.clear();
317 const Texture::LevelInfo& level = texture->level_infos_[0][0];
318 LevelInfo info(level.target,
319 level.internal_format,
327 std::vector<LevelInfo> infos;
328 infos.push_back(info);
329 level_infos_.push_back(infos);
332 TextureDefinition::~TextureDefinition() {
335 Texture* TextureDefinition::CreateTexture() const {
336 if (!image_buffer_.get())
340 glGenTextures(1, &texture_id);
342 Texture* texture(new Texture(texture_id));
343 UpdateTexture(texture);
348 void TextureDefinition::UpdateTexture(Texture* texture) const {
349 gfx::ScopedTextureBinder texture_binder(target_, texture->service_id());
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter_);
351 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter_);
352 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s_);
353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t_);
354 if (image_buffer_.get())
355 image_buffer_->BindToTexture(target_);
356 // We have to make sure the changes are visible to other clients in this share
357 // group. As far as the clients are concerned, the mailbox semantics only
358 // demand a single flush from the client after changes are first made,
359 // and it is not visible to them when another share group boundary is crossed.
360 // We could probably track this and be a bit smarter about when to flush
364 texture->level_infos_.resize(1);
365 for (size_t i = 0; i < level_infos_.size(); i++) {
366 const LevelInfo& base_info = level_infos_[i][0];
367 const size_t levels_needed = TextureManager::ComputeMipMapCount(
368 base_info.target, base_info.width, base_info.height, base_info.depth);
369 DCHECK(level_infos_.size() <= levels_needed);
370 texture->level_infos_[0].resize(levels_needed);
371 for (size_t n = 0; n < level_infos_.size(); n++) {
372 const LevelInfo& info = level_infos_[i][n];
373 texture->SetLevelInfo(NULL,
376 info.internal_format,
386 if (image_buffer_.get()) {
387 texture->SetLevelImage(
393 gfx::Size(level_infos_[0][0].width, level_infos_[0][0].height)));
396 texture->target_ = target_;
397 texture->SetImmutable(immutable_);
398 texture->min_filter_ = min_filter_;
399 texture->mag_filter_ = mag_filter_;
400 texture->wrap_s_ = wrap_s_;
401 texture->wrap_t_ = wrap_t_;
402 texture->usage_ = usage_;
405 bool TextureDefinition::Matches(const Texture* texture) const {
406 DCHECK(target_ == texture->target());
407 if (texture->min_filter_ != min_filter_ ||
408 texture->mag_filter_ != mag_filter_ ||
409 texture->wrap_s_ != wrap_s_ ||
410 texture->wrap_t_ != wrap_t_) {
414 // All structural changes should have orphaned the texture.
415 if (image_buffer_.get() && !texture->GetLevelImage(texture->target(), 0))