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();
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
;
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
);
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());