summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/software_layer_mac.mm
blob: 896e3d25595aa600684a37ff5ed9cb09e202de11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/software_layer_mac.h"

#include "base/debug/trace_event.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/sdk_forward_declarations.h"
#include "ui/base/cocoa/animation_utils.h"

@implementation SoftwareLayer

- (id)init {
  if (self = [super init]) {
    [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
    [self setAnchorPoint:CGPointMake(0, 0)];
    // Setting contents gravity is necessary to prevent the layer from being
    // scaled during dyanmic resizes (especially with devtools open).
    [self setContentsGravity:kCAGravityTopLeft];
  }
  return self;
}

- (void)setContentsToData:(const void *)data
             withRowBytes:(size_t)rowBytes
            withPixelSize:(gfx::Size)pixelSize
          withScaleFactor:(float)scaleFactor {
  TRACE_EVENT0("browser", "-[SoftwareLayer setContentsToData]");

  // Disable animating the contents change or the scale factor change.
  ScopedCAActionDisabler disabler;

  // Set the contents of the software CALayer to be a CGImage with the provided
  // pixel data. Make a copy of the data before backing the image with them,
  // because the same buffer will be reused for the next frame.
  base::ScopedCFTypeRef<CFDataRef> dataCopy(
      CFDataCreate(NULL,
                   static_cast<const UInt8 *>(data),
                   rowBytes * pixelSize.height()));
  base::ScopedCFTypeRef<CGDataProviderRef> dataProvider(
      CGDataProviderCreateWithCFData(dataCopy));
  base::ScopedCFTypeRef<CGImageRef> image(
      CGImageCreate(pixelSize.width(),
                    pixelSize.height(),
                    8,
                    32,
                    rowBytes,
                    base::mac::GetSystemColorSpace(),
                    kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
                    dataProvider,
                    NULL,
                    false,
                    kCGRenderingIntentDefault));
  [self setContents:(id)image.get()];

  // Set the contents scale of the software CALayer.
  if ([self respondsToSelector:(@selector(contentsScale))] &&
      [self respondsToSelector:(@selector(setContentsScale:))] &&
      [self contentsScale] != scaleFactor) {
    [self setContentsScale:scaleFactor];
  }
}

@end