forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_resource_bitmap.cc
123 lines (106 loc) · 3.99 KB
/
ui_resource_bitmap.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/resources/ui_resource_bitmap.h"
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/checked_math.h"
#include "build/build_config.h"
#include "gpu/config/gpu_finch_features.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkMallocPixelRef.h"
#include "third_party/skia/include/core/SkPixelRef.h"
namespace cc {
namespace {
UIResourceBitmap::UIResourceFormat SkColorTypeToUIResourceFormat(
SkColorType sk_type) {
UIResourceBitmap::UIResourceFormat format = UIResourceBitmap::RGBA8;
switch (sk_type) {
case kN32_SkColorType:
format = UIResourceBitmap::RGBA8;
break;
case kAlpha_8_SkColorType:
format = UIResourceBitmap::ALPHA_8;
break;
default:
NOTREACHED() << "Invalid SkColorType for UIResourceBitmap: " << sk_type;
break;
}
return format;
}
} // namespace
void UIResourceBitmap::Create(sk_sp<SkPixelRef> pixel_ref,
const SkImageInfo& info,
UIResourceFormat format) {
DCHECK(info.width());
DCHECK(info.height());
DCHECK(pixel_ref);
DCHECK(pixel_ref->isImmutable());
format_ = format;
info_ = info;
pixel_ref_ = std::move(pixel_ref);
}
void UIResourceBitmap::DrawToCanvas(SkCanvas* canvas, SkPaint* paint) {
DCHECK_NE(info_.colorType(), kUnknown_SkColorType);
SkBitmap bitmap;
bitmap.setInfo(info_, pixel_ref_.get()->rowBytes());
bitmap.setPixelRef(pixel_ref_, 0, 0);
canvas->drawImage(bitmap.asImage(), 0, 0, SkSamplingOptions(), paint);
canvas->flush();
}
size_t UIResourceBitmap::SizeInBytes() const {
if (!pixel_ref_)
return 0u;
base::CheckedNumeric<size_t> size_in_bytes = pixel_ref_->rowBytes();
size_in_bytes *= info_.height();
return size_in_bytes.ValueOrDie();
}
UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) {
DCHECK(skbitmap.isImmutable());
const SkBitmap* target = &skbitmap;
#if defined(OS_ANDROID)
SkBitmap copy;
if (features::IsDrDcEnabled()) {
// TODO(vikassoni): Forcing everything to N32 while android backing cannot
// support some other formats.
if (skbitmap.colorType() != kN32_SkColorType) {
SkImageInfo new_info = skbitmap.info().makeColorType(kN32_SkColorType);
copy.allocPixels(new_info, new_info.minRowBytes());
SkCanvas copy_canvas(copy);
copy_canvas.drawImage(skbitmap.asImage(), 0, 0, SkSamplingOptions(),
nullptr);
copy.setImmutable();
target = ©
}
DCHECK_EQ(target->width(), target->rowBytesAsPixels());
DCHECK(target->isImmutable());
}
#endif
sk_sp<SkPixelRef> pixel_ref = sk_ref_sp(target->pixelRef());
Create(std::move(pixel_ref), target->info(),
SkColorTypeToUIResourceFormat(target->colorType()));
}
UIResourceBitmap::UIResourceBitmap(const gfx::Size& size, bool is_opaque) {
SkAlphaType alphaType = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
SkImageInfo info =
SkImageInfo::MakeN32(size.width(), size.height(), alphaType);
sk_sp<SkPixelRef> pixel_ref(
SkMallocPixelRef::MakeAllocate(info, info.minRowBytes()));
pixel_ref->setImmutable();
Create(std::move(pixel_ref), info, UIResourceBitmap::RGBA8);
}
UIResourceBitmap::UIResourceBitmap(sk_sp<SkPixelRef> pixel_ref,
const gfx::Size& size) {
// TODO(khushalsagar): It doesn't make sense to SkPixelRef to pass around
// encoded data.
SkImageInfo info = SkImageInfo::Make(
size.width(), size.height(), kUnknown_SkColorType, kOpaque_SkAlphaType);
Create(std::move(pixel_ref), info, UIResourceBitmap::ETC1);
}
UIResourceBitmap::UIResourceBitmap(const UIResourceBitmap& other) = default;
UIResourceBitmap::~UIResourceBitmap() = default;
} // namespace cc