James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "cc/resources/ui_resource_bitmap.h" |
| 6 | |
| 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" |
| 12 | |
| 13 | namespace cc { |
| 14 | namespace { |
| 15 | |
| 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; |
| 29 | } |
| 30 | return format; |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 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; |
| 45 | |
| 46 | // Default values for secondary parameters. |
| 47 | wrap_mode_ = CLAMP_TO_EDGE; |
| 48 | opaque_ = (format == ETC1); |
| 49 | } |
| 50 | |
| 51 | UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) { |
| 52 | DCHECK_EQ(skbitmap.width(), skbitmap.rowBytesAsPixels()); |
| 53 | DCHECK(skbitmap.isImmutable()); |
| 54 | |
| 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())); |
| 60 | |
| 61 | SetOpaque(skbitmap.isOpaque()); |
| 62 | } |
| 63 | |
| 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); |
| 73 | } |
| 74 | |
| 75 | UIResourceBitmap::UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref, |
| 76 | const gfx::Size& size) { |
| 77 | Create(pixel_ref, size, UIResourceBitmap::ETC1); |
| 78 | } |
| 79 | |
| 80 | UIResourceBitmap::~UIResourceBitmap() {} |
| 81 | |
| 82 | AutoLockUIResourceBitmap::AutoLockUIResourceBitmap( |
| 83 | const UIResourceBitmap& bitmap) : bitmap_(bitmap) { |
| 84 | bitmap_.pixel_ref_->lockPixels(); |
| 85 | } |
| 86 | |
| 87 | AutoLockUIResourceBitmap::~AutoLockUIResourceBitmap() { |
| 88 | bitmap_.pixel_ref_->unlockPixels(); |
| 89 | } |
| 90 | |
| 91 | const uint8_t* AutoLockUIResourceBitmap::GetPixels() const { |
| 92 | return static_cast<const uint8_t*>(bitmap_.pixel_ref_->pixels()); |
| 93 | } |
| 94 | |
| 95 | } // namespace cc |