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"
15 void UIResourceBitmap::Create(const skia::RefPtr
<SkPixelRef
>& pixel_ref
,
16 const gfx::Size
& size
,
17 UIResourceFormat format
) {
19 DCHECK(size
.height());
21 DCHECK(pixel_ref
->isImmutable());
24 pixel_ref_
= pixel_ref
;
26 // Default values for secondary parameters.
27 wrap_mode_
= CLAMP_TO_EDGE
;
28 opaque_
= (format
== ETC1
);
31 UIResourceBitmap::UIResourceBitmap(const SkBitmap
& skbitmap
) {
32 DCHECK_EQ(skbitmap
.colorType(), kPMColor_SkColorType
);
33 DCHECK_EQ(skbitmap
.width(), skbitmap
.rowBytesAsPixels());
34 DCHECK(skbitmap
.isImmutable());
36 skia::RefPtr
<SkPixelRef
> pixel_ref
= skia::SharePtr(skbitmap
.pixelRef());
37 const SkImageInfo
& info
= pixel_ref
->info();
39 pixel_ref
, gfx::Size(info
.fWidth
, info
.fHeight
), UIResourceBitmap::RGBA8
);
41 SetOpaque(skbitmap
.isOpaque());
44 UIResourceBitmap::UIResourceBitmap(const gfx::Size
& size
, bool is_opaque
) {
45 SkAlphaType alphaType
= is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
;
47 SkImageInfo::MakeN32(size
.width(), size
.height(), alphaType
);
48 skia::RefPtr
<SkPixelRef
> pixel_ref
= skia::AdoptRef(
49 SkMallocPixelRef::NewAllocate(info
, info
.minRowBytes(), NULL
));
50 pixel_ref
->setImmutable();
51 Create(pixel_ref
, size
, UIResourceBitmap::RGBA8
);
55 UIResourceBitmap::UIResourceBitmap(const skia::RefPtr
<SkPixelRef
>& pixel_ref
,
56 const gfx::Size
& size
) {
57 Create(pixel_ref
, size
, UIResourceBitmap::ETC1
);
60 UIResourceBitmap::~UIResourceBitmap() {}
62 AutoLockUIResourceBitmap::AutoLockUIResourceBitmap(
63 const UIResourceBitmap
& bitmap
) : bitmap_(bitmap
) {
64 bitmap_
.pixel_ref_
->lockPixels();
67 AutoLockUIResourceBitmap::~AutoLockUIResourceBitmap() {
68 bitmap_
.pixel_ref_
->unlockPixels();
71 const uint8_t* AutoLockUIResourceBitmap::GetPixels() const {
72 return static_cast<const uint8_t*>(bitmap_
.pixel_ref_
->pixels());