[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / content / common / gpu / image_transport_surface_iosurface_mac.cc
blob49aa4cda74dbc32fe7e271bb617d344723c7a43c
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/image_transport_surface_iosurface_mac.h"
7 #include "content/common/gpu/gpu_messages.h"
8 #include "content/common/gpu/surface_handle_types_mac.h"
10 namespace content {
11 namespace {
13 // IOSurface dimensions will be rounded up to a multiple of this value in order
14 // to reduce memory thrashing during resize. This must be a power of 2.
15 const uint32 kIOSurfaceDimensionRoundup = 64;
17 int RoundUpSurfaceDimension(int number) {
18 DCHECK(number >= 0);
19 // Cast into unsigned space for portable bitwise ops.
20 uint32 unsigned_number = static_cast<uint32>(number);
21 uint32 roundup_sub_1 = kIOSurfaceDimensionRoundup - 1;
22 unsigned_number = (unsigned_number + roundup_sub_1) & ~roundup_sub_1;
23 return static_cast<int>(unsigned_number);
26 void AddBooleanValue(CFMutableDictionaryRef dictionary,
27 const CFStringRef key,
28 bool value) {
29 CFDictionaryAddValue(dictionary, key,
30 (value ? kCFBooleanTrue : kCFBooleanFalse));
33 void AddIntegerValue(CFMutableDictionaryRef dictionary,
34 const CFStringRef key,
35 int32 value) {
36 base::ScopedCFTypeRef<CFNumberRef> number(
37 CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
38 CFDictionaryAddValue(dictionary, key, number.get());
41 } // namespace
43 IOSurfaceStorageProvider::IOSurfaceStorageProvider() {}
45 IOSurfaceStorageProvider::~IOSurfaceStorageProvider() {
46 DCHECK(!io_surface_);
49 gfx::Size IOSurfaceStorageProvider::GetRoundedSize(gfx::Size size) {
50 return gfx::Size(RoundUpSurfaceDimension(size.width()),
51 RoundUpSurfaceDimension(size.height()));
54 bool IOSurfaceStorageProvider::AllocateColorBufferStorage(
55 CGLContextObj context, GLuint texture,
56 gfx::Size pixel_size, float scale_factor) {
57 // Allocate a new IOSurface, which is the GPU resource that can be
58 // shared across processes.
59 base::ScopedCFTypeRef<CFMutableDictionaryRef> properties;
60 properties.reset(CFDictionaryCreateMutable(kCFAllocatorDefault,
62 &kCFTypeDictionaryKeyCallBacks,
63 &kCFTypeDictionaryValueCallBacks));
64 AddIntegerValue(properties,
65 kIOSurfaceWidth,
66 pixel_size.width());
67 AddIntegerValue(properties,
68 kIOSurfaceHeight,
69 pixel_size.height());
70 AddIntegerValue(properties,
71 kIOSurfaceBytesPerElement, 4);
72 AddBooleanValue(properties,
73 kIOSurfaceIsGlobal, true);
74 // I believe we should be able to unreference the IOSurfaces without
75 // synchronizing with the browser process because they are
76 // ultimately reference counted by the operating system.
77 io_surface_.reset(IOSurfaceCreate(properties));
78 io_surface_id_ = IOSurfaceGetID(io_surface_);
80 // Don't think we need to identify a plane.
81 GLuint plane = 0;
82 CGLError cglerror = CGLTexImageIOSurface2D(
83 context,
84 GL_TEXTURE_RECTANGLE_ARB,
85 GL_RGBA,
86 pixel_size.width(),
87 pixel_size.height(),
88 GL_BGRA,
89 GL_UNSIGNED_INT_8_8_8_8_REV,
90 io_surface_.get(),
91 plane);
92 if (cglerror != kCGLNoError) {
93 DLOG(ERROR) << "CGLTexImageIOSurface2D failed with CGL error: " << cglerror;
94 return false;
97 glFlush();
98 return true;
101 void IOSurfaceStorageProvider::FreeColorBufferStorage() {
102 io_surface_.reset();
103 io_surface_id_ = 0;
106 uint64 IOSurfaceStorageProvider::GetSurfaceHandle() const {
107 return SurfaceHandleFromIOSurfaceID(io_surface_id_);
110 void IOSurfaceStorageProvider::WillSwapBuffers() {
113 } // namespace content