OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sky/compositor/resource_manager.h" | |
6 | |
7 #ifndef GL_GLEXT_PROTOTYPES | |
8 #define GL_GLEXT_PROTOTYPES | |
9 #endif | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/stl_util.h" | |
13 #include "gpu/GLES2/gl2chromium.h" | |
14 #include "gpu/GLES2/gl2extchromium.h" | |
15 #include "mojo/converters/geometry/geometry_type_converters.h" | |
16 #include "mojo/gpu/gl_context.h" | |
17 #include "mojo/gpu/gl_texture.h" | |
18 #include "mojo/public/c/gles2/gles2.h" | |
19 #include "sky/compositor/layer.h" | |
20 | |
21 namespace sky { | |
22 | |
23 ResourceManager::ResourceManager(base::WeakPtr<mojo::GLContext> gl_context) | |
24 : gl_context_(gl_context), next_resource_id_(0) { | |
25 } | |
26 | |
27 ResourceManager::~ResourceManager() { | |
28 STLDeleteContainerPairSecondPointers(resource_to_texture_map_.begin(), | |
29 resource_to_texture_map_.end()); | |
30 } | |
31 | |
32 scoped_ptr<mojo::GLTexture> ResourceManager::CreateTexture( | |
33 const gfx::Size& size) { | |
34 scoped_ptr<mojo::GLTexture> texture = texture_cache_.GetTexture(size); | |
35 if (texture) | |
36 return texture.Pass(); | |
37 gl_context_->MakeCurrent(); | |
38 return make_scoped_ptr(new mojo::GLTexture( | |
39 gl_context_, mojo::TypeConverter<mojo::Size, gfx::Size>::Convert(size))); | |
40 } | |
41 | |
42 mojo::TransferableResourcePtr ResourceManager::CreateTransferableResource( | |
43 Layer* layer) { | |
44 scoped_ptr<mojo::GLTexture> texture = layer->GetTexture(); | |
45 mojo::Size size = texture->size(); | |
46 | |
47 gl_context_->MakeCurrent(); | |
48 glBindTexture(GL_TEXTURE_2D, texture->texture_id()); | |
49 GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM]; | |
50 glGenMailboxCHROMIUM(mailbox); | |
51 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox); | |
52 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
53 | |
54 mojo::TransferableResourcePtr resource = mojo::TransferableResource::New(); | |
55 resource->id = next_resource_id_++; | |
56 resource_to_texture_map_[resource->id] = texture.release(); | |
57 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; | |
58 resource->filter = GL_LINEAR; | |
59 resource->size = size.Clone(); | |
60 resource->is_repeated = false; | |
61 resource->is_software = false; | |
62 | |
63 mojo::MailboxHolderPtr mailbox_holder = mojo::MailboxHolder::New(); | |
64 mailbox_holder->mailbox = mojo::Mailbox::New(); | |
65 for (int i = 0; i < GL_MAILBOX_SIZE_CHROMIUM; ++i) | |
66 mailbox_holder->mailbox->name.push_back(mailbox[i]); | |
67 mailbox_holder->texture_target = GL_TEXTURE_2D; | |
68 mailbox_holder->sync_point = sync_point; | |
69 resource->mailbox_holder = mailbox_holder.Pass(); | |
70 | |
71 return resource.Pass(); | |
72 } | |
73 | |
74 void ResourceManager::ReturnResources( | |
75 mojo::Array<mojo::ReturnedResourcePtr> resources) { | |
76 DCHECK(resources.size()); | |
77 | |
78 gl_context_->MakeCurrent(); | |
79 for (size_t i = 0u; i < resources.size(); ++i) { | |
80 mojo::ReturnedResourcePtr resource = resources[i].Pass(); | |
81 DCHECK_EQ(1, resource->count); | |
82 auto iter = resource_to_texture_map_.find(resource->id); | |
83 if (iter == resource_to_texture_map_.end()) | |
84 continue; | |
85 scoped_ptr<mojo::GLTexture> texture(iter->second); | |
86 DCHECK_NE(0u, texture->texture_id()); | |
87 resource_to_texture_map_.erase(iter); | |
88 glWaitSyncPointCHROMIUM(resource->sync_point); | |
89 texture_cache_.PutTexture(texture.Pass()); | |
90 } | |
91 } | |
92 | |
93 } // namespace examples | |
OLD | NEW |