blob: 854df6ecfd9f0ea904937104bd8ff2075b8e2074 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.orgab131672013-09-13 12:39:55 +00007
junov@google.com869d6d92011-05-17 15:47:10 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
junov@google.com869d6d92011-05-17 15:47:10 +000011#include "SkCanvas.h"
reed@google.com76f10a32014-02-05 15:32:21 +000012#include "SkSurface.h"
junov@google.com869d6d92011-05-17 15:47:10 +000013
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000014static SkBitmap make_bitmap() {
junov@google.com869d6d92011-05-17 15:47:10 +000015 SkBitmap bm;
twiz@google.com76b82742011-06-02 20:30:02 +000016 bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5);
junov@google.com869d6d92011-05-17 15:47:10 +000017 bm.allocPixels();
18
19 for (int y = 0; y < bm.height(); y++) {
20 uint32_t* p = bm.getAddr32(0, y);
21 for (int x = 0; x < bm.width(); x++) {
22 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
23 }
24 }
25 bm.unlockPixels();
26 return bm;
27}
junov@google.com869d6d92011-05-17 15:47:10 +000028
29class TextureDomainView : public SampleView {
30 SkBitmap fBM;
31
32public:
33 TextureDomainView(){
34 fBM = make_bitmap();
35 }
36
37protected:
38 // overrides from SkEventSink
39 virtual bool onQuery(SkEvent* evt) {
40 if (SampleCode::TitleQ(*evt)) {
junov@google.com1d329782011-07-28 20:10:09 +000041 SampleCode::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000042 return true;
43 }
44 return this->INHERITED::onQuery(evt);
45 }
46
47 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com76f10a32014-02-05 15:32:21 +000048 SkRect srcRect;
junov@google.com869d6d92011-05-17 15:47:10 +000049 SkRect dstRect;
50 SkPaint paint;
reed@google.com44699382013-10-31 17:28:30 +000051 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
twiz@google.com76b82742011-06-02 20:30:02 +000052
53 // Test that bitmap draws from malloc-backed bitmaps respect
54 // the constrained texture domain.
55 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000056 dstRect.setXYWH(5, 5, 305, 305);
57 canvas->drawBitmapRectToRect(fBM, &srcRect, dstRect, &paint);
twiz@google.com76b82742011-06-02 20:30:02 +000058
59 // Test that bitmap draws across separate devices also respect
60 // the constrainted texture domain.
61 // Note: GPU-backed bitmaps follow a different rendering path
62 // when copying from one GPU device to another.
reed@google.com76f10a32014-02-05 15:32:21 +000063 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
64 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
twiz@google.com76b82742011-06-02 20:30:02 +000065
66 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000067 dstRect.setXYWH(1, 1, 3, 3);
68 surface->getCanvas()->drawBitmapRectToRect(fBM, &srcRect, dstRect,
69 &paint);
twiz@google.com76b82742011-06-02 20:30:02 +000070
reed@google.com76f10a32014-02-05 15:32:21 +000071 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
twiz@google.com76b82742011-06-02 20:30:02 +000072
73 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000074 dstRect.setXYWH(405, 5, 305, 305);
75 image->draw(canvas, &srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000076
77 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000079 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000080 dstRect.setXYWH(5, 405, 305, 305);
junov@google.comd935cfb2011-06-27 20:48:23 +000081 SkMaskFilter* mf = SkBlurMaskFilter::Create(
junov@google.comd935cfb2011-06-27 20:48:23 +000082 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000083 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
junov@google.com1d329782011-07-28 20:10:09 +000084 SkBlurMaskFilter::kHighQuality_BlurFlag |
85 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
junov@google.comd935cfb2011-06-27 20:48:23 +000086 paint.setMaskFilter(mf)->unref();
reed@google.com76f10a32014-02-05 15:32:21 +000087 image->draw(canvas, &srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000088
epoger@google.com9ef2d832011-07-01 21:12:20 +000089 // Blur and a rotation + NULL src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000090 // This should not trigger the texture domain code
91 // but it will test a code path in SkGpuDevice::drawBitmap
92 // that handles blurs with rects transformed to non-
93 // orthogonal rects. It also tests the NULL src rect handling
reed@google.com76f10a32014-02-05 15:32:21 +000094 mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
95 SkBlurMask::ConvertRadiusToSigma(5),
96 SkBlurMaskFilter::kHighQuality_BlurFlag);
junov@google.com1d329782011-07-28 20:10:09 +000097 paint.setMaskFilter(mf)->unref();
98
reed@google.com76f10a32014-02-05 15:32:21 +000099 dstRect.setXYWH(-150, -150, 300, 300);
junov@google.comd935cfb2011-06-27 20:48:23 +0000100 canvas->translate(550, 550);
101 canvas->rotate(45);
reed@google.com76f10a32014-02-05 15:32:21 +0000102 canvas->drawBitmapRectToRect(fBM, NULL, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +0000103 }
104private:
105 typedef SkView INHERITED;
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static SkView* MyFactory() { return new TextureDomainView; }
111static SkViewRegister reg(MyFactory);