blob: 995937ac60cb752ec53d5e2ebef7e15ac900955d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
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"
twiz@google.com76b82742011-06-02 20:30:02 +000012#include "SkDevice.h"
junov@google.com869d6d92011-05-17 15:47:10 +000013
14namespace {
15SkBitmap make_bitmap() {
16 SkBitmap bm;
twiz@google.com76b82742011-06-02 20:30:02 +000017 bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5);
junov@google.com869d6d92011-05-17 15:47:10 +000018 bm.allocPixels();
19
20 for (int y = 0; y < bm.height(); y++) {
21 uint32_t* p = bm.getAddr32(0, y);
22 for (int x = 0; x < bm.width(); x++) {
23 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
24 }
25 }
26 bm.unlockPixels();
27 return bm;
28}
29} // unnamed namespace
30
31class TextureDomainView : public SampleView {
32 SkBitmap fBM;
33
34public:
35 TextureDomainView(){
36 fBM = make_bitmap();
37 }
38
39protected:
40 // overrides from SkEventSink
41 virtual bool onQuery(SkEvent* evt) {
42 if (SampleCode::TitleQ(*evt)) {
junov@google.com1d329782011-07-28 20:10:09 +000043 SampleCode::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000044 return true;
45 }
46 return this->INHERITED::onQuery(evt);
47 }
48
49 virtual void onDrawContent(SkCanvas* canvas) {
50 SkIRect srcRect;
51 SkRect dstRect;
52 SkPaint paint;
53 paint.setFilterBitmap(true);
twiz@google.com76b82742011-06-02 20:30:02 +000054
55 // Test that bitmap draws from malloc-backed bitmaps respect
56 // the constrained texture domain.
57 srcRect.setXYWH(1, 1, 3, 3);
58 dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f);
junov@google.com869d6d92011-05-17 15:47:10 +000059 canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint);
twiz@google.com76b82742011-06-02 20:30:02 +000060
61 // Test that bitmap draws across separate devices also respect
62 // the constrainted texture domain.
63 // Note: GPU-backed bitmaps follow a different rendering path
64 // when copying from one GPU device to another.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000065 SkAutoTUnref<SkBaseDevice> secondDevice(canvas->createCompatibleDevice(
reed@google.comaf951c92011-06-16 19:10:39 +000066 SkBitmap::kARGB_8888_Config, 5, 5, true));
twiz@google.com76b82742011-06-02 20:30:02 +000067 SkCanvas secondCanvas(secondDevice.get());
68
69 srcRect.setXYWH(1, 1, 3, 3);
70 dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f);
71 secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint);
72
73 SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
74
75 srcRect.setXYWH(1, 1, 3, 3);
76 dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f);
77 canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000078
79 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000080 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000081 srcRect.setXYWH(1, 1, 3, 3);
82 dstRect.setXYWH(5.0f, 405.0f, 305.0f, 305.0f);
83 SkMaskFilter* mf = SkBlurMaskFilter::Create(
junov@google.comd935cfb2011-06-27 20:48:23 +000084 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000085 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
junov@google.com1d329782011-07-28 20:10:09 +000086 SkBlurMaskFilter::kHighQuality_BlurFlag |
87 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
junov@google.comd935cfb2011-06-27 20:48:23 +000088 paint.setMaskFilter(mf)->unref();
89 canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
90
epoger@google.com9ef2d832011-07-01 21:12:20 +000091 // Blur and a rotation + NULL src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000092 // This should not trigger the texture domain code
93 // but it will test a code path in SkGpuDevice::drawBitmap
94 // that handles blurs with rects transformed to non-
95 // orthogonal rects. It also tests the NULL src rect handling
rmistry@google.comae933ce2012-08-23 18:19:56 +000096 mf = SkBlurMaskFilter::Create(
junov@google.com1d329782011-07-28 20:10:09 +000097 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000098 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
junov@google.com1d329782011-07-28 20:10:09 +000099 SkBlurMaskFilter::kHighQuality_BlurFlag);
100 paint.setMaskFilter(mf)->unref();
101
junov@google.comd935cfb2011-06-27 20:48:23 +0000102 dstRect.setXYWH(-150.0f, -150.0f, 300.0f, 300.0f);
103 canvas->translate(550, 550);
104 canvas->rotate(45);
105 canvas->drawBitmapRect(fBM, NULL, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +0000106 }
107private:
108 typedef SkView INHERITED;
109};
110
111//////////////////////////////////////////////////////////////////////////////
112
113static SkView* MyFactory() { return new TextureDomainView; }
114static SkViewRegister reg(MyFactory);