blob: de84c35ea4e7d90c5918af1f8322bef2e9621c1a [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"
9#include "SkCanvas.h"
twiz@google.com76b82742011-06-02 20:30:02 +000010#include "SkDevice.h"
junov@google.comd935cfb2011-06-27 20:48:23 +000011#include "SkBlurMaskFilter.h"
junov@google.com869d6d92011-05-17 15:47:10 +000012
13namespace {
14SkBitmap make_bitmap() {
15 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}
28} // unnamed namespace
29
30class TextureDomainView : public SampleView {
31 SkBitmap fBM;
32
33public:
34 TextureDomainView(){
35 fBM = make_bitmap();
36 }
37
38protected:
39 // overrides from SkEventSink
40 virtual bool onQuery(SkEvent* evt) {
41 if (SampleCode::TitleQ(*evt)) {
junov@google.com1d329782011-07-28 20:10:09 +000042 SampleCode::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000043 return true;
44 }
45 return this->INHERITED::onQuery(evt);
46 }
47
48 virtual void onDrawContent(SkCanvas* canvas) {
49 SkIRect srcRect;
50 SkRect dstRect;
51 SkPaint paint;
52 paint.setFilterBitmap(true);
twiz@google.com76b82742011-06-02 20:30:02 +000053
54 // Test that bitmap draws from malloc-backed bitmaps respect
55 // the constrained texture domain.
56 srcRect.setXYWH(1, 1, 3, 3);
57 dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f);
junov@google.com869d6d92011-05-17 15:47:10 +000058 canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint);
twiz@google.com76b82742011-06-02 20:30:02 +000059
60 // Test that bitmap draws across separate devices also respect
61 // the constrainted texture domain.
62 // Note: GPU-backed bitmaps follow a different rendering path
63 // when copying from one GPU device to another.
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000064 SkAutoTUnref<SkDevice> secondDevice(canvas->createCompatibleDevice(
reed@google.comaf951c92011-06-16 19:10:39 +000065 SkBitmap::kARGB_8888_Config, 5, 5, true));
twiz@google.com76b82742011-06-02 20:30:02 +000066 SkCanvas secondCanvas(secondDevice.get());
67
68 srcRect.setXYWH(1, 1, 3, 3);
69 dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f);
70 secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint);
71
72 SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
73
74 srcRect.setXYWH(1, 1, 3, 3);
75 dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f);
76 canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000077
78 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000079 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000080 srcRect.setXYWH(1, 1, 3, 3);
81 dstRect.setXYWH(5.0f, 405.0f, 305.0f, 305.0f);
82 SkMaskFilter* mf = SkBlurMaskFilter::Create(
83 5,
84 SkBlurMaskFilter::kNormal_BlurStyle,
junov@google.com1d329782011-07-28 20:10:09 +000085 SkBlurMaskFilter::kHighQuality_BlurFlag |
86 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
junov@google.comd935cfb2011-06-27 20:48:23 +000087 paint.setMaskFilter(mf)->unref();
88 canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
89
epoger@google.com9ef2d832011-07-01 21:12:20 +000090 // Blur and a rotation + NULL src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000091 // This should not trigger the texture domain code
92 // but it will test a code path in SkGpuDevice::drawBitmap
93 // that handles blurs with rects transformed to non-
94 // orthogonal rects. It also tests the NULL src rect handling
rmistry@google.comae933ce2012-08-23 18:19:56 +000095 mf = SkBlurMaskFilter::Create(
junov@google.com1d329782011-07-28 20:10:09 +000096 5,
97 SkBlurMaskFilter::kNormal_BlurStyle,
98 SkBlurMaskFilter::kHighQuality_BlurFlag);
99 paint.setMaskFilter(mf)->unref();
100
junov@google.comd935cfb2011-06-27 20:48:23 +0000101 dstRect.setXYWH(-150.0f, -150.0f, 300.0f, 300.0f);
102 canvas->translate(550, 550);
103 canvas->rotate(45);
104 canvas->drawBitmapRect(fBM, NULL, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +0000105 }
106private:
107 typedef SkView INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
112static SkView* MyFactory() { return new TextureDomainView; }
113static SkViewRegister reg(MyFactory);