| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 // Transport texture is a mechanism to share a texture between renderer process | |
| 6 // and GPU process. This is useful when a texture is used in the renderer | |
| 7 // process but updated in the GPU process. | |
| 8 // | |
| 9 // BACKGROUND INFORMATION | |
| 10 // | |
| 11 // Renderer process uses command buffer to submit GLES2 commands to the GPU | |
| 12 // process for execution. When using a texture in the renderer process it is | |
| 13 // supposed to update the texture, for example by using glTexImage2D(). | |
| 14 // | |
| 15 // However for some cases the texture needs to be updated in the GPU process. | |
| 16 // Objects other than command buffer in the GPU process will then need to | |
| 17 // access the texture ID. | |
| 18 // | |
| 19 // This class provides the following functions that solve the problems: | |
| 20 // 1. Textures be requested in the GPU process. The request is submitted | |
| 21 // to renderer process and textures are created in the command buffer | |
| 22 // context and eventually in the system context. | |
| 23 // 2. Textures are then create and used in the renderer process. | |
| 24 // 3. The corresponding texture ID in the GPU process can be obtained. | |
| 25 // 4. When texture is updated in the GPU process, notification can be received | |
| 26 // in the renderer process. | |
| 27 // | |
| 28 // THREAD SEMANTICS | |
| 29 // | |
| 30 // TransportTextureHost *must* be created on the render thread. | |
| 31 // After that methods of this object call be called on any thread. | |
| 32 // | |
| 33 // GLES2 commands are executed on Render Thread. IPC messages are sent and | |
| 34 // received on IO thread. | |
| 35 // | |
| 36 // USAGE | |
| 37 // | |
| 38 // -------------------------- | |
| 39 // | In the renderer procss | | |
| 40 // -------------------------- | |
| 41 // | |
| 42 // class TextureUpdateHandler { | |
| 43 // public: | |
| 44 // void OnTextureUpdate(int texture_id) { | |
| 45 // // Do something with the texture. | |
| 46 // } | |
| 47 // } | |
| 48 // | |
| 49 // TransportTextureHost factory_host; | |
| 50 // TextureUpdateHandler handler; | |
| 51 // | |
| 52 // void MyObjectInitDone() { | |
| 53 // std::vector<int> textures; | |
| 54 // factory_host.GetTextures( | |
| 55 // NewCallback(&handler, &TextureUpdateHandler::OnTextureUpdate), | |
| 56 // &textures); | |
| 57 // } | |
| 58 // | |
| 59 // void InitDone() { | |
| 60 // InitMyObjectInGPUProcess(factory_host.GetPeerId(), | |
| 61 // NewRunnableFunction(&MyObjectInitDone)); | |
| 62 // } | |
| 63 // | |
| 64 // factory_host.Init(NewRunnableFunction(&InitDone)); | |
| 65 // | |
| 66 // ---------------------- | |
| 67 // | In the GPU process | | |
| 68 // ---------------------- | |
| 69 // | |
| 70 // // When the transport texture factory id is known. | |
| 71 // TransportTexture* factory = gpu_channel->GetTransportTexture(id); | |
| 72 // | |
| 73 // void TextureCreateDone(vector<int> textures) { | |
| 74 // // Send message to renderer saying init is done. | |
| 75 // SendInitDone(); | |
| 76 // | |
| 77 // UpdateTextureContent(textures[0]); | |
| 78 // factory->TextureUpdated(textures[0]); | |
| 79 // } | |
| 80 // | |
| 81 // // When init is requested from renderer. | |
| 82 // vector<int> textures; | |
| 83 // | |
| 84 // void OnInit() { | |
| 85 // factory->CreateTextures(3, 1024, 768, TransportTexture::RGB, &textures, | |
| 86 // NewRunnableFunction(&TextureCreateDone), | |
| 87 // textures); | |
| 88 // } | |
| 89 | |
| 90 #ifndef CONTENT_RENDERER_TRANSPORT_TEXTURE_HOST_H_ | |
| 91 #define CONTENT_RENDERER_TRANSPORT_TEXTURE_HOST_H_ | |
| 92 | |
| 93 #include <vector> | |
| 94 | |
| 95 #include "base/basictypes.h" | |
| 96 #include "base/callback_old.h" | |
| 97 #include "base/memory/ref_counted.h" | |
| 98 #include "base/memory/scoped_ptr.h" | |
| 99 #include "base/task.h" | |
| 100 #include "ipc/ipc_channel.h" | |
| 101 | |
| 102 class MessageLoop; | |
| 103 class RendererGLContext; | |
| 104 class TransportTextureService; | |
| 105 | |
| 106 class TransportTextureHost | |
| 107 : public base::RefCountedThreadSafe<TransportTextureHost>, | |
| 108 public IPC::Channel::Listener { | |
| 109 public: | |
| 110 typedef Callback1<int>::Type TextureUpdateCallback; | |
| 111 | |
| 112 // |io_message_loop| is where the IPC communication should happen. | |
| 113 // |render_message_loop| is where the GLES2 commands should be exeucted. | |
| 114 // |service| contains the route to this object. | |
| 115 // |sender| is used to send IPC messages to GPU process. | |
| 116 // |context| is the RendererGLContextt for generating textures. | |
| 117 // |context_route_id| is the route ID for the GpuChannelHost. | |
| 118 // |host_id| is the ID of this object in GpuChannelHost. | |
| 119 TransportTextureHost(MessageLoop* io_message_loop, | |
| 120 MessageLoop* render_message_loop, | |
| 121 TransportTextureService* service, | |
| 122 IPC::Message::Sender* sender, | |
| 123 RendererGLContext* context, | |
| 124 int32 context_route_id, | |
| 125 int32 host_id); | |
| 126 virtual ~TransportTextureHost(); | |
| 127 | |
| 128 // Initialize this object, this will cause a corresponding | |
| 129 // TransportTexture be created in the GPU process. | |
| 130 // |done_task| is called when initialization is completed. | |
| 131 void Init(Task* done_task); | |
| 132 | |
| 133 // Destroy resources acquired by this object and in the GPU process. | |
| 134 // | |
| 135 // WARNING | |
| 136 // | |
| 137 // Call this method only after textures are not used in both the renderer | |
| 138 // and GPU process. | |
| 139 void Destroy(); | |
| 140 | |
| 141 // Get the list of textures generated through this factory. | |
| 142 // A callback must be provided to listen to texture update events. The | |
| 143 // callback will be called on the IO thread. | |
| 144 // | |
| 145 // Note that this method doesn't generate any textures, it simply return | |
| 146 // the list of textures generated. | |
| 147 void GetTextures(TextureUpdateCallback* callback, | |
| 148 std::vector<int>* textures); | |
| 149 | |
| 150 // Return the peer ID of TransportTexture in the GPU process. | |
| 151 int GetPeerId(); | |
| 152 | |
| 153 // IPC::Channel::Listener. | |
| 154 virtual void OnChannelConnected(int32 peer_pid); | |
| 155 virtual void OnChannelError(); | |
| 156 virtual bool OnMessageReceived(const IPC::Message& message); | |
| 157 | |
| 158 private: | |
| 159 // Released all textures generated. | |
| 160 void ReleaseTexturesInternal(); | |
| 161 | |
| 162 // Send the texture IDs to the GPU process. This will copy the set of | |
| 163 // texture IDs. | |
| 164 void SendTexturesInternal(std::vector<int> textures); | |
| 165 | |
| 166 // Send the destroy message to the GPU process. | |
| 167 void SendDestroyInternal(); | |
| 168 | |
| 169 //////////////////////////////////////////////////////////////////////////// | |
| 170 // IPC Message Handlers | |
| 171 void OnTransportTextureCreated(int32 peer_id); | |
| 172 void OnCreateTextures(int32 n, uint32 width, uint32 height, int32 format); | |
| 173 void OnReleaseTextures(); | |
| 174 void OnTextureUpdated(int texture_id); | |
| 175 | |
| 176 MessageLoop* io_message_loop_; | |
| 177 MessageLoop* render_message_loop_; | |
| 178 scoped_refptr<TransportTextureService> service_; | |
| 179 | |
| 180 IPC::Message::Sender* sender_; | |
| 181 RendererGLContext* context_; | |
| 182 int32 context_route_id_; | |
| 183 int32 host_id_; | |
| 184 int32 peer_id_; | |
| 185 | |
| 186 scoped_ptr<Task> init_task_; | |
| 187 | |
| 188 // A list of textures generated. | |
| 189 std::vector<int> textures_; | |
| 190 | |
| 191 // Callback when a texture is updated. | |
| 192 scoped_ptr<TextureUpdateCallback> update_callback_; | |
| 193 | |
| 194 DISALLOW_COPY_AND_ASSIGN(TransportTextureHost); | |
| 195 }; | |
| 196 | |
| 197 #endif // CONTENT_RENDERER_TRANSPORT_TEXTURE_HOST_H_ | |
| OLD | NEW |