Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: app/gfx/chrome_canvas.cc

Issue 113443: ChromeCanvas->gfx::Canvas (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « app/gfx/chrome_canvas.h ('k') | app/gfx/chrome_canvas_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_canvas.h"
6
7 #include <limits>
8
9 #include "app/gfx/chrome_font.h"
10 #include "app/l10n_util.h"
11 #include "base/gfx/rect.h"
12 #include "base/logging.h"
13 #include "third_party/skia/include/core/SkShader.h"
14
15 bool ChromeCanvas::GetClipRect(gfx::Rect* r) {
16 SkRect clip;
17 if (!getClipBounds(&clip)) {
18 if (r)
19 r->SetRect(0, 0, 0, 0);
20 return false;
21 }
22 r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop),
23 SkScalarRound(clip.fRight - clip.fLeft),
24 SkScalarRound(clip.fBottom - clip.fTop));
25 return true;
26 }
27
28 bool ChromeCanvas::ClipRectInt(int x, int y, int w, int h) {
29 SkRect new_clip;
30 new_clip.set(SkIntToScalar(x), SkIntToScalar(y),
31 SkIntToScalar(x + w), SkIntToScalar(y + h));
32 return clipRect(new_clip);
33 }
34
35 bool ChromeCanvas::IntersectsClipRectInt(int x, int y, int w, int h) {
36 SkRect clip;
37 return getClipBounds(&clip) &&
38 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
39 SkIntToScalar(y + h));
40 }
41
42 void ChromeCanvas::TranslateInt(int x, int y) {
43 translate(SkIntToScalar(x), SkIntToScalar(y));
44 }
45
46 void ChromeCanvas::ScaleInt(int x, int y) {
47 scale(SkIntToScalar(x), SkIntToScalar(y));
48 }
49
50 void ChromeCanvas::FillRectInt(const SkColor& color,
51 int x, int y, int w, int h) {
52 SkPaint paint;
53 paint.setColor(color);
54 paint.setStyle(SkPaint::kFill_Style);
55 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode);
56 FillRectInt(x, y, w, h, paint);
57 }
58
59 void ChromeCanvas::FillRectInt(int x, int y, int w, int h,
60 const SkPaint& paint) {
61 SkIRect rc = {x, y, x + w, y + h};
62 drawIRect(rc, paint);
63 }
64
65 void ChromeCanvas::DrawRectInt(const SkColor& color,
66 int x, int y, int w, int h) {
67 DrawRectInt(color, x, y, w, h, SkPorterDuff::kSrcOver_Mode);
68 }
69
70 void ChromeCanvas::DrawRectInt(const SkColor& color,
71 int x, int y, int w, int h,
72 SkPorterDuff::Mode mode) {
73 SkPaint paint;
74 paint.setColor(color);
75 paint.setStyle(SkPaint::kStroke_Style);
76 // Set a stroke width of 0, which will put us down the stroke rect path. If
77 // we set a stroke width of 1, for example, this will internally create a
78 // path and fill it, which causes problems near the edge of the canvas.
79 paint.setStrokeWidth(SkIntToScalar(0));
80 paint.setPorterDuffXfermode(mode);
81
82 SkIRect rc = {x, y, x + w, y + h};
83 drawIRect(rc, paint);
84 }
85
86 void ChromeCanvas::DrawLineInt(const SkColor& color,
87 int x1, int y1, int x2, int y2) {
88 SkPaint paint;
89 paint.setColor(color);
90 paint.setStrokeWidth(SkIntToScalar(1));
91 drawLine(SkIntToScalar(x1), SkIntToScalar(y1), SkIntToScalar(x2),
92 SkIntToScalar(y2), paint);
93 }
94
95 void ChromeCanvas::DrawFocusRect(int x, int y, int width, int height) {
96 // Create a 2D bitmap containing alternating on/off pixels - we do this
97 // so that you never get two pixels of the same color around the edges
98 // of the focus rect (this may mean that opposing edges of the rect may
99 // have a dot pattern out of phase to each other).
100 static SkBitmap* dots = NULL;
101 if (!dots) {
102 int col_pixels = 32;
103 int row_pixels = 32;
104
105 dots = new SkBitmap;
106 dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels);
107 dots->allocPixels();
108 dots->eraseARGB(0, 0, 0, 0);
109
110 uint32_t* dot = dots->getAddr32(0, 0);
111 for (int i = 0; i < row_pixels; i++) {
112 for (int u = 0; u < col_pixels; u++) {
113 if ((u % 2 + i % 2) % 2 != 0) {
114 dot[i * row_pixels + u] = SK_ColorGRAY;
115 }
116 }
117 }
118 }
119
120 // First the horizontal lines.
121
122 // Make a shader for the bitmap with an origin of the box we'll draw. This
123 // shader is refcounted and will have an initial refcount of 1.
124 SkShader* shader = SkShader::CreateBitmapShader(
125 *dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
126 // Assign the shader to the paint & release our reference. The paint will
127 // now own the shader and the shader will be destroyed when the paint goes
128 // out of scope.
129 SkPaint paint;
130 paint.setShader(shader);
131 shader->unref();
132
133 SkRect rect;
134 rect.set(SkIntToScalar(x), SkIntToScalar(y),
135 SkIntToScalar(x + width), SkIntToScalar(y + 1));
136 drawRect(rect, paint);
137 rect.set(SkIntToScalar(x), SkIntToScalar(y + height - 1),
138 SkIntToScalar(x + width), SkIntToScalar(y + height));
139 drawRect(rect, paint);
140
141 rect.set(SkIntToScalar(x), SkIntToScalar(y),
142 SkIntToScalar(x + 1), SkIntToScalar(y + height));
143 drawRect(rect, paint);
144 rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y),
145 SkIntToScalar(x + width), SkIntToScalar(y + height));
146 drawRect(rect, paint);
147 }
148
149 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) {
150 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y));
151 }
152
153 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y,
154 const SkPaint& paint) {
155 drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
156 }
157
158 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y,
159 int src_w, int src_h, int dest_x, int dest_y,
160 int dest_w, int dest_h,
161 bool filter) {
162 SkPaint p;
163 DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y,
164 dest_w, dest_h, filter, p);
165 }
166
167 void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y,
168 int src_w, int src_h, int dest_x, int dest_y,
169 int dest_w, int dest_h,
170 bool filter, const SkPaint& paint) {
171 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
172 src_y + src_h < std::numeric_limits<int16_t>::max());
173 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) {
174 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!";
175 return;
176 }
177
178 if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
179 return;
180
181 SkRect dest_rect = { SkIntToScalar(dest_x),
182 SkIntToScalar(dest_y),
183 SkIntToScalar(dest_x + dest_w),
184 SkIntToScalar(dest_y + dest_h) };
185
186 if (src_w == dest_w && src_h == dest_h) {
187 // Workaround for apparent bug in Skia that causes image to occasionally
188 // shift.
189 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
190 drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
191 return;
192 }
193
194 // Make a bitmap shader that contains the bitmap we want to draw. This is
195 // basically what SkCanvas.drawBitmap does internally, but it gives us
196 // more control over quality and will use the mipmap in the source image if
197 // it has one, whereas drawBitmap won't.
198 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
199 SkShader::kRepeat_TileMode,
200 SkShader::kRepeat_TileMode);
201 SkMatrix shader_scale;
202 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w),
203 SkFloatToScalar(static_cast<float>(dest_h) / src_h));
204 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
205 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
206 shader->setLocalMatrix(shader_scale);
207
208 // Set up our paint to use the shader & release our reference (now just owned
209 // by the paint).
210 SkPaint p(paint);
211 p.setFilterBitmap(filter);
212 p.setShader(shader);
213 shader->unref();
214
215 // The rect will be filled by the bitmap.
216 drawRect(dest_rect, p);
217 }
218
219 void ChromeCanvas::DrawStringInt(const std::wstring& text,
220 const gfx::Font& font,
221 const SkColor& color,
222 int x, int y,
223 int w, int h) {
224 DrawStringInt(text, font, color, x, y, w, h,
225 l10n_util::DefaultCanvasTextAlignment());
226 }
227
228 void ChromeCanvas::TileImageInt(const SkBitmap& bitmap,
229 int x, int y, int w, int h) {
230 TileImageInt(bitmap, 0, 0, x, y, w, h);
231 }
232
233 void ChromeCanvas::TileImageInt(const SkBitmap& bitmap, int src_x, int src_y,
234 int dest_x, int dest_y, int w, int h) {
235 if (!IntersectsClipRectInt(dest_x, dest_y, w, h))
236 return;
237
238 SkPaint paint;
239
240 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
241 SkShader::kRepeat_TileMode,
242 SkShader::kRepeat_TileMode);
243 paint.setShader(shader);
244 paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode);
245
246 // CreateBitmapShader returns a Shader with a reference count of one, we
247 // need to unref after paint takes ownership of the shader.
248 shader->unref();
249 save();
250 translate(SkIntToScalar(dest_x - src_x), SkIntToScalar(dest_y - src_y));
251 ClipRectInt(src_x, src_y, w, h);
252 drawPaint(paint);
253 restore();
254 }
255
256 SkBitmap ChromeCanvas::ExtractBitmap() {
257 const SkBitmap& device_bitmap = getDevice()->accessBitmap(false);
258
259 // Make a bitmap to return, and a canvas to draw into it. We don't just want
260 // to call extractSubset or the copy constuctor, since we want an actual copy
261 // of the bitmap.
262 SkBitmap result;
263 device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
264 return result;
265 }
OLDNEW
« no previous file with comments | « app/gfx/chrome_canvas.h ('k') | app/gfx/chrome_canvas_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698