Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / ui / accelerated_widget_mac / software_layer.mm
blobabdef16905920b319234050aa2517d1d08134878
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
15 - (id)init {
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];
21   }
22   return self;
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(
38       CFDataCreate(NULL,
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(),
45                     pixelSize.height(),
46                     8,
47                     32,
48                     rowBytes,
49                     base::mac::GetSystemColorSpace(),
50                     kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
51                     dataProvider,
52                     NULL,
53                     false,
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];
64   }
67 @end