1 /*
2  * Copyright 2015 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  */
7 
8 #include "SkHwuiRenderer.h"
9 
10 #include "AnimationContext.h"
11 #include "IContextFactory.h"
12 #include "SkBitmap.h"
13 #include "gui/BufferQueue.h"
14 
15 namespace {
16 
17 /**
18  * Helper class for setting up android::uirenderer::renderthread::RenderProxy.
19  */
20 class ContextFactory : public android::uirenderer::IContextFactory {
21 public:
createAnimationContext(android::uirenderer::renderthread::TimeLord & clock)22     android::uirenderer::AnimationContext* createAnimationContext
23         (android::uirenderer::renderthread::TimeLord& clock) override {
24         return new android::uirenderer::AnimationContext(clock);
25     }
26 };
27 
28 }
29 
initialize(SkISize size)30 void SkHwuiRenderer::initialize(SkISize size) {
31     this->size = size;
32     android::BufferQueue::createBufferQueue(&this->producer, &this->consumer);
33     this->cpuConsumer = new android::CpuConsumer(this->consumer, 1);
34     this->cpuConsumer->setName(android::String8("SkiaBenchmarkClient"));
35     this->cpuConsumer->setDefaultBufferSize(size.width(), size.height());
36     this->androidSurface = new android::Surface(this->producer);
37     native_window_set_buffers_dimensions(this->androidSurface.get(),
38                                          size.width(), size.height());
39     native_window_set_buffers_format(this->androidSurface.get(),
40                                      android::PIXEL_FORMAT_RGBA_8888);
41     native_window_set_usage(this->androidSurface.get(), GRALLOC_USAGE_SW_READ_OFTEN |
42                                            GRALLOC_USAGE_SW_WRITE_NEVER |
43                                            GRALLOC_USAGE_HW_RENDER);
44     this->rootNode.reset(new android::uirenderer::RenderNode());
45     this->rootNode->incStrong(nullptr);
46     this->rootNode->mutateStagingProperties().setLeftTopRightBottom
47         (0, 0, size.width(), size.height());
48     this->rootNode->mutateStagingProperties().setClipToBounds(false);
49     this->rootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENERIC);
50     ContextFactory factory;
51     this->proxy.reset
52         (new android::uirenderer::renderthread::RenderProxy(false, this->rootNode, &factory));
53     this->proxy->loadSystemProperties();
54     this->proxy->initialize(this->androidSurface.get());
55     float lightX = size.width() / 2.0f;
56     android::uirenderer::Vector3 lightVector { lightX, -200.0f, 800.0f };
57     this->proxy->setup(size.width(), size.height(), 800.0f,
58                          255 * 0.075f, 255 * 0.15f);
59     this->proxy->setLightCenter(lightVector);
60     this->canvas.reset(new android::uirenderer::DisplayListCanvas());
61     this->canvas->setViewport(size.width(), size.height());
62 }
63 
prepareToDraw()64 SkCanvas* SkHwuiRenderer::prepareToDraw() {
65     this->canvas->prepare();
66     this->canvas->clipRect(0, 0, this->size.width(), this->size.height(),
67                            SkRegion::Op::kReplace_Op);
68     return this->canvas->asSkCanvas();
69 }
70 
finishDrawing()71 void SkHwuiRenderer::finishDrawing() {
72     this->canvas->finish();
73     this->rootNode->setStagingDisplayList(this->canvas->finishRecording());
74     this->proxy->syncAndDrawFrame();
75     // Surprisingly, calling this->proxy->fence() here appears to make no difference to
76     // the timings we record.
77 }
78 
capturePixels(SkBitmap * bmp)79 bool SkHwuiRenderer::capturePixels(SkBitmap* bmp) {
80     SkImageInfo destinationConfig =
81         SkImageInfo::Make(this->size.width(), this->size.height(),
82                           kRGBA_8888_SkColorType, kPremul_SkAlphaType);
83     bmp->allocPixels(destinationConfig);
84     sk_memset32((uint32_t*) bmp->getPixels(), SK_ColorRED,
85                 this->size.width() * this->size.height());
86 
87     android::CpuConsumer::LockedBuffer nativeBuffer;
88     android::status_t retval = this->cpuConsumer->lockNextBuffer(&nativeBuffer);
89     if (retval == android::BAD_VALUE) {
90         SkDebugf("write_canvas_png() got no buffer; returning transparent");
91         // No buffer ready to read - commonly triggered by dm sending us
92         // a no-op source, or calling code that doesn't do anything on this
93         // backend.
94         bmp->eraseColor(SK_ColorTRANSPARENT);
95         return false;
96     } else if (retval) {
97         SkDebugf("Failed to lock buffer to read pixels: %d.", retval);
98         return false;
99     }
100 
101     // Move the pixels into the destination SkBitmap
102 
103     SK_ALWAYSBREAK(nativeBuffer.format == android::PIXEL_FORMAT_RGBA_8888 &&
104                    "Native buffer not RGBA!");
105     SkImageInfo nativeConfig =
106         SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height,
107                           kRGBA_8888_SkColorType, kPremul_SkAlphaType);
108 
109     // Android stride is in pixels, Skia stride is in bytes
110     SkBitmap nativeWrapper;
111     bool success =
112         nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffer.stride * 4);
113     if (!success) {
114         SkDebugf("Failed to wrap HWUI buffer in a SkBitmap");
115         return false;
116     }
117 
118     SK_ALWAYSBREAK(bmp->colorType() == kRGBA_8888_SkColorType &&
119                    "Destination buffer not RGBA!");
120     success =
121         nativeWrapper.readPixels(destinationConfig, bmp->getPixels(), bmp->rowBytes(), 0, 0);
122     if (!success) {
123         SkDebugf("Failed to extract pixels from HWUI buffer");
124         return false;
125     }
126 
127     this->cpuConsumer->unlockBuffer(nativeBuffer);
128 
129     return true;
130 }
131 
132