562e13a234ce09070aefac08791d994959e5d6b8
[platform/framework/web/crosswalk.git] / src / cc / resources / ui_resource_bitmap.cc
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
15 void UIResourceBitmap::Create(const skia::RefPtr<SkPixelRef>& pixel_ref,
16                               gfx::Size size,
17                               UIResourceFormat format) {
18   DCHECK(size.width());
19   DCHECK(size.height());
20   DCHECK(pixel_ref);
21   DCHECK(pixel_ref->isImmutable());
22   format_ = format;
23   size_ = size;
24   pixel_ref_ = pixel_ref;
25
26   // Default values for secondary parameters.
27   wrap_mode_ = CLAMP_TO_EDGE;
28   opaque_ = (format == ETC1);
29 }
30
31 UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) {
32   DCHECK_EQ(skbitmap.config(), SkBitmap::kARGB_8888_Config);
33   DCHECK_EQ(skbitmap.width(), skbitmap.rowBytesAsPixels());
34   DCHECK(skbitmap.isImmutable());
35
36   skia::RefPtr<SkPixelRef> pixel_ref = skia::SharePtr(skbitmap.pixelRef());
37   const SkImageInfo& info = pixel_ref->info();
38   Create(
39       pixel_ref, gfx::Size(info.fWidth, info.fHeight), UIResourceBitmap::RGBA8);
40
41   SetOpaque(skbitmap.isOpaque());
42 }
43
44 UIResourceBitmap::UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref,
45                                    gfx::Size size) {
46   Create(pixel_ref, size, UIResourceBitmap::ETC1);
47 }
48
49 UIResourceBitmap::~UIResourceBitmap() {}
50
51 AutoLockUIResourceBitmap::AutoLockUIResourceBitmap(
52     const UIResourceBitmap& bitmap) : bitmap_(bitmap) {
53   bitmap_.pixel_ref_->lockPixels();
54 }
55
56 AutoLockUIResourceBitmap::~AutoLockUIResourceBitmap() {
57   bitmap_.pixel_ref_->unlockPixels();
58 }
59
60 const uint8_t* AutoLockUIResourceBitmap::GetPixels() const {
61   return static_cast<const uint8_t*>(bitmap_.pixel_ref_->pixels());
62 }
63
64 }  // namespace cc