| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/common/gpu/image_transport_surface_iosurface_mac.h" | |
| 6 | |
| 7 #include "content/common/gpu/gpu_messages.h" | |
| 8 #include "ui/accelerated_widget_mac/surface_handle_types.h" | |
| 9 | |
| 10 namespace content { | |
| 11 namespace { | |
| 12 | |
| 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 = 1; | |
| 16 | |
| 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); | |
| 24 } | |
| 25 | |
| 26 void AddBooleanValue(CFMutableDictionaryRef dictionary, | |
| 27 const CFStringRef key, | |
| 28 bool value) { | |
| 29 CFDictionaryAddValue(dictionary, key, | |
| 30 (value ? kCFBooleanTrue : kCFBooleanFalse)); | |
| 31 } | |
| 32 | |
| 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()); | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 IOSurfaceStorageProvider::IOSurfaceStorageProvider( | |
| 44 ImageTransportSurfaceFBO* transport_surface) | |
| 45 : transport_surface_(transport_surface), | |
| 46 frame_scale_factor_(1) {} | |
| 47 | |
| 48 IOSurfaceStorageProvider::~IOSurfaceStorageProvider() { | |
| 49 DCHECK(!io_surface_); | |
| 50 } | |
| 51 | |
| 52 gfx::Size IOSurfaceStorageProvider::GetRoundedSize(gfx::Size size) { | |
| 53 return gfx::Size(RoundUpSurfaceDimension(size.width()), | |
| 54 RoundUpSurfaceDimension(size.height())); | |
| 55 } | |
| 56 | |
| 57 bool IOSurfaceStorageProvider::AllocateColorBufferStorage( | |
| 58 CGLContextObj context, const base::Closure& context_dirtied_callback, | |
| 59 GLuint texture, gfx::Size pixel_size, float scale_factor) { | |
| 60 // Allocate a new IOSurface, which is the GPU resource that can be | |
| 61 // shared across processes. | |
| 62 unsigned pixel_format = 'BGRA'; | |
| 63 base::ScopedCFTypeRef<CFMutableDictionaryRef> properties; | |
| 64 properties.reset(CFDictionaryCreateMutable(kCFAllocatorDefault, | |
| 65 0, | |
| 66 &kCFTypeDictionaryKeyCallBacks, | |
| 67 &kCFTypeDictionaryValueCallBacks)); | |
| 68 AddIntegerValue(properties, kIOSurfacePixelFormat, pixel_format); | |
| 69 AddIntegerValue(properties, | |
| 70 kIOSurfaceWidth, | |
| 71 pixel_size.width()); | |
| 72 AddIntegerValue(properties, | |
| 73 kIOSurfaceHeight, | |
| 74 pixel_size.height()); | |
| 75 AddIntegerValue(properties, | |
| 76 kIOSurfaceBytesPerElement, 4); | |
| 77 AddBooleanValue(properties, | |
| 78 kIOSurfaceIsGlobal, true); | |
| 79 // I believe we should be able to unreference the IOSurfaces without | |
| 80 // synchronizing with the browser process because they are | |
| 81 // ultimately reference counted by the operating system. | |
| 82 io_surface_.reset(IOSurfaceCreate(properties)); | |
| 83 io_surface_id_ = IOSurfaceGetID(io_surface_); | |
| 84 | |
| 85 // Don't think we need to identify a plane. | |
| 86 GLuint plane = 0; | |
| 87 CGLError cglerror = CGLTexImageIOSurface2D( | |
| 88 context, | |
| 89 GL_TEXTURE_RECTANGLE_ARB, | |
| 90 GL_RGBA, | |
| 91 pixel_size.width(), | |
| 92 pixel_size.height(), | |
| 93 GL_BGRA, | |
| 94 GL_UNSIGNED_INT_8_8_8_8_REV, | |
| 95 io_surface_.get(), | |
| 96 plane); | |
| 97 if (cglerror != kCGLNoError) { | |
| 98 DLOG(ERROR) << "CGLTexImageIOSurface2D failed with CGL error: " << cglerror; | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 glFlush(); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 void IOSurfaceStorageProvider::FreeColorBufferStorage() { | |
| 107 io_surface_.reset(); | |
| 108 io_surface_id_ = 0; | |
| 109 } | |
| 110 | |
| 111 void IOSurfaceStorageProvider::FrameSizeChanged(const gfx::Size& pixel_size, | |
| 112 float scale_factor) { | |
| 113 frame_pixel_size_ = pixel_size; | |
| 114 frame_scale_factor_ = scale_factor; | |
| 115 } | |
| 116 | |
| 117 void IOSurfaceStorageProvider::SwapBuffers(const gfx::Rect& dirty_rect) { | |
| 118 // The browser compositor will throttle itself, so we are free to unblock the | |
| 119 // context immediately. Make sure that the browser is doing its throttling | |
| 120 // appropriately by ensuring that the previous swap was acknowledged before | |
| 121 // we get another swap. | |
| 122 DCHECK(pending_swapped_surfaces_.empty()); | |
| 123 pending_swapped_surfaces_.push_back(io_surface_); | |
| 124 | |
| 125 transport_surface_->SendSwapBuffers( | |
| 126 ui::SurfaceHandleFromIOSurfaceID(io_surface_id_), | |
| 127 frame_pixel_size_, | |
| 128 frame_scale_factor_); | |
| 129 } | |
| 130 | |
| 131 void IOSurfaceStorageProvider::WillWriteToBackbuffer() { | |
| 132 } | |
| 133 | |
| 134 void IOSurfaceStorageProvider::DiscardBackbuffer() { | |
| 135 } | |
| 136 | |
| 137 void IOSurfaceStorageProvider::SwapBuffersAckedByBrowser( | |
| 138 bool disable_throttling) { | |
| 139 DCHECK(!pending_swapped_surfaces_.empty()); | |
| 140 pending_swapped_surfaces_.pop_front(); | |
| 141 } | |
| 142 | |
| 143 } // namespace content | |
| OLD | NEW |