1 // Copyright 2016 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 "gpu/command_buffer/common/gpu_memory_buffer_support.h"
6 
7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2extchromium.h>
9 
10 #include "base/check.h"
11 #include "base/notreached.h"
12 #include "base/stl_util.h"
13 #include "build/build_config.h"
14 #include "gpu/command_buffer/common/capabilities.h"
15 
16 namespace gpu {
17 
18 #if defined(OS_MAC)
19 static uint32_t macos_specific_texture_target = GL_TEXTURE_RECTANGLE_ARB;
20 #endif  // defined(OS_MAC)
21 
IsImageFromGpuMemoryBufferFormatSupported(gfx::BufferFormat format,const gpu::Capabilities & capabilities)22 bool IsImageFromGpuMemoryBufferFormatSupported(
23     gfx::BufferFormat format,
24     const gpu::Capabilities& capabilities) {
25   return capabilities.gpu_memory_buffer_formats.Has(format);
26 }
27 
IsImageSizeValidForGpuMemoryBufferFormat(const gfx::Size & size,gfx::BufferFormat format)28 bool IsImageSizeValidForGpuMemoryBufferFormat(const gfx::Size& size,
29                                               gfx::BufferFormat format) {
30   switch (format) {
31     case gfx::BufferFormat::R_8:
32     case gfx::BufferFormat::R_16:
33     case gfx::BufferFormat::RG_88:
34     case gfx::BufferFormat::BGR_565:
35     case gfx::BufferFormat::RGBA_4444:
36     case gfx::BufferFormat::RGBA_8888:
37     case gfx::BufferFormat::RGBX_8888:
38     case gfx::BufferFormat::BGRA_8888:
39     case gfx::BufferFormat::BGRX_8888:
40     case gfx::BufferFormat::BGRA_1010102:
41     case gfx::BufferFormat::RGBA_1010102:
42     case gfx::BufferFormat::RGBA_F16:
43       return true;
44     case gfx::BufferFormat::YVU_420:
45     case gfx::BufferFormat::YUV_420_BIPLANAR:
46     case gfx::BufferFormat::P010:
47       // U and V planes are subsampled by a factor of 2.
48       return size.width() % 2 == 0 && size.height() % 2 == 0;
49   }
50 
51   NOTREACHED();
52   return false;
53 }
54 
GetPlatformSpecificTextureTarget()55 uint32_t GetPlatformSpecificTextureTarget() {
56 #if defined(OS_MAC)
57   return macos_specific_texture_target;
58 #elif defined(OS_ANDROID) || defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
59   return GL_TEXTURE_EXTERNAL_OES;
60 #elif defined(OS_WIN) || defined(OS_FUCHSIA)
61   return GL_TEXTURE_2D;
62 #elif defined(OS_NACL)
63   NOTREACHED();
64   return 0;
65 #else
66 #error Unsupported OS
67 #endif
68 }
69 
70 #if defined(OS_MAC)
SetMacOSSpecificTextureTarget(uint32_t texture_target)71 GPU_EXPORT void SetMacOSSpecificTextureTarget(uint32_t texture_target) {
72   DCHECK(texture_target == GL_TEXTURE_2D ||
73          texture_target == GL_TEXTURE_RECTANGLE_ARB);
74   macos_specific_texture_target = texture_target;
75 }
76 #endif  // defined(OS_MAC)
77 
GetBufferTextureTarget(gfx::BufferUsage usage,gfx::BufferFormat format,const Capabilities & capabilities)78 GPU_EXPORT uint32_t GetBufferTextureTarget(gfx::BufferUsage usage,
79                                            gfx::BufferFormat format,
80                                            const Capabilities& capabilities) {
81   bool found = base::Contains(capabilities.texture_target_exception_list,
82                               gfx::BufferUsageAndFormat(usage, format));
83   return found ? gpu::GetPlatformSpecificTextureTarget() : GL_TEXTURE_2D;
84 }
85 
NativeBufferNeedsPlatformSpecificTextureTarget(gfx::BufferFormat format)86 GPU_EXPORT bool NativeBufferNeedsPlatformSpecificTextureTarget(
87     gfx::BufferFormat format) {
88 #if defined(USE_OZONE) || defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
89   // Always use GL_TEXTURE_2D as the target for RGB textures.
90   // https://crbug.com/916728
91   if (format == gfx::BufferFormat::R_8 || format == gfx::BufferFormat::RG_88 ||
92       format == gfx::BufferFormat::RGBA_8888 ||
93       format == gfx::BufferFormat::BGRA_8888 ||
94       format == gfx::BufferFormat::RGBX_8888 ||
95       format == gfx::BufferFormat::BGRX_8888 ||
96       format == gfx::BufferFormat::RGBA_1010102 ||
97       format == gfx::BufferFormat::BGRA_1010102) {
98     return false;
99   }
100 #elif defined(OS_ANDROID)
101   if (format == gfx::BufferFormat::BGR_565 ||
102       format == gfx::BufferFormat::RGBA_8888) {
103     return false;
104   }
105 #endif
106   return true;
107 }
108 
109 }  // namespace gpu
110