blob: 624d20763384d292d630e39df198087da3239f34 [file] [log] [blame]
Tom Hudsonb2f5bd22015-10-15 16:41:55 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "TestWindowContext.h"
17
18#include "AnimationContext.h"
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040019#include "IContextFactory.h"
Derek Sollenberger3d5278b2016-01-14 14:22:53 -050020#include "RecordingCanvas.h"
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040021#include "RenderNode.h"
22#include "SkTypes.h"
23#include "gui/BufferQueue.h"
24#include "gui/CpuConsumer.h"
25#include "gui/IGraphicBufferConsumer.h"
26#include "gui/IGraphicBufferProducer.h"
27#include "gui/Surface.h"
28#include "renderthread/RenderProxy.h"
29
Ben Wagnered87fa22016-02-19 17:56:44 -050030#include <cutils/memory.h>
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040031
32namespace {
33
34/**
35 * Helper class for setting up android::uirenderer::renderthread::RenderProxy.
36 */
37class ContextFactory : public android::uirenderer::IContextFactory {
38public:
39 android::uirenderer::AnimationContext* createAnimationContext
40 (android::uirenderer::renderthread::TimeLord& clock) override {
41 return new android::uirenderer::AnimationContext(clock);
42 }
43};
44
45} // anonymous namespace
46
47namespace android {
48namespace uirenderer {
49
50/**
51 Android strong pointers (android::sp) can't hold forward-declared classes,
52 so we have to use pointer-to-implementation here if we want to hide the
53 details from our non-framework users.
54*/
55
56class TestWindowContext::TestWindowData {
57
58public:
59
Chih-Hung Hsiehd53e3be2016-05-03 10:02:51 -070060 explicit TestWindowData(SkISize size) : mSize(size) {
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040061 android::BufferQueue::createBufferQueue(&mProducer, &mConsumer);
62 mCpuConsumer = new android::CpuConsumer(mConsumer, 1);
63 mCpuConsumer->setName(android::String8("TestWindowContext"));
64 mCpuConsumer->setDefaultBufferSize(mSize.width(), mSize.height());
65 mAndroidSurface = new android::Surface(mProducer);
66 native_window_set_buffers_dimensions(mAndroidSurface.get(),
67 mSize.width(), mSize.height());
68 native_window_set_buffers_format(mAndroidSurface.get(),
69 android::PIXEL_FORMAT_RGBA_8888);
70 native_window_set_usage(mAndroidSurface.get(),
71 GRALLOC_USAGE_SW_READ_OFTEN |
72 GRALLOC_USAGE_SW_WRITE_NEVER |
73 GRALLOC_USAGE_HW_RENDER);
74 mRootNode.reset(new android::uirenderer::RenderNode());
75 mRootNode->incStrong(nullptr);
76 mRootNode->mutateStagingProperties().setLeftTopRightBottom
77 (0, 0, mSize.width(), mSize.height());
78 mRootNode->mutateStagingProperties().setClipToBounds(false);
79 mRootNode->setPropertyFieldsDirty(android::uirenderer::RenderNode::GENERIC);
80 ContextFactory factory;
81 mProxy.reset
82 (new android::uirenderer::renderthread::RenderProxy(false,
83 mRootNode.get(),
84 &factory));
85 mProxy->loadSystemProperties();
86 mProxy->initialize(mAndroidSurface.get());
87 float lightX = mSize.width() / 2.0f;
88 android::uirenderer::Vector3 lightVector { lightX, -200.0f, 800.0f };
John Reckab1080c2016-06-21 16:24:20 -070089 mProxy->setup(800.0f, 255 * 0.075f, 255 * 0.15f);
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040090 mProxy->setLightCenter(lightVector);
Derek Sollenberger3d5278b2016-01-14 14:22:53 -050091 mCanvas.reset(new android::uirenderer::RecordingCanvas(mSize.width(), mSize.height()));
Tom Hudsonb2f5bd22015-10-15 16:41:55 -040092 }
93
94 SkCanvas* prepareToDraw() {
95 //mCanvas->reset(mSize.width(), mSize.height());
96 mCanvas->clipRect(0, 0, mSize.width(), mSize.height(),
97 SkRegion::Op::kReplace_Op);
98 return mCanvas->asSkCanvas();
99 }
100
101 void finishDrawing() {
John Reck51f2d602016-04-06 07:50:47 -0700102 mRootNode->setStagingDisplayList(mCanvas->finishRecording(), nullptr);
103 mProxy->syncAndDrawFrame(nullptr);
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400104 // Surprisingly, calling mProxy->fence() here appears to make no difference to
105 // the timings we record.
106 }
107
108 void fence() {
109 mProxy->fence();
110 }
111
112 bool capturePixels(SkBitmap* bmp) {
Romain Guy253f2c22016-09-28 17:34:42 -0700113 sk_sp<SkColorSpace> colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400114 SkImageInfo destinationConfig =
115 SkImageInfo::Make(mSize.width(), mSize.height(),
Romain Guy253f2c22016-09-28 17:34:42 -0700116 kRGBA_8888_SkColorType, kPremul_SkAlphaType, colorSpace);
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400117 bmp->allocPixels(destinationConfig);
Ben Wagnered87fa22016-02-19 17:56:44 -0500118 android_memset32((uint32_t*) bmp->getPixels(), SK_ColorRED,
119 mSize.width() * mSize.height() * 4);
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400120
121 android::CpuConsumer::LockedBuffer nativeBuffer;
122 android::status_t retval = mCpuConsumer->lockNextBuffer(&nativeBuffer);
123 if (retval == android::BAD_VALUE) {
124 SkDebugf("write_canvas_png() got no buffer; returning transparent");
125 // No buffer ready to read - commonly triggered by dm sending us
126 // a no-op source, or calling code that doesn't do anything on this
127 // backend.
128 bmp->eraseColor(SK_ColorTRANSPARENT);
129 return false;
130 } else if (retval) {
131 SkDebugf("Failed to lock buffer to read pixels: %d.", retval);
132 return false;
133 }
134
135 // Move the pixels into the destination SkBitmap
136
Derek Sollenbergera3f16d42016-01-25 10:29:35 -0500137 LOG_ALWAYS_FATAL_IF(nativeBuffer.format != android::PIXEL_FORMAT_RGBA_8888,
138 "Native buffer not RGBA!");
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400139 SkImageInfo nativeConfig =
140 SkImageInfo::Make(nativeBuffer.width, nativeBuffer.height,
141 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
142
143 // Android stride is in pixels, Skia stride is in bytes
144 SkBitmap nativeWrapper;
145 bool success =
146 nativeWrapper.installPixels(nativeConfig, nativeBuffer.data, nativeBuffer.stride * 4);
147 if (!success) {
148 SkDebugf("Failed to wrap HWUI buffer in a SkBitmap");
149 return false;
150 }
151
Derek Sollenbergera3f16d42016-01-25 10:29:35 -0500152 LOG_ALWAYS_FATAL_IF(bmp->colorType() != kRGBA_8888_SkColorType,
153 "Destination buffer not RGBA!");
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400154 success =
155 nativeWrapper.readPixels(destinationConfig, bmp->getPixels(), bmp->rowBytes(), 0, 0);
156 if (!success) {
157 SkDebugf("Failed to extract pixels from HWUI buffer");
158 return false;
159 }
160
161 mCpuConsumer->unlockBuffer(nativeBuffer);
162
163 return true;
164 }
165
166private:
167
168 std::unique_ptr<android::uirenderer::RenderNode> mRootNode;
169 std::unique_ptr<android::uirenderer::renderthread::RenderProxy> mProxy;
Derek Sollenberger3d5278b2016-01-14 14:22:53 -0500170 std::unique_ptr<android::uirenderer::RecordingCanvas> mCanvas;
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400171 android::sp<android::IGraphicBufferProducer> mProducer;
172 android::sp<android::IGraphicBufferConsumer> mConsumer;
173 android::sp<android::CpuConsumer> mCpuConsumer;
174 android::sp<android::Surface> mAndroidSurface;
175 SkISize mSize;
176};
177
178
179TestWindowContext::TestWindowContext() :
180 mData (nullptr) { }
181
Tom Hudson58862c92015-12-10 16:46:57 -0500182TestWindowContext::~TestWindowContext() {
183 delete mData;
184}
185
Tom Hudsonb2f5bd22015-10-15 16:41:55 -0400186void TestWindowContext::initialize(int width, int height) {
187 mData = new TestWindowData(SkISize::Make(width, height));
188}
189
190SkCanvas* TestWindowContext::prepareToDraw() {
191 return mData ? mData->prepareToDraw() : nullptr;
192}
193
194void TestWindowContext::finishDrawing() {
195 if (mData) {
196 mData->finishDrawing();
197 }
198}
199
200void TestWindowContext::fence() {
201 if (mData) {
202 mData->fence();
203 }
204}
205
206bool TestWindowContext::capturePixels(SkBitmap* bmp) {
207 return mData ? mData->capturePixels(bmp) : false;
208}
209
210} // namespace uirenderer
211} // namespace android
212