Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / cc / resources / ui_resource_bitmap.cc
blobb5a84b93db215710e975e1553765ce3469a456e6
1 // Copyright 2013 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 "cc/resources/ui_resource_bitmap.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "third_party/skia/include/core/SkMallocPixelRef.h"
11 #include "third_party/skia/include/core/SkPixelRef.h"
13 namespace cc {
14 namespace {
16 UIResourceBitmap::UIResourceFormat SkColorTypeToUIResourceFormat(
17 SkColorType sk_type) {
18 UIResourceBitmap::UIResourceFormat format = UIResourceBitmap::RGBA8;
19 switch (sk_type) {
20 case kN32_SkColorType:
21 format = UIResourceBitmap::RGBA8;
22 break;
23 case kAlpha_8_SkColorType:
24 format = UIResourceBitmap::ALPHA_8;
25 break;
26 default:
27 NOTREACHED() << "Invalid SkColorType for UIResourceBitmap: " << sk_type;
28 break;
30 return format;
33 } // namespace
35 void UIResourceBitmap::Create(const skia::RefPtr<SkPixelRef>& pixel_ref,
36 const gfx::Size& size,
37 UIResourceFormat format) {
38 DCHECK(size.width());
39 DCHECK(size.height());
40 DCHECK(pixel_ref);
41 DCHECK(pixel_ref->isImmutable());
42 format_ = format;
43 size_ = size;
44 pixel_ref_ = pixel_ref;
46 // Default values for secondary parameters.
47 wrap_mode_ = CLAMP_TO_EDGE;
48 opaque_ = (format == ETC1);
51 UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) {
52 DCHECK_EQ(skbitmap.width(), skbitmap.rowBytesAsPixels());
53 DCHECK(skbitmap.isImmutable());
55 skia::RefPtr<SkPixelRef> pixel_ref = skia::SharePtr(skbitmap.pixelRef());
56 const SkImageInfo& info = pixel_ref->info();
57 Create(pixel_ref, gfx::Size(info.width(), info.height()),
58 SkColorTypeToUIResourceFormat(skbitmap.colorType()));
60 SetOpaque(skbitmap.isOpaque());
63 UIResourceBitmap::UIResourceBitmap(const gfx::Size& size, bool is_opaque) {
64 SkAlphaType alphaType = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
65 SkImageInfo info =
66 SkImageInfo::MakeN32(size.width(), size.height(), alphaType);
67 skia::RefPtr<SkPixelRef> pixel_ref = skia::AdoptRef(
68 SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), NULL));
69 pixel_ref->setImmutable();
70 Create(pixel_ref, size, UIResourceBitmap::RGBA8);
71 SetOpaque(is_opaque);
74 UIResourceBitmap::UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref,
75 const gfx::Size& size) {
76 Create(pixel_ref, size, UIResourceBitmap::ETC1);
79 UIResourceBitmap::~UIResourceBitmap() {}
81 AutoLockUIResourceBitmap::AutoLockUIResourceBitmap(
82 const UIResourceBitmap& bitmap) : bitmap_(bitmap) {
83 bitmap_.pixel_ref_->lockPixels();
86 AutoLockUIResourceBitmap::~AutoLockUIResourceBitmap() {
87 bitmap_.pixel_ref_->unlockPixels();
90 const uint8_t* AutoLockUIResourceBitmap::GetPixels() const {
91 return static_cast<const uint8_t*>(bitmap_.pixel_ref_->pixels());
94 } // namespace cc