15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/image_transport_factory_android.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/lazy_instance.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/stringprintf.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/gpu/client/gl_helper.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/common/gpu/gpu_process_launch_causes.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "gpu/command_buffer/client/gles2_implementation.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/khronos/GLES2/gl2.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ui/gfx/android/device_display_info.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbase::LazyInstance<ObserverList<ImageTransportFactoryAndroidObserver> >::Leaky
217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    g_factory_observers = LAZY_INSTANCE_INITIALIZER;
227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochclass GLContextLostListener
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback {
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch public:
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // WebGraphicsContextLostCallback implementation.
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual void onContextLost() OVERRIDE;
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch private:
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static void DidLoseContext();
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch};
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static ImageTransportFactoryAndroid* g_factory = NULL;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CmdBufferImageTransportFactory : public ImageTransportFactoryAndroid {
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CmdBufferImageTransportFactory();
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~CmdBufferImageTransportFactory();
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual GLHelper* GetGLHelper() OVERRIDE;
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual uint32 GetChannelID() OVERRIDE {
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return BrowserGpuChannelHostFactory::instance()->GetGpuChannelId();
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context_;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<GLHelper> gl_helper_;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CmdBufferImageTransportFactory);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)CmdBufferImageTransportFactory::CmdBufferImageTransportFactory() {
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  BrowserGpuChannelHostFactory* factory =
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      BrowserGpuChannelHostFactory::instance();
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_refptr<GpuChannelHost> gpu_channel_host(factory->EstablishGpuChannelSync(
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE));
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(gpu_channel_host);
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  blink::WebGraphicsContext3D::Attributes attrs;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  attrs.shareResources = true;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url("chrome://gpu/ImageTransportFactoryAndroid");
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  static const size_t kBytesPerPixel = 4;
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  gfx::DeviceDisplayInfo display_info;
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  size_t full_screen_texture_size_in_bytes = display_info.GetDisplayHeight() *
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                             display_info.GetDisplayWidth() *
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                             kBytesPerPixel;
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  limits.command_buffer_size = 64 * 1024;
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  limits.start_transfer_buffer_size = 64 * 1024;
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  limits.min_transfer_buffer_size = 64 * 1024;
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  limits.max_transfer_buffer_size = std::min(
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  limits.mapped_memory_reclaim_limit =
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      WebGraphicsContext3DCommandBufferImpl::kNoLimit;
76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  bool lose_context_when_out_of_memory = false;
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  context_.reset(
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      new WebGraphicsContext3DCommandBufferImpl(0,  // offscreen
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                url,
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                gpu_channel_host.get(),
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                attrs,
82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                                                lose_context_when_out_of_memory,
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                limits,
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                NULL));
857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  context_->setContextLostCallback(context_lost_listener_.get());
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (context_->InitializeOnCurrentThread())
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    context_->pushGroupMarkerEXT(
88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        base::StringPrintf("CmdBufferImageTransportFactory-%p",
89eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           context_.get()).c_str());
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)CmdBufferImageTransportFactory::~CmdBufferImageTransportFactory() {
937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  context_->setContextLostCallback(NULL);
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GLHelper* CmdBufferImageTransportFactory::GetGLHelper() {
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!gl_helper_)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gl_helper_.reset(new GLHelper(context_->GetImplementation(),
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                  context_->GetContextSupport()));
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gl_helper_.get();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // anonymous namespace
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ImageTransportFactoryAndroid::InitializeForUnitTests(
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    scoped_ptr<ImageTransportFactoryAndroid> test_factory) {
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!g_factory);
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  g_factory = test_factory.release();
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// static
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ImageTransportFactoryAndroid::TerminateForUnitTests() {
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(g_factory);
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  delete g_factory;
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  g_factory = NULL;
1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// static
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ImageTransportFactoryAndroid* ImageTransportFactoryAndroid::GetInstance() {
1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!g_factory)
1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    g_factory = new CmdBufferImageTransportFactory();
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return g_factory;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1287dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochImageTransportFactoryAndroid::ImageTransportFactoryAndroid()
1297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    : context_lost_listener_(new GLContextLostListener()) {}
1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1317dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochImageTransportFactoryAndroid::~ImageTransportFactoryAndroid() {}
1327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid ImageTransportFactoryAndroid::AddObserver(
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ImageTransportFactoryAndroidObserver* observer) {
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  g_factory_observers.Get().AddObserver(observer);
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid ImageTransportFactoryAndroid::RemoveObserver(
1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ImageTransportFactoryAndroidObserver* observer) {
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  g_factory_observers.Get().RemoveObserver(observer);
1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid GLContextLostListener::onContextLost() {
1447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Need to post a task because the command buffer client cannot be deleted
1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // from within this callback.
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  LOG(ERROR) << "Context lost.";
1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::MessageLoop::current()->PostTask(
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      FROM_HERE,
1497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      base::Bind(&GLContextLostListener::DidLoseContext));
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid GLContextLostListener::DidLoseContext() {
1537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  delete g_factory;
1547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  g_factory = NULL;
1557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FOR_EACH_OBSERVER(ImageTransportFactoryAndroidObserver,
1567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      g_factory_observers.Get(),
1577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      OnLostResources());
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace content
161