GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_surface_texture.cc
bloba1d0e5c0d3fa67425b2b9c5aa2d8ccb0f3611381
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.
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h"
7 #include "base/logging.h"
8 #include "base/trace_event/trace_event.h"
9 #include "content/common/android/surface_texture_manager.h"
10 #include "ui/gl/gl_bindings.h"
12 namespace content {
13 namespace {
15 int WindowFormat(gfx::GpuMemoryBuffer::Format format) {
16 switch (format) {
17 case gfx::GpuMemoryBuffer::RGBA_8888:
18 return WINDOW_FORMAT_RGBA_8888;
19 case gfx::GpuMemoryBuffer::ATC:
20 case gfx::GpuMemoryBuffer::ATCIA:
21 case gfx::GpuMemoryBuffer::DXT1:
22 case gfx::GpuMemoryBuffer::DXT5:
23 case gfx::GpuMemoryBuffer::ETC1:
24 case gfx::GpuMemoryBuffer::RGBX_8888:
25 case gfx::GpuMemoryBuffer::BGRA_8888:
26 NOTREACHED();
27 return 0;
30 NOTREACHED();
31 return 0;
34 } // namespace
36 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
37 gfx::GpuMemoryBufferId id,
38 const gfx::Size& size,
39 Format format,
40 const DestructionCallback& callback,
41 ANativeWindow* native_window)
42 : GpuMemoryBufferImpl(id, size, format, callback),
43 native_window_(native_window),
44 stride_(0) {
47 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
48 ANativeWindow_release(native_window_);
51 // static
52 scoped_ptr<GpuMemoryBufferImpl>
53 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
54 const gfx::GpuMemoryBufferHandle& handle,
55 const gfx::Size& size,
56 Format format,
57 const DestructionCallback& callback) {
58 ANativeWindow* native_window = SurfaceTextureManager::GetInstance()->
59 AcquireNativeWidgetForSurfaceTexture(handle.id);
60 if (!native_window)
61 return scoped_ptr<GpuMemoryBufferImpl>();
63 ANativeWindow_setBuffersGeometry(
64 native_window, size.width(), size.height(), WindowFormat(format));
66 return make_scoped_ptr<GpuMemoryBufferImpl>(
67 new GpuMemoryBufferImplSurfaceTexture(
68 handle.id, size, format, callback, native_window));
71 bool GpuMemoryBufferImplSurfaceTexture::Map(void** data) {
72 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
74 DCHECK(!mapped_);
75 DCHECK(native_window_);
76 ANativeWindow_Buffer buffer;
77 int status = ANativeWindow_lock(native_window_, &buffer, NULL);
78 if (status) {
79 VLOG(1) << "ANativeWindow_lock failed with error code: " << status;
80 return false;
83 size_t stride_in_bytes = 0;
84 if (!StrideInBytes(buffer.stride, format_, &stride_in_bytes))
85 return false;
87 DCHECK_LE(size_.width(), buffer.stride);
88 stride_ = stride_in_bytes;
89 mapped_ = true;
90 *data = buffer.bits;
91 return true;
94 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
95 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
97 DCHECK(mapped_);
98 ANativeWindow_unlockAndPost(native_window_);
99 mapped_ = false;
102 void GpuMemoryBufferImplSurfaceTexture::GetStride(uint32* stride) const {
103 *stride = stride_;
106 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle()
107 const {
108 gfx::GpuMemoryBufferHandle handle;
109 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
110 handle.id = id_;
111 return handle;
114 } // namespace content