Sync: Avoid deadlock in SyncBackendRegistrar / ModelSafeWorker on sync backend shutdown.
[chromium-blink-merge.git] / cc / resources / ui_resource_bitmap.cc
blobe254cf71614e8fdf73f2d576594c7fbcd5572644
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,
58 gfx::Size(info.fWidth, info.fHeight),
59 SkColorTypeToUIResourceFormat(skbitmap.colorType()));
61 SetOpaque(skbitmap.isOpaque());
64 UIResourceBitmap::UIResourceBitmap(const gfx::Size& size, bool is_opaque) {
65 SkAlphaType alphaType = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
66 SkImageInfo info =
67 SkImageInfo::MakeN32(size.width(), size.height(), alphaType);
68 skia::RefPtr<SkPixelRef> pixel_ref = skia::AdoptRef(
69 SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), NULL));
70 pixel_ref->setImmutable();
71 Create(pixel_ref, size, UIResourceBitmap::RGBA8);
72 SetOpaque(is_opaque);
75 UIResourceBitmap::UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref,
76 const gfx::Size& size) {
77 Create(pixel_ref, size, UIResourceBitmap::ETC1);
80 UIResourceBitmap::~UIResourceBitmap() {}
82 AutoLockUIResourceBitmap::AutoLockUIResourceBitmap(
83 const UIResourceBitmap& bitmap) : bitmap_(bitmap) {
84 bitmap_.pixel_ref_->lockPixels();
87 AutoLockUIResourceBitmap::~AutoLockUIResourceBitmap() {
88 bitmap_.pixel_ref_->unlockPixels();
91 const uint8_t* AutoLockUIResourceBitmap::GetPixels() const {
92 return static_cast<const uint8_t*>(bitmap_.pixel_ref_->pixels());
95 } // namespace cc