blob: ce4e17f8b191d9d0d73bb28ca29f0a013083212f [file] [log] [blame]
Chia-I Wubb61a722016-10-24 15:40:20 +08001/*
2 * Copyright 2016 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
17#define LOG_TAG "HwcPassthrough"
18
Chia-I Wu2ae85702017-04-20 11:01:18 -070019#include <android/hardware/graphics/mapper/2.0/IMapper.h>
Chia-I Wubb61a722016-10-24 15:40:20 +080020#include <log/log.h>
21
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -050022#include "ComposerClient.h"
Fabien Sanglard9b117a42017-03-21 09:39:35 -070023#include "ComposerBase.h"
Chia-I Wubb61a722016-10-24 15:40:20 +080024#include "IComposerCommandBuffer.h"
25
26namespace android {
27namespace hardware {
28namespace graphics {
29namespace composer {
30namespace V2_1 {
31namespace implementation {
32
33namespace {
34
Chia-I Wu2ae85702017-04-20 11:01:18 -070035using MapperError = android::hardware::graphics::mapper::V2_0::Error;
36using android::hardware::graphics::mapper::V2_0::IMapper;
37
Chia-I Wubb61a722016-10-24 15:40:20 +080038class HandleImporter {
39public:
Chia-I Wubb61a722016-10-24 15:40:20 +080040 bool initialize()
41 {
42 // allow only one client
43 if (mInitialized) {
44 return false;
45 }
46
Chia-I Wu2ae85702017-04-20 11:01:18 -070047 mMapper = IMapper::getService();
Chia-I Wubb61a722016-10-24 15:40:20 +080048
49 mInitialized = true;
50 return true;
51 }
52
53 void cleanup()
54 {
Chia-I Wu2ae85702017-04-20 11:01:18 -070055 mMapper.clear();
Chia-I Wubb61a722016-10-24 15:40:20 +080056 mInitialized = false;
57 }
58
59 // In IComposer, any buffer_handle_t is owned by the caller and we need to
60 // make a clone for hwcomposer2. We also need to translate empty handle
61 // to nullptr. This function does that, in-place.
62 bool importBuffer(buffer_handle_t& handle)
63 {
64 if (!handle) {
65 return true;
66 }
67
68 if (!handle->numFds && !handle->numInts) {
69 handle = nullptr;
70 return true;
71 }
72
Chia-I Wu2ae85702017-04-20 11:01:18 -070073 MapperError error;
74 buffer_handle_t importedHandle;
75 mMapper->importBuffer(
76 hidl_handle(handle),
77 [&](const auto& tmpError, const auto& tmpBufferHandle) {
78 error = tmpError;
79 importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle);
80 });
81 if (error != MapperError::NONE) {
Chia-I Wubb61a722016-10-24 15:40:20 +080082 return false;
83 }
84
Chia-I Wu2ae85702017-04-20 11:01:18 -070085 handle = importedHandle;
86
Chia-I Wubb61a722016-10-24 15:40:20 +080087 return true;
88 }
89
90 void freeBuffer(buffer_handle_t handle)
91 {
92 if (!handle) {
93 return;
94 }
95
Chia-I Wu2ae85702017-04-20 11:01:18 -070096 mMapper->freeBuffer(const_cast<native_handle_t*>(handle));
Chia-I Wubb61a722016-10-24 15:40:20 +080097 }
98
99private:
Chia-I Wu2ae85702017-04-20 11:01:18 -0700100 bool mInitialized = false;
101 sp<IMapper> mMapper;
Chia-I Wubb61a722016-10-24 15:40:20 +0800102};
103
104HandleImporter sHandleImporter;
105
106} // anonymous namespace
107
Chia-I Wu41a1c152017-03-09 15:41:59 -0800108BufferCacheEntry::BufferCacheEntry()
Chia-I Wubb61a722016-10-24 15:40:20 +0800109 : mHandle(nullptr)
110{
111}
112
Chia-I Wu41a1c152017-03-09 15:41:59 -0800113BufferCacheEntry::BufferCacheEntry(BufferCacheEntry&& other)
Chia-I Wubb61a722016-10-24 15:40:20 +0800114{
115 mHandle = other.mHandle;
116 other.mHandle = nullptr;
117}
118
Chia-I Wu41a1c152017-03-09 15:41:59 -0800119BufferCacheEntry& BufferCacheEntry::operator=(buffer_handle_t handle)
Chia-I Wubb61a722016-10-24 15:40:20 +0800120{
121 clear();
122 mHandle = handle;
123 return *this;
124}
125
Chia-I Wu41a1c152017-03-09 15:41:59 -0800126BufferCacheEntry::~BufferCacheEntry()
Chia-I Wubb61a722016-10-24 15:40:20 +0800127{
128 clear();
129}
130
Chia-I Wu41a1c152017-03-09 15:41:59 -0800131void BufferCacheEntry::clear()
Chia-I Wubb61a722016-10-24 15:40:20 +0800132{
133 if (mHandle) {
134 sHandleImporter.freeBuffer(mHandle);
135 }
136}
137
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500138ComposerClient::ComposerClient(ComposerBase& hal)
139 : mHal(hal), mWriter(kWriterInitialSize)
Chia-I Wubb61a722016-10-24 15:40:20 +0800140{
Chia-I Wubb61a722016-10-24 15:40:20 +0800141}
142
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500143ComposerClient::~ComposerClient()
Chia-I Wubb61a722016-10-24 15:40:20 +0800144{
Chia-I Wu6c84f7e2017-03-08 15:30:35 -0800145 // We want to call hwc2_close here (and move hwc2_open to the
146 // constructor), with the assumption that hwc2_close would
147 //
148 // - clean up all resources owned by the client
149 // - make sure all displays are blank (since there is no layer)
150 //
151 // But since SF used to crash at this point, different hwcomposer2
152 // implementations behave differently on hwc2_close. Our only portable
153 // choice really is to abort(). But that is not an option anymore
154 // because we might also have VTS or VR as clients that can come and go.
155 //
156 // Below we manually clean all resources (layers and virtual
157 // displays), and perform a presentDisplay afterwards.
158 ALOGW("destroying composer client");
Chia-I Wu43398712017-02-17 12:38:50 -0800159
Chia-I Wubb61a722016-10-24 15:40:20 +0800160 mHal.enableCallback(false);
Chia-I Wubb61a722016-10-24 15:40:20 +0800161
Chia-I Wu6c84f7e2017-03-08 15:30:35 -0800162 // no need to grab the mutex as any in-flight hwbinder call would have
163 // kept the client alive
Chia-I Wubb61a722016-10-24 15:40:20 +0800164 for (const auto& dpy : mDisplayData) {
Chia-I Wu6c84f7e2017-03-08 15:30:35 -0800165 ALOGW("destroying client resources for display %" PRIu64, dpy.first);
166
Chia-I Wubb61a722016-10-24 15:40:20 +0800167 for (const auto& ly : dpy.second.Layers) {
168 mHal.destroyLayer(dpy.first, ly.first);
169 }
170
171 if (dpy.second.IsVirtual) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800172 mHal.destroyVirtualDisplay(dpy.first);
Chia-I Wu6c84f7e2017-03-08 15:30:35 -0800173 } else {
174 ALOGW("performing a final presentDisplay");
175
176 std::vector<Layer> changedLayers;
177 std::vector<IComposerClient::Composition> compositionTypes;
178 uint32_t displayRequestMask = 0;
179 std::vector<Layer> requestedLayers;
180 std::vector<uint32_t> requestMasks;
181 mHal.validateDisplay(dpy.first, &changedLayers, &compositionTypes,
182 &displayRequestMask, &requestedLayers, &requestMasks);
183
184 mHal.acceptDisplayChanges(dpy.first);
185
186 int32_t presentFence = -1;
187 std::vector<Layer> releasedLayers;
188 std::vector<int32_t> releaseFences;
189 mHal.presentDisplay(dpy.first, &presentFence, &releasedLayers, &releaseFences);
190 if (presentFence >= 0) {
191 close(presentFence);
192 }
193 for (auto fence : releaseFences) {
194 if (fence >= 0) {
195 close(fence);
196 }
197 }
Chia-I Wubb61a722016-10-24 15:40:20 +0800198 }
199 }
200
201 mDisplayData.clear();
202
203 sHandleImporter.cleanup();
Chia-I Wu6c84f7e2017-03-08 15:30:35 -0800204
205 mHal.removeClient();
206
207 ALOGW("removed composer client");
Chia-I Wubb61a722016-10-24 15:40:20 +0800208}
209
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500210void ComposerClient::initialize()
211{
212 mReader = createCommandReader();
213 if (!sHandleImporter.initialize()) {
214 LOG_ALWAYS_FATAL("failed to initialize handle importer");
215 }
216}
217
218void ComposerClient::onHotplug(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800219 IComposerCallback::Connection connected)
220{
221 {
222 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
223
224 if (connected == IComposerCallback::Connection::CONNECTED) {
225 mDisplayData.emplace(display, DisplayData(false));
226 } else if (connected == IComposerCallback::Connection::DISCONNECTED) {
227 mDisplayData.erase(display);
228 }
229 }
230
Chia-I Wu43398712017-02-17 12:38:50 -0800231 auto ret = mCallback->onHotplug(display, connected);
232 ALOGE_IF(!ret.isOk(), "failed to send onHotplug: %s",
233 ret.description().c_str());
Chia-I Wubb61a722016-10-24 15:40:20 +0800234}
235
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500236void ComposerClient::onRefresh(Display display)
Chia-I Wubb61a722016-10-24 15:40:20 +0800237{
Chia-I Wu43398712017-02-17 12:38:50 -0800238 auto ret = mCallback->onRefresh(display);
239 ALOGE_IF(!ret.isOk(), "failed to send onRefresh: %s",
240 ret.description().c_str());
Chia-I Wubb61a722016-10-24 15:40:20 +0800241}
242
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500243void ComposerClient::onVsync(Display display, int64_t timestamp)
Chia-I Wubb61a722016-10-24 15:40:20 +0800244{
Chia-I Wu43398712017-02-17 12:38:50 -0800245 auto ret = mCallback->onVsync(display, timestamp);
246 ALOGE_IF(!ret.isOk(), "failed to send onVsync: %s",
247 ret.description().c_str());
Chia-I Wubb61a722016-10-24 15:40:20 +0800248}
249
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500250Return<void> ComposerClient::registerCallback(
251 const sp<IComposerCallback>& callback)
Chia-I Wubb61a722016-10-24 15:40:20 +0800252{
253 // no locking as we require this function to be called only once
254 mCallback = callback;
255 mHal.enableCallback(callback != nullptr);
256
257 return Void();
258}
259
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500260Return<uint32_t> ComposerClient::getMaxVirtualDisplayCount()
Chia-I Wubb61a722016-10-24 15:40:20 +0800261{
262 return mHal.getMaxVirtualDisplayCount();
263}
264
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500265Return<void> ComposerClient::createVirtualDisplay(uint32_t width,
266 uint32_t height, PixelFormat formatHint, uint32_t outputBufferSlotCount,
Chia-I Wubb61a722016-10-24 15:40:20 +0800267 createVirtualDisplay_cb hidl_cb)
268{
Chia-I Wue1768352016-12-19 12:56:54 +0800269 Display display = 0;
270 Error err = mHal.createVirtualDisplay(width, height,
271 &formatHint, &display);
Chia-I Wubb61a722016-10-24 15:40:20 +0800272 if (err == Error::NONE) {
273 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
274
275 auto dpy = mDisplayData.emplace(display, DisplayData(true)).first;
276 dpy->second.OutputBuffers.resize(outputBufferSlotCount);
277 }
278
279 hidl_cb(err, display, formatHint);
280 return Void();
281}
282
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500283Return<Error> ComposerClient::destroyVirtualDisplay(Display display)
Chia-I Wubb61a722016-10-24 15:40:20 +0800284{
285 Error err = mHal.destroyVirtualDisplay(display);
286 if (err == Error::NONE) {
287 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
288
289 mDisplayData.erase(display);
290 }
291
292 return err;
293}
294
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500295Return<void> ComposerClient::createLayer(Display display,
296 uint32_t bufferSlotCount, createLayer_cb hidl_cb)
Chia-I Wubb61a722016-10-24 15:40:20 +0800297{
Chia-I Wue1768352016-12-19 12:56:54 +0800298 Layer layer = 0;
299 Error err = mHal.createLayer(display, &layer);
Chia-I Wubb61a722016-10-24 15:40:20 +0800300 if (err == Error::NONE) {
301 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
302
303 auto dpy = mDisplayData.find(display);
304 auto ly = dpy->second.Layers.emplace(layer, LayerBuffers()).first;
305 ly->second.Buffers.resize(bufferSlotCount);
306 }
307
308 hidl_cb(err, layer);
309 return Void();
310}
311
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500312Return<Error> ComposerClient::destroyLayer(Display display, Layer layer)
Chia-I Wubb61a722016-10-24 15:40:20 +0800313{
314 Error err = mHal.destroyLayer(display, layer);
315 if (err == Error::NONE) {
316 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
317
318 auto dpy = mDisplayData.find(display);
319 dpy->second.Layers.erase(layer);
320 }
321
322 return err;
323}
324
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500325Return<void> ComposerClient::getActiveConfig(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800326 getActiveConfig_cb hidl_cb)
327{
Chia-I Wue1768352016-12-19 12:56:54 +0800328 Config config = 0;
329 Error err = mHal.getActiveConfig(display, &config);
Chia-I Wubb61a722016-10-24 15:40:20 +0800330
331 hidl_cb(err, config);
332 return Void();
333}
334
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500335Return<Error> ComposerClient::getClientTargetSupport(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800336 uint32_t width, uint32_t height,
337 PixelFormat format, Dataspace dataspace)
338{
339 Error err = mHal.getClientTargetSupport(display,
340 width, height, format, dataspace);
341 return err;
342}
343
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500344Return<void> ComposerClient::getColorModes(Display display,
345 getColorModes_cb hidl_cb)
Chia-I Wubb61a722016-10-24 15:40:20 +0800346{
347 hidl_vec<ColorMode> modes;
Chia-I Wue1768352016-12-19 12:56:54 +0800348 Error err = mHal.getColorModes(display, &modes);
Chia-I Wubb61a722016-10-24 15:40:20 +0800349
350 hidl_cb(err, modes);
351 return Void();
352}
353
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500354Return<void> ComposerClient::getDisplayAttribute(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800355 Config config, Attribute attribute,
356 getDisplayAttribute_cb hidl_cb)
357{
Chia-I Wue1768352016-12-19 12:56:54 +0800358 int32_t value = 0;
359 Error err = mHal.getDisplayAttribute(display, config, attribute, &value);
Chia-I Wubb61a722016-10-24 15:40:20 +0800360
361 hidl_cb(err, value);
362 return Void();
363}
364
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500365Return<void> ComposerClient::getDisplayConfigs(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800366 getDisplayConfigs_cb hidl_cb)
367{
368 hidl_vec<Config> configs;
Chia-I Wue1768352016-12-19 12:56:54 +0800369 Error err = mHal.getDisplayConfigs(display, &configs);
Chia-I Wubb61a722016-10-24 15:40:20 +0800370
371 hidl_cb(err, configs);
372 return Void();
373}
374
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500375Return<void> ComposerClient::getDisplayName(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800376 getDisplayName_cb hidl_cb)
377{
378 hidl_string name;
Chia-I Wue1768352016-12-19 12:56:54 +0800379 Error err = mHal.getDisplayName(display, &name);
Chia-I Wubb61a722016-10-24 15:40:20 +0800380
381 hidl_cb(err, name);
382 return Void();
383}
384
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500385Return<void> ComposerClient::getDisplayType(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800386 getDisplayType_cb hidl_cb)
387{
Chia-I Wue1768352016-12-19 12:56:54 +0800388 DisplayType type = DisplayType::INVALID;
389 Error err = mHal.getDisplayType(display, &type);
Chia-I Wubb61a722016-10-24 15:40:20 +0800390
391 hidl_cb(err, type);
392 return Void();
393}
394
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500395Return<void> ComposerClient::getDozeSupport(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800396 getDozeSupport_cb hidl_cb)
397{
Chia-I Wue1768352016-12-19 12:56:54 +0800398 bool support = false;
399 Error err = mHal.getDozeSupport(display, &support);
Chia-I Wubb61a722016-10-24 15:40:20 +0800400
401 hidl_cb(err, support);
402 return Void();
403}
404
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500405Return<void> ComposerClient::getHdrCapabilities(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800406 getHdrCapabilities_cb hidl_cb)
407{
408 hidl_vec<Hdr> types;
409 float max_lumi = 0.0f;
410 float max_avg_lumi = 0.0f;
411 float min_lumi = 0.0f;
Chia-I Wue1768352016-12-19 12:56:54 +0800412 Error err = mHal.getHdrCapabilities(display, &types,
413 &max_lumi, &max_avg_lumi, &min_lumi);
Chia-I Wubb61a722016-10-24 15:40:20 +0800414
415 hidl_cb(err, types, max_lumi, max_avg_lumi, min_lumi);
416 return Void();
417}
418
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500419Return<Error> ComposerClient::setClientTargetSlotCount(Display display,
Chia-I Wubb61a722016-10-24 15:40:20 +0800420 uint32_t clientTargetSlotCount)
421{
422 std::lock_guard<std::mutex> lock(mDisplayDataMutex);
423
424 auto dpy = mDisplayData.find(display);
425 if (dpy == mDisplayData.end()) {
426 return Error::BAD_DISPLAY;
427 }
428
429 dpy->second.ClientTargets.resize(clientTargetSlotCount);
430
431 return Error::NONE;
432}
433
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500434Return<Error> ComposerClient::setActiveConfig(Display display, Config config)
Chia-I Wubb61a722016-10-24 15:40:20 +0800435{
436 Error err = mHal.setActiveConfig(display, config);
437 return err;
438}
439
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500440Return<Error> ComposerClient::setColorMode(Display display, ColorMode mode)
Chia-I Wubb61a722016-10-24 15:40:20 +0800441{
442 Error err = mHal.setColorMode(display, mode);
443 return err;
444}
445
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500446Return<Error> ComposerClient::setPowerMode(Display display, PowerMode mode)
Chia-I Wubb61a722016-10-24 15:40:20 +0800447{
448 Error err = mHal.setPowerMode(display, mode);
449 return err;
450}
451
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500452Return<Error> ComposerClient::setVsyncEnabled(Display display, Vsync enabled)
Chia-I Wubb61a722016-10-24 15:40:20 +0800453{
454 Error err = mHal.setVsyncEnabled(display, enabled);
455 return err;
456}
457
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500458Return<Error> ComposerClient::setInputCommandQueue(
Hridya Valsaraju33351da2016-12-27 12:40:01 -0800459 const MQDescriptorSync<uint32_t>& descriptor)
Chia-I Wubb61a722016-10-24 15:40:20 +0800460{
461 std::lock_guard<std::mutex> lock(mCommandMutex);
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500462 return mReader->setMQDescriptor(descriptor) ?
Chia-I Wubb61a722016-10-24 15:40:20 +0800463 Error::NONE : Error::NO_RESOURCES;
464}
465
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500466Return<void> ComposerClient::getOutputCommandQueue(
Chia-I Wubb61a722016-10-24 15:40:20 +0800467 getOutputCommandQueue_cb hidl_cb)
468{
469 // no locking as we require this function to be called inside
470 // executeCommands_cb
471
472 auto outDescriptor = mWriter.getMQDescriptor();
473 if (outDescriptor) {
474 hidl_cb(Error::NONE, *outDescriptor);
475 } else {
Hridya Valsaraju790db102017-01-10 08:58:23 -0800476 hidl_cb(Error::NO_RESOURCES, CommandQueueType::Descriptor());
Chia-I Wubb61a722016-10-24 15:40:20 +0800477 }
478
479 return Void();
480}
481
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500482Return<void> ComposerClient::executeCommands(uint32_t inLength,
Chia-I Wubb61a722016-10-24 15:40:20 +0800483 const hidl_vec<hidl_handle>& inHandles,
484 executeCommands_cb hidl_cb)
485{
486 std::lock_guard<std::mutex> lock(mCommandMutex);
487
488 bool outChanged = false;
489 uint32_t outLength = 0;
490 hidl_vec<hidl_handle> outHandles;
491
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500492 if (!mReader->readQueue(inLength, inHandles)) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800493 hidl_cb(Error::BAD_PARAMETER, outChanged, outLength, outHandles);
494 return Void();
495 }
496
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500497 Error err = mReader->parse();
Chia-I Wubb61a722016-10-24 15:40:20 +0800498 if (err == Error::NONE &&
Chia-I Wue1768352016-12-19 12:56:54 +0800499 !mWriter.writeQueue(&outChanged, &outLength, &outHandles)) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800500 err = Error::NO_RESOURCES;
501 }
502
503 hidl_cb(err, outChanged, outLength, outHandles);
504
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500505 mReader->reset();
Chia-I Wubb61a722016-10-24 15:40:20 +0800506 mWriter.reset();
507
508 return Void();
509}
510
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500511std::unique_ptr<ComposerClient::CommandReader>
512ComposerClient::createCommandReader()
513{
514 return std::unique_ptr<ComposerClient::CommandReader>(
515 new CommandReader(*this));
516}
517
518ComposerClient::CommandReader::CommandReader(ComposerClient& client)
Chia-I Wubb61a722016-10-24 15:40:20 +0800519 : mClient(client), mHal(client.mHal), mWriter(client.mWriter)
520{
521}
522
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500523ComposerClient::CommandReader::~CommandReader()
524{
525}
526
527Error ComposerClient::CommandReader::parse()
Chia-I Wubb61a722016-10-24 15:40:20 +0800528{
529 IComposerClient::Command command;
Chia-I Wue1768352016-12-19 12:56:54 +0800530 uint16_t length = 0;
Chia-I Wubb61a722016-10-24 15:40:20 +0800531
532 while (!isEmpty()) {
Chia-I Wue1768352016-12-19 12:56:54 +0800533 if (!beginCommand(&command, &length)) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800534 break;
535 }
536
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500537 bool parsed = parseCommand(command, length);
Chia-I Wubb61a722016-10-24 15:40:20 +0800538 endCommand();
539
540 if (!parsed) {
541 ALOGE("failed to parse command 0x%x, length %" PRIu16,
542 command, length);
543 break;
544 }
545 }
546
547 return (isEmpty()) ? Error::NONE : Error::BAD_PARAMETER;
548}
549
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500550bool ComposerClient::CommandReader::parseCommand(
551 IComposerClient::Command command, uint16_t length) {
552 switch (command) {
553 case IComposerClient::Command::SELECT_DISPLAY:
554 return parseSelectDisplay(length);
555 case IComposerClient::Command::SELECT_LAYER:
556 return parseSelectLayer(length);
557 case IComposerClient::Command::SET_COLOR_TRANSFORM:
558 return parseSetColorTransform(length);
559 case IComposerClient::Command::SET_CLIENT_TARGET:
560 return parseSetClientTarget(length);
561 case IComposerClient::Command::SET_OUTPUT_BUFFER:
562 return parseSetOutputBuffer(length);
563 case IComposerClient::Command::VALIDATE_DISPLAY:
564 return parseValidateDisplay(length);
565 case IComposerClient::Command::ACCEPT_DISPLAY_CHANGES:
566 return parseAcceptDisplayChanges(length);
567 case IComposerClient::Command::PRESENT_DISPLAY:
568 return parsePresentDisplay(length);
569 case IComposerClient::Command::SET_LAYER_CURSOR_POSITION:
570 return parseSetLayerCursorPosition(length);
571 case IComposerClient::Command::SET_LAYER_BUFFER:
572 return parseSetLayerBuffer(length);
573 case IComposerClient::Command::SET_LAYER_SURFACE_DAMAGE:
574 return parseSetLayerSurfaceDamage(length);
575 case IComposerClient::Command::SET_LAYER_BLEND_MODE:
576 return parseSetLayerBlendMode(length);
577 case IComposerClient::Command::SET_LAYER_COLOR:
578 return parseSetLayerColor(length);
579 case IComposerClient::Command::SET_LAYER_COMPOSITION_TYPE:
580 return parseSetLayerCompositionType(length);
581 case IComposerClient::Command::SET_LAYER_DATASPACE:
582 return parseSetLayerDataspace(length);
583 case IComposerClient::Command::SET_LAYER_DISPLAY_FRAME:
584 return parseSetLayerDisplayFrame(length);
585 case IComposerClient::Command::SET_LAYER_PLANE_ALPHA:
586 return parseSetLayerPlaneAlpha(length);
587 case IComposerClient::Command::SET_LAYER_SIDEBAND_STREAM:
588 return parseSetLayerSidebandStream(length);
589 case IComposerClient::Command::SET_LAYER_SOURCE_CROP:
590 return parseSetLayerSourceCrop(length);
591 case IComposerClient::Command::SET_LAYER_TRANSFORM:
592 return parseSetLayerTransform(length);
593 case IComposerClient::Command::SET_LAYER_VISIBLE_REGION:
594 return parseSetLayerVisibleRegion(length);
595 case IComposerClient::Command::SET_LAYER_Z_ORDER:
596 return parseSetLayerZOrder(length);
597 default:
598 return false;
599 }
600}
601
602bool ComposerClient::CommandReader::parseSelectDisplay(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800603{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500604 if (length != CommandWriterBase::kSelectDisplayLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800605 return false;
606 }
607
608 mDisplay = read64();
609 mWriter.selectDisplay(mDisplay);
610
611 return true;
612}
613
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500614bool ComposerClient::CommandReader::parseSelectLayer(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800615{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500616 if (length != CommandWriterBase::kSelectLayerLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800617 return false;
618 }
619
620 mLayer = read64();
621
622 return true;
623}
624
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500625bool ComposerClient::CommandReader::parseSetColorTransform(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800626{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500627 if (length != CommandWriterBase::kSetColorTransformLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800628 return false;
629 }
630
631 float matrix[16];
632 for (int i = 0; i < 16; i++) {
633 matrix[i] = readFloat();
634 }
635 auto transform = readSigned();
636
637 auto err = mHal.setColorTransform(mDisplay, matrix, transform);
638 if (err != Error::NONE) {
639 mWriter.setError(getCommandLoc(), err);
640 }
641
642 return true;
643}
644
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500645bool ComposerClient::CommandReader::parseSetClientTarget(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800646{
647 // 4 parameters followed by N rectangles
648 if ((length - 4) % 4 != 0) {
649 return false;
650 }
651
Chia-I Wue1768352016-12-19 12:56:54 +0800652 bool useCache = false;
Chia-I Wubb61a722016-10-24 15:40:20 +0800653 auto slot = read();
Chia-I Wue1768352016-12-19 12:56:54 +0800654 auto clientTarget = readHandle(&useCache);
Chia-I Wubb61a722016-10-24 15:40:20 +0800655 auto fence = readFence();
656 auto dataspace = readSigned();
657 auto damage = readRegion((length - 4) / 4);
Steven Thomas58da77e2017-04-26 15:24:28 -0700658 bool closeFence = true;
Chia-I Wubb61a722016-10-24 15:40:20 +0800659
660 auto err = lookupBuffer(BufferCache::CLIENT_TARGETS,
Chia-I Wu41a1c152017-03-09 15:41:59 -0800661 slot, useCache, clientTarget, &clientTarget);
Chia-I Wubb61a722016-10-24 15:40:20 +0800662 if (err == Error::NONE) {
663 err = mHal.setClientTarget(mDisplay, clientTarget, fence,
664 dataspace, damage);
Steven Thomas58da77e2017-04-26 15:24:28 -0700665 auto updateBufErr = updateBuffer(BufferCache::CLIENT_TARGETS, slot,
666 useCache, clientTarget);
667 if (err == Error::NONE) {
668 closeFence = false;
669 err = updateBufErr;
670 }
671 }
672 if (closeFence) {
673 close(fence);
Chia-I Wubb61a722016-10-24 15:40:20 +0800674 }
675 if (err != Error::NONE) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800676 mWriter.setError(getCommandLoc(), err);
677 }
678
679 return true;
680}
681
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500682bool ComposerClient::CommandReader::parseSetOutputBuffer(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800683{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500684 if (length != CommandWriterBase::kSetOutputBufferLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800685 return false;
686 }
687
Chia-I Wue1768352016-12-19 12:56:54 +0800688 bool useCache = false;
Chia-I Wubb61a722016-10-24 15:40:20 +0800689 auto slot = read();
Chia-I Wue1768352016-12-19 12:56:54 +0800690 auto outputBuffer = readHandle(&useCache);
Chia-I Wubb61a722016-10-24 15:40:20 +0800691 auto fence = readFence();
Steven Thomas58da77e2017-04-26 15:24:28 -0700692 bool closeFence = true;
Chia-I Wubb61a722016-10-24 15:40:20 +0800693
694 auto err = lookupBuffer(BufferCache::OUTPUT_BUFFERS,
Chia-I Wu41a1c152017-03-09 15:41:59 -0800695 slot, useCache, outputBuffer, &outputBuffer);
Chia-I Wubb61a722016-10-24 15:40:20 +0800696 if (err == Error::NONE) {
697 err = mHal.setOutputBuffer(mDisplay, outputBuffer, fence);
Steven Thomas58da77e2017-04-26 15:24:28 -0700698 auto updateBufErr = updateBuffer(BufferCache::OUTPUT_BUFFERS,
699 slot, useCache, outputBuffer);
700 if (err == Error::NONE) {
701 closeFence = false;
702 err = updateBufErr;
703 }
704 }
705 if (closeFence) {
706 close(fence);
Chia-I Wubb61a722016-10-24 15:40:20 +0800707 }
708 if (err != Error::NONE) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800709 mWriter.setError(getCommandLoc(), err);
710 }
711
712 return true;
713}
714
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500715bool ComposerClient::CommandReader::parseValidateDisplay(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800716{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500717 if (length != CommandWriterBase::kValidateDisplayLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800718 return false;
719 }
720
721 std::vector<Layer> changedLayers;
722 std::vector<IComposerClient::Composition> compositionTypes;
Chia-I Wue1768352016-12-19 12:56:54 +0800723 uint32_t displayRequestMask = 0x0;
Chia-I Wubb61a722016-10-24 15:40:20 +0800724 std::vector<Layer> requestedLayers;
725 std::vector<uint32_t> requestMasks;
726
Chia-I Wue1768352016-12-19 12:56:54 +0800727 auto err = mHal.validateDisplay(mDisplay, &changedLayers,
728 &compositionTypes, &displayRequestMask,
729 &requestedLayers, &requestMasks);
Chia-I Wubb61a722016-10-24 15:40:20 +0800730 if (err == Error::NONE) {
731 mWriter.setChangedCompositionTypes(changedLayers,
732 compositionTypes);
733 mWriter.setDisplayRequests(displayRequestMask,
734 requestedLayers, requestMasks);
735 } else {
736 mWriter.setError(getCommandLoc(), err);
737 }
738
739 return true;
740}
741
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500742bool ComposerClient::CommandReader::parseAcceptDisplayChanges(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800743{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500744 if (length != CommandWriterBase::kAcceptDisplayChangesLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800745 return false;
746 }
747
748 auto err = mHal.acceptDisplayChanges(mDisplay);
749 if (err != Error::NONE) {
750 mWriter.setError(getCommandLoc(), err);
751 }
752
753 return true;
754}
755
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500756bool ComposerClient::CommandReader::parsePresentDisplay(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800757{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500758 if (length != CommandWriterBase::kPresentDisplayLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800759 return false;
760 }
761
Chia-I Wue1768352016-12-19 12:56:54 +0800762 int presentFence = -1;
Chia-I Wubb61a722016-10-24 15:40:20 +0800763 std::vector<Layer> layers;
764 std::vector<int> fences;
Chia-I Wue1768352016-12-19 12:56:54 +0800765 auto err = mHal.presentDisplay(mDisplay, &presentFence, &layers, &fences);
Chia-I Wubb61a722016-10-24 15:40:20 +0800766 if (err == Error::NONE) {
767 mWriter.setPresentFence(presentFence);
768 mWriter.setReleaseFences(layers, fences);
769 } else {
770 mWriter.setError(getCommandLoc(), err);
771 }
772
773 return true;
774}
775
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500776bool ComposerClient::CommandReader::parseSetLayerCursorPosition(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800777{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500778 if (length != CommandWriterBase::kSetLayerCursorPositionLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800779 return false;
780 }
781
782 auto err = mHal.setLayerCursorPosition(mDisplay, mLayer,
783 readSigned(), readSigned());
784 if (err != Error::NONE) {
785 mWriter.setError(getCommandLoc(), err);
786 }
787
788 return true;
789}
790
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500791bool ComposerClient::CommandReader::parseSetLayerBuffer(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800792{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500793 if (length != CommandWriterBase::kSetLayerBufferLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800794 return false;
795 }
796
Chia-I Wue1768352016-12-19 12:56:54 +0800797 bool useCache = false;
Chia-I Wubb61a722016-10-24 15:40:20 +0800798 auto slot = read();
Chia-I Wue1768352016-12-19 12:56:54 +0800799 auto buffer = readHandle(&useCache);
Chia-I Wubb61a722016-10-24 15:40:20 +0800800 auto fence = readFence();
Steven Thomas58da77e2017-04-26 15:24:28 -0700801 bool closeFence = true;
Chia-I Wubb61a722016-10-24 15:40:20 +0800802
803 auto err = lookupBuffer(BufferCache::LAYER_BUFFERS,
Chia-I Wu41a1c152017-03-09 15:41:59 -0800804 slot, useCache, buffer, &buffer);
Chia-I Wubb61a722016-10-24 15:40:20 +0800805 if (err == Error::NONE) {
806 err = mHal.setLayerBuffer(mDisplay, mLayer, buffer, fence);
Steven Thomas58da77e2017-04-26 15:24:28 -0700807 auto updateBufErr = updateBuffer(BufferCache::LAYER_BUFFERS, slot,
808 useCache, buffer);
809 if (err == Error::NONE) {
810 closeFence = false;
811 err = updateBufErr;
812 }
813 }
814 if (closeFence) {
815 close(fence);
Chia-I Wubb61a722016-10-24 15:40:20 +0800816 }
817 if (err != Error::NONE) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800818 mWriter.setError(getCommandLoc(), err);
819 }
820
821 return true;
822}
823
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500824bool ComposerClient::CommandReader::parseSetLayerSurfaceDamage(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800825{
826 // N rectangles
827 if (length % 4 != 0) {
828 return false;
829 }
830
831 auto damage = readRegion(length / 4);
832 auto err = mHal.setLayerSurfaceDamage(mDisplay, mLayer, damage);
833 if (err != Error::NONE) {
834 mWriter.setError(getCommandLoc(), err);
835 }
836
837 return true;
838}
839
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500840bool ComposerClient::CommandReader::parseSetLayerBlendMode(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800841{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500842 if (length != CommandWriterBase::kSetLayerBlendModeLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800843 return false;
844 }
845
846 auto err = mHal.setLayerBlendMode(mDisplay, mLayer, readSigned());
847 if (err != Error::NONE) {
848 mWriter.setError(getCommandLoc(), err);
849 }
850
851 return true;
852}
853
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500854bool ComposerClient::CommandReader::parseSetLayerColor(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800855{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500856 if (length != CommandWriterBase::kSetLayerColorLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800857 return false;
858 }
859
860 auto err = mHal.setLayerColor(mDisplay, mLayer, readColor());
861 if (err != Error::NONE) {
862 mWriter.setError(getCommandLoc(), err);
863 }
864
865 return true;
866}
867
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500868bool ComposerClient::CommandReader::parseSetLayerCompositionType(
869 uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800870{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500871 if (length != CommandWriterBase::kSetLayerCompositionTypeLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800872 return false;
873 }
874
875 auto err = mHal.setLayerCompositionType(mDisplay, mLayer, readSigned());
876 if (err != Error::NONE) {
877 mWriter.setError(getCommandLoc(), err);
878 }
879
880 return true;
881}
882
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500883bool ComposerClient::CommandReader::parseSetLayerDataspace(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800884{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500885 if (length != CommandWriterBase::kSetLayerDataspaceLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800886 return false;
887 }
888
889 auto err = mHal.setLayerDataspace(mDisplay, mLayer, readSigned());
890 if (err != Error::NONE) {
891 mWriter.setError(getCommandLoc(), err);
892 }
893
894 return true;
895}
896
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500897bool ComposerClient::CommandReader::parseSetLayerDisplayFrame(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800898{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500899 if (length != CommandWriterBase::kSetLayerDisplayFrameLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800900 return false;
901 }
902
903 auto err = mHal.setLayerDisplayFrame(mDisplay, mLayer, readRect());
904 if (err != Error::NONE) {
905 mWriter.setError(getCommandLoc(), err);
906 }
907
908 return true;
909}
910
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500911bool ComposerClient::CommandReader::parseSetLayerPlaneAlpha(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800912{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500913 if (length != CommandWriterBase::kSetLayerPlaneAlphaLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800914 return false;
915 }
916
Dan Stoza0df10c42016-12-19 15:22:47 -0800917 auto err = mHal.setLayerPlaneAlpha(mDisplay, mLayer, readFloat());
Chia-I Wubb61a722016-10-24 15:40:20 +0800918 if (err != Error::NONE) {
919 mWriter.setError(getCommandLoc(), err);
920 }
921
922 return true;
923}
924
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500925bool ComposerClient::CommandReader::parseSetLayerSidebandStream(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800926{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500927 if (length != CommandWriterBase::kSetLayerSidebandStreamLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800928 return false;
929 }
930
931 auto stream = readHandle();
932
Chia-I Wu41a1c152017-03-09 15:41:59 -0800933 auto err = lookupLayerSidebandStream(stream, &stream);
Chia-I Wubb61a722016-10-24 15:40:20 +0800934 if (err == Error::NONE) {
935 err = mHal.setLayerSidebandStream(mDisplay, mLayer, stream);
Steven Thomas58da77e2017-04-26 15:24:28 -0700936 auto updateErr = updateLayerSidebandStream(stream);
937 if (err == Error::NONE) {
938 err = updateErr;
939 }
Chia-I Wubb61a722016-10-24 15:40:20 +0800940 }
941 if (err != Error::NONE) {
942 mWriter.setError(getCommandLoc(), err);
943 }
944
945 return true;
946}
947
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500948bool ComposerClient::CommandReader::parseSetLayerSourceCrop(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800949{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500950 if (length != CommandWriterBase::kSetLayerSourceCropLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800951 return false;
952 }
953
954 auto err = mHal.setLayerSourceCrop(mDisplay, mLayer, readFRect());
955 if (err != Error::NONE) {
956 mWriter.setError(getCommandLoc(), err);
957 }
958
959 return true;
960}
961
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500962bool ComposerClient::CommandReader::parseSetLayerTransform(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800963{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500964 if (length != CommandWriterBase::kSetLayerTransformLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800965 return false;
966 }
967
968 auto err = mHal.setLayerTransform(mDisplay, mLayer, readSigned());
969 if (err != Error::NONE) {
970 mWriter.setError(getCommandLoc(), err);
971 }
972
973 return true;
974}
975
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500976bool ComposerClient::CommandReader::parseSetLayerVisibleRegion(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800977{
978 // N rectangles
979 if (length % 4 != 0) {
980 return false;
981 }
982
983 auto region = readRegion(length / 4);
984 auto err = mHal.setLayerVisibleRegion(mDisplay, mLayer, region);
985 if (err != Error::NONE) {
986 mWriter.setError(getCommandLoc(), err);
987 }
988
989 return true;
990}
991
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500992bool ComposerClient::CommandReader::parseSetLayerZOrder(uint16_t length)
Chia-I Wubb61a722016-10-24 15:40:20 +0800993{
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500994 if (length != CommandWriterBase::kSetLayerZOrderLength) {
Chia-I Wubb61a722016-10-24 15:40:20 +0800995 return false;
996 }
997
998 auto err = mHal.setLayerZOrder(mDisplay, mLayer, read());
999 if (err != Error::NONE) {
1000 mWriter.setError(getCommandLoc(), err);
1001 }
1002
1003 return true;
1004}
1005
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -05001006hwc_rect_t ComposerClient::CommandReader::readRect()
Chia-I Wubb61a722016-10-24 15:40:20 +08001007{
1008 return hwc_rect_t{
1009 readSigned(),
1010 readSigned(),
1011 readSigned(),
1012 readSigned(),
1013 };
1014}
1015
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -05001016std::vector<hwc_rect_t> ComposerClient::CommandReader::readRegion(size_t count)
Chia-I Wubb61a722016-10-24 15:40:20 +08001017{
1018 std::vector<hwc_rect_t> region;
1019 region.reserve(count);
1020 while (count > 0) {
1021 region.emplace_back(readRect());
1022 count--;
1023 }
1024
1025 return region;
1026}
1027
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -05001028hwc_frect_t ComposerClient::CommandReader::readFRect()
Chia-I Wubb61a722016-10-24 15:40:20 +08001029{
1030 return hwc_frect_t{
1031 readFloat(),
1032 readFloat(),
1033 readFloat(),
1034 readFloat(),
1035 };
1036}
1037
Chia-I Wu41a1c152017-03-09 15:41:59 -08001038Error ComposerClient::CommandReader::lookupBufferCacheEntryLocked(
1039 BufferCache cache, uint32_t slot, BufferCacheEntry** outEntry)
Chia-I Wubb61a722016-10-24 15:40:20 +08001040{
Chia-I Wubb61a722016-10-24 15:40:20 +08001041 auto dpy = mClient.mDisplayData.find(mDisplay);
1042 if (dpy == mClient.mDisplayData.end()) {
1043 return Error::BAD_DISPLAY;
1044 }
1045
Chia-I Wu41a1c152017-03-09 15:41:59 -08001046 BufferCacheEntry* entry = nullptr;
Chia-I Wubb61a722016-10-24 15:40:20 +08001047 switch (cache) {
1048 case BufferCache::CLIENT_TARGETS:
1049 if (slot < dpy->second.ClientTargets.size()) {
Chia-I Wu41a1c152017-03-09 15:41:59 -08001050 entry = &dpy->second.ClientTargets[slot];
Chia-I Wubb61a722016-10-24 15:40:20 +08001051 }
1052 break;
1053 case BufferCache::OUTPUT_BUFFERS:
1054 if (slot < dpy->second.OutputBuffers.size()) {
Chia-I Wu41a1c152017-03-09 15:41:59 -08001055 entry = &dpy->second.OutputBuffers[slot];
Chia-I Wubb61a722016-10-24 15:40:20 +08001056 }
1057 break;
1058 case BufferCache::LAYER_BUFFERS:
1059 {
1060 auto ly = dpy->second.Layers.find(mLayer);
1061 if (ly == dpy->second.Layers.end()) {
1062 return Error::BAD_LAYER;
1063 }
1064 if (slot < ly->second.Buffers.size()) {
Chia-I Wu41a1c152017-03-09 15:41:59 -08001065 entry = &ly->second.Buffers[slot];
Chia-I Wubb61a722016-10-24 15:40:20 +08001066 }
1067 }
1068 break;
1069 case BufferCache::LAYER_SIDEBAND_STREAMS:
1070 {
1071 auto ly = dpy->second.Layers.find(mLayer);
1072 if (ly == dpy->second.Layers.end()) {
1073 return Error::BAD_LAYER;
1074 }
1075 if (slot == 0) {
Chia-I Wu41a1c152017-03-09 15:41:59 -08001076 entry = &ly->second.SidebandStream;
Chia-I Wubb61a722016-10-24 15:40:20 +08001077 }
1078 }
1079 break;
1080 default:
1081 break;
1082 }
1083
Chia-I Wu41a1c152017-03-09 15:41:59 -08001084 if (!entry) {
1085 ALOGW("invalid buffer slot %" PRIu32, slot);
Chia-I Wubb61a722016-10-24 15:40:20 +08001086 return Error::BAD_PARAMETER;
1087 }
1088
Chia-I Wu41a1c152017-03-09 15:41:59 -08001089 *outEntry = entry;
1090
1091 return Error::NONE;
1092}
1093
1094Error ComposerClient::CommandReader::lookupBuffer(BufferCache cache,
1095 uint32_t slot, bool useCache, buffer_handle_t handle,
1096 buffer_handle_t* outHandle)
1097{
Chia-I Wubb61a722016-10-24 15:40:20 +08001098 if (useCache) {
Chia-I Wu41a1c152017-03-09 15:41:59 -08001099 std::lock_guard<std::mutex> lock(mClient.mDisplayDataMutex);
1100
1101 BufferCacheEntry* entry;
1102 Error error = lookupBufferCacheEntryLocked(cache, slot, &entry);
1103 if (error != Error::NONE) {
1104 return error;
1105 }
1106
1107 // input handle is ignored
1108 *outHandle = entry->getHandle();
Chia-I Wubb61a722016-10-24 15:40:20 +08001109 } else {
1110 if (!sHandleImporter.importBuffer(handle)) {
1111 return Error::NO_RESOURCES;
1112 }
1113
Chia-I Wu41a1c152017-03-09 15:41:59 -08001114 *outHandle = handle;
Chia-I Wubb61a722016-10-24 15:40:20 +08001115 }
1116
1117 return Error::NONE;
1118}
1119
Steven Thomas58da77e2017-04-26 15:24:28 -07001120Error ComposerClient::CommandReader::updateBuffer(BufferCache cache,
Chia-I Wu41a1c152017-03-09 15:41:59 -08001121 uint32_t slot, bool useCache, buffer_handle_t handle)
1122{
1123 // handle was looked up from cache
1124 if (useCache) {
Steven Thomas58da77e2017-04-26 15:24:28 -07001125 return Error::NONE;
Chia-I Wu41a1c152017-03-09 15:41:59 -08001126 }
1127
1128 std::lock_guard<std::mutex> lock(mClient.mDisplayDataMutex);
1129
1130 BufferCacheEntry* entry = nullptr;
Steven Thomas58da77e2017-04-26 15:24:28 -07001131 Error error = lookupBufferCacheEntryLocked(cache, slot, &entry);
1132 if (error != Error::NONE) {
1133 return error;
1134 }
Chia-I Wu41a1c152017-03-09 15:41:59 -08001135
1136 *entry = handle;
Steven Thomas58da77e2017-04-26 15:24:28 -07001137 return Error::NONE;
Chia-I Wu41a1c152017-03-09 15:41:59 -08001138}
1139
Chia-I Wubb61a722016-10-24 15:40:20 +08001140} // namespace implementation
1141} // namespace V2_1
1142} // namespace composer
1143} // namespace graphics
1144} // namespace hardware
1145} // namespace android