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"
16 UIResourceBitmap::UIResourceFormat
SkColorTypeToUIResourceFormat(
17 SkColorType sk_type
) {
18 UIResourceBitmap::UIResourceFormat format
= UIResourceBitmap::RGBA8
;
20 case kN32_SkColorType
:
21 format
= UIResourceBitmap::RGBA8
;
23 case kAlpha_8_SkColorType
:
24 format
= UIResourceBitmap::ALPHA_8
;
27 NOTREACHED() << "Invalid SkColorType for UIResourceBitmap: " << sk_type
;
35 void UIResourceBitmap::Create(const skia::RefPtr
<SkPixelRef
>& pixel_ref
,
36 const gfx::Size
& size
,
37 UIResourceFormat format
) {
39 DCHECK(size
.height());
41 DCHECK(pixel_ref
->isImmutable());
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
;
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
);
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());