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 "ui/accelerated_widget_mac/software_layer.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/scoped_cftyperef.h"
9 #include "base/mac/sdk_forward_declarations.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/base/cocoa/animation_utils.h"
13 @implementation SoftwareLayer
16 if (self = [super init]) {
17 [self setAnchorPoint:CGPointMake(0, 0)];
18 // Setting contents gravity is necessary to prevent the layer from being
19 // scaled during dyanmic resizes (especially with devtools open).
20 [self setContentsGravity:kCAGravityTopLeft];
25 - (void)setContentsToData:(const void *)data
26 withRowBytes:(size_t)rowBytes
27 withPixelSize:(gfx::Size)pixelSize
28 withScaleFactor:(float)scaleFactor {
29 TRACE_EVENT0("browser", "-[SoftwareLayer setContentsToData]");
31 // Disable animating the contents change or the scale factor change.
32 ScopedCAActionDisabler disabler;
34 // Set the contents of the software CALayer to be a CGImage with the provided
35 // pixel data. Make a copy of the data before backing the image with them,
36 // because the same buffer will be reused for the next frame.
37 base::ScopedCFTypeRef<CFDataRef> dataCopy(
39 static_cast<const UInt8 *>(data),
40 rowBytes * pixelSize.height()));
41 base::ScopedCFTypeRef<CGDataProviderRef> dataProvider(
42 CGDataProviderCreateWithCFData(dataCopy));
43 base::ScopedCFTypeRef<CGImageRef> image(
44 CGImageCreate(pixelSize.width(),
49 base::mac::GetSystemColorSpace(),
50 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
54 kCGRenderingIntentDefault));
55 [self setContents:(id)image.get()];
56 [self setBounds:CGRectMake(
57 0, 0, pixelSize.width() / scaleFactor, pixelSize.height() / scaleFactor)];
59 // Set the contents scale of the software CALayer.
60 if ([self respondsToSelector:(@selector(contentsScale))] &&
61 [self respondsToSelector:(@selector(setContentsScale:))] &&
62 [self contentsScale] != scaleFactor) {
63 [self setContentsScale:scaleFactor];