summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/async_pixel_transfer_manager_android.cc
blob: 9b6b7e212c602268a40f971a3e80b39d6dcc7cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/async_pixel_transfer_manager.h"

#include "base/command_line.h"
#include "base/sys_info.h"
#include "base/trace_event/trace_event.h"
#include "gpu/command_buffer/service/async_pixel_transfer_manager_egl.h"
#include "gpu/command_buffer/service/async_pixel_transfer_manager_idle.h"
#include "gpu/command_buffer/service/async_pixel_transfer_manager_stub.h"
#include "gpu/command_buffer/service/async_pixel_transfer_manager_sync.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_implementation.h"

namespace gpu {
namespace {

enum GpuType {
  GPU_BROADCOM,
  GPU_IMAGINATION,
  GPU_NVIDIA_ES31,
  GPU_ADRENO_420,
  GPU_OTHER,
};

std::string MakeString(const char* s) {
  return std::string(s ? s : "");
}

GpuType GetGpuType() {
  const std::string vendor = MakeString(
      reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
  const std::string renderer = MakeString(
      reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
  const std::string version = MakeString(
      reinterpret_cast<const char*>(glGetString(GL_VERSION)));

  if (vendor.find("Broadcom") != std::string::npos)
    return GPU_BROADCOM;

  if (vendor.find("Imagination") != std::string::npos)
    return GPU_IMAGINATION;

  if (vendor.find("NVIDIA") != std::string::npos &&
      version.find("OpenGL ES 3.1") != std::string::npos) {
    return GPU_NVIDIA_ES31;
  }

  if (vendor.find("Qualcomm") != std::string::npos &&
      renderer.find("Adreno (TM) 420") != std::string::npos) {
    return GPU_ADRENO_420;
  }

  return GPU_OTHER;
}

bool AllowTransferThreadForGpu() {
  GpuType gpu = GetGpuType();
  return gpu != GPU_BROADCOM && gpu != GPU_IMAGINATION &&
         gpu != GPU_NVIDIA_ES31 && gpu != GPU_ADRENO_420;
}

}

// We only used threaded uploads when we can:
// - Create EGLImages out of OpenGL textures (EGL_KHR_gl_texture_2D_image)
// - Bind EGLImages to OpenGL textures (GL_OES_EGL_image)
// - Use fences (to test for upload completion).
// - The heap size is large enough.
// TODO(kaanb|epenner): Remove the IsImagination() check pending the
// resolution of crbug.com/249147
// TODO(kaanb|epenner): Remove the IsLowEndDevice() check pending the
// resolution of crbug.com/271929
AsyncPixelTransferManager* AsyncPixelTransferManager::Create(
    gfx::GLContext* context) {
  DCHECK(context->IsCurrent(NULL));
  base::CommandLine* cl = base::CommandLine::ForCurrentProcess();

  // Threaded mailbox uses EGLImage which conflicts with EGL uploader.
  // The spec only allows one EGL image per sibling group, but currently the
  // image handle cannot be shared between the threaded mailbox code and
  // AsyncPixelTransferManagerEGL.
  bool uses_threaded_mailboxes =
      cl->HasSwitch(switches::kEnableThreadedTextureMailboxes);
  // TexImage2D orphans the EGLImage used for threaded mailbox sharing.
  bool use_teximage2d_over_texsubimage2d = !uses_threaded_mailboxes;
  switch (gfx::GetGLImplementation()) {
    case gfx::kGLImplementationEGLGLES2:
      DCHECK(context);
      if (!base::SysInfo::IsLowEndDevice() &&
          context->HasExtension("EGL_KHR_fence_sync") &&
          context->HasExtension("EGL_KHR_image") &&
          context->HasExtension("EGL_KHR_image_base") &&
          context->HasExtension("EGL_KHR_gl_texture_2D_image") &&
          context->HasExtension("GL_OES_EGL_image") &&
          !uses_threaded_mailboxes && AllowTransferThreadForGpu()) {
        TRACE_EVENT0("gpu", "AsyncPixelTransferManager_CreateWithThread");
        return new AsyncPixelTransferManagerEGL;
      }
      return new AsyncPixelTransferManagerIdle(
          use_teximage2d_over_texsubimage2d);
    case gfx::kGLImplementationOSMesaGL: {
      TRACE_EVENT0("gpu", "AsyncPixelTransferManager_CreateIdle");
      return new AsyncPixelTransferManagerIdle(
          use_teximage2d_over_texsubimage2d);
    }
    case gfx::kGLImplementationMockGL:
      return new AsyncPixelTransferManagerStub;
    default:
      NOTREACHED();
      return NULL;
  }
}

}  // namespace gpu